Merge branch 'hotfix/5.43.1'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialog / selection / TaxonNodeSelectionDialog.java
1 /**
2 *
3 */
4 package eu.etaxonomy.taxeditor.ui.dialog.selection;
5
6 import java.util.Collections;
7 import java.util.Comparator;
8 import java.util.List;
9 import java.util.Set;
10 import java.util.UUID;
11
12 import org.eclipse.jface.viewers.ILabelProvider;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.SelectionAdapter;
15 import org.eclipse.swt.events.SelectionEvent;
16 import org.eclipse.swt.events.SelectionListener;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Button;
20 import org.eclipse.swt.widgets.Combo;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Label;
24 import org.eclipse.swt.widgets.Shell;
25
26 import eu.etaxonomy.cdm.api.service.IClassificationService;
27 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
28 import eu.etaxonomy.cdm.model.taxon.Classification;
29 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
30 import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
31 import eu.etaxonomy.taxeditor.l10n.Messages;
32 import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
33 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
34 import eu.etaxonomy.taxeditor.store.CdmStore;
35
36 /**
37 * @author p.ciardelli
38 */
39 public class TaxonNodeSelectionDialog extends AbstractFilteredCdmResourceSelectionDialog<TaxonNode> implements SelectionListener{
40
41 public static TaxonNode select(Shell shell,
42 String title, Set<UUID> excludeTaxa, TaxonNode node, UUID classificationUUID, boolean allowSelectClassification) {
43 TaxonNodeSelectionDialog dialog = new TaxonNodeSelectionDialog(shell,
44 //conversation,
45 title,
46 excludeTaxa,
47 false,
48 node,
49 classificationUUID, allowSelectClassification, false);
50 return getSelectionFromDialog(dialog);
51 }
52 public static TaxonNode select(Shell shell,
53 String title, Set<UUID> excludeTaxa, TaxonNode node, UUID classificationUUID) {
54 TaxonNodeSelectionDialog dialog = new TaxonNodeSelectionDialog(shell,
55 //conversation,
56 title,
57 excludeTaxa,
58 false,
59 node,
60 classificationUUID);
61 return getSelectionFromDialog(dialog);
62 }
63
64 public static TaxonNodeAndBoolean select(Shell shell,
65 String title, Set<UUID> excludeTaxa, TaxonNode node, UUID classificationUUID, boolean allowClassification, boolean showDefaultDescription) {
66 TaxonNodeSelectionDialog dialog = new TaxonNodeSelectionDialog(shell,
67 //conversation,
68 title,
69 excludeTaxa,
70 false,
71 node,
72 classificationUUID, allowClassification, showDefaultDescription);
73 TaxonNode nodeResult = getSelectionFromDialog(dialog);
74 TaxonNodeAndBoolean result = new TaxonNodeAndBoolean(nodeResult,dialog.useDefaultDescription);
75
76 return result;
77 }
78
79 public static UuidAndTitleCache<TaxonNode> selectUuidAndTitleCache(Shell shell,
80 String title, Set<UUID> excludeTaxa, TaxonNode node, UUID classificationUUID) {
81 TaxonNodeSelectionDialog dialog = new TaxonNodeSelectionDialog(shell,
82 //conversation,
83 title,
84 excludeTaxa,
85 false,
86 node,
87 classificationUUID, false, false);
88 return getUuidAndTitleCacheSelectionFromDialog(dialog);
89 }
90
91 private Combo classificationSelectionCombo;
92
93 private List<Classification> classifications;
94
95 private Classification selectedClassification;
96 private UUID selectedUuid;
97 // private final Set<UUID> excludeTaxa;
98 private boolean allowClassificationSelection = false;
99
100 private boolean useDefaultDescription = false;
101
102 Button checkDefaultDescription;
103 private boolean showDefaultDescriptionCheck = false;
104
105 protected TaxonNodeSelectionDialog(Shell shell,
106 String title, Set<UUID> excludeTaxa, boolean multi, TaxonNode node, UUID classificationUUID, boolean allowSelectClassification, boolean showDefaultDescription) {
107 super(shell, //conversation,
108 title, multi, TaxonNodeSelectionDialog.class.getCanonicalName(), node);
109
110 ILabelProvider labelProvider = new FilteredCdmResourceLabelProvider();
111
112 setListLabelProvider(labelProvider);
113 // setDetailsLabelProvider(labelProvider);
114 this.cdmBaseToBeFiltered = excludeTaxa;
115 if(classificationUUID != null){
116 selectedUuid = classificationUUID;
117 }
118
119 fillClassifications();
120 this.allowClassificationSelection = allowSelectClassification;
121 this.showDefaultDescriptionCheck = showDefaultDescription;
122
123 //createClassificationSelectionCombo(shell);
124 }
125 protected TaxonNodeSelectionDialog(Shell shell,
126 String title, Set<UUID> excludeTaxa, boolean multi, TaxonNode node, UUID classificationUUID) {
127 this(shell, title, excludeTaxa, multi, node, classificationUUID, false, false);
128 }
129
130 /** {@inheritDoc} */
131 @Override
132 protected Control createDialogArea(Composite parent) {
133 Composite container = (Composite) super.createDialogArea(parent);
134
135 return createClassificationSelectionCombo(container);
136 }
137
138 /*
139 * currently disabled tree selection composite
140 */
141 private Control createClassificationSelectionCombo(Composite parent){
142 // classifications = CdmStore.getTaxonTreeService().list(null, null, null, null, null);
143
144 Composite classificationSelection = new Composite(parent, SWT.NULL);
145 classificationSelection.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, false));
146
147
148 GridLayout layout = new GridLayout();
149 classificationSelection.setLayout(layout);
150 layout.numColumns = 2;
151
152 if (showDefaultDescriptionCheck) {
153
154 Label labelDefault = new Label(classificationSelection, SWT.NULL);
155 labelDefault.setText(Messages.TaxonNodeSelectionDialog_0);
156 GridData labelDefaultLayout = new GridData(SWT.LEFT, SWT.TOP, false, false,1,1);
157 labelDefault.setLayoutData(labelDefaultLayout);
158 checkDefaultDescription = new Button(classificationSelection, SWT.CHECK);
159 checkDefaultDescription.addSelectionListener(new SelectionListener() {
160
161 @Override
162 public void widgetSelected(SelectionEvent e) {
163 useDefaultDescription = checkDefaultDescription.getSelection();
164 }
165
166 @Override
167 public void widgetDefaultSelected(SelectionEvent e) {
168 // TODO Auto-generated method stub
169
170 }
171 });
172
173 }
174
175 Label label = new Label(classificationSelection, SWT.NULL);
176 // TODO not working is not really true but leave it here to remind everyone that this is under construction
177 label.setText(Messages.TaxonNodeSelectionDialog_1);
178 GridData labelLayout = new GridData(SWT.LEFT, SWT.TOP, true, false,2,1);
179 label.setLayoutData(labelLayout);
180 classificationSelectionCombo = new Combo(classificationSelection, SWT.BORDER | SWT.READ_ONLY);
181 classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true));
182
183 for(Classification tree : classifications){
184 classificationSelectionCombo.add(tree.getTitleCache(), classifications.indexOf(tree));
185 if (tree.getUuid().equals(selectedUuid)){
186 selectedClassification = tree;
187 }
188
189 }
190
191 classificationSelectionCombo.select(classifications.indexOf(selectedClassification));
192
193 // TODO remember last selection
194 classificationSelectionCombo.addSelectionListener(this);
195
196
197
198 return classificationSelection;
199 }
200
201 @Override
202 protected String getTitle(TaxonNode taxonNode) {
203 if(taxonNode != null && taxonNode.getTaxon() != null){
204 return taxonNode.getTaxon().getTitleCache();
205 }
206
207 return ""; //$NON-NLS-1$
208 }
209
210 @Override
211 protected SelectionListener getNewWizardButtonSelectionListener(){
212 return new SelectionAdapter() {
213
214 @Override
215 public void widgetSelected(SelectionEvent e) {
216 Classification tree = selectedClassification;
217 setPattern(tree.getRootNode());
218
219 }
220 };
221 }
222
223 @Override
224 protected TaxonNode getPersistentObject(UUID uuid) {
225 return CdmStore.getService(ITaxonNodeService.class).find(uuid);
226 }
227
228 @Override
229 protected void callService(String pattern) {
230 model = CdmStore.getService(IClassificationService.class)
231 .getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(
232 selectedClassification.getUuid(), limitOfInitialElements, pattern, allowClassificationSelection, true);
233 }
234
235 @Override
236 protected void sort() {
237 if(!PreferencesUtil.isSortTaxaByRankAndName()){
238 Collections.sort(model, getItemsComparator());
239 }
240 // otherwise result is already sorted
241 }
242
243 private void fillClassifications() {
244 if(classifications == null){
245 classifications = CdmStore.getService(IClassificationService.class).list(null, null, null, null, null);
246 Collections.sort(classifications, new Comparator<Classification>() {
247
248 @Override
249 public int compare(Classification o1, Classification o2) {
250 if (o1.equals(o2)){
251 return 0;
252 }
253 int result = o1.getTitleCache().compareTo(o2.getTitleCache());
254 if (result == 0){
255 return o1.getUuid().compareTo(o2.getUuid());
256 }
257 return result;
258 }
259 });
260 if (selectedClassification == null){
261 // if (this.cdmBaseToBeFiltered.isEmpty()){
262 selectedClassification = classifications.iterator().next();
263
264 // } else {
265 // selectedClassification = this.cdmBaseToBeFiltered.getClassification();
266 // }
267 }
268 }
269 }
270
271 @Override
272 protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
273 return null;
274 }
275
276 @Override
277 protected String[] getNewWizardText() {
278 return null;
279 }
280
281 @Override
282 public void widgetSelected(SelectionEvent e) {
283 selectedClassification = classifications.get(classificationSelectionCombo.getSelectionIndex());
284
285 // refresh();
286 if (getSearchField().getText() != null){
287 search();
288 }
289 }
290
291 @Override
292 public void widgetDefaultSelected(SelectionEvent e) {}
293 }