fix multiple representation by removing conversation of selection dialog
[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.UUID;
10
11 import org.eclipse.core.runtime.CoreException;
12 import org.eclipse.jface.viewers.ILabelProvider;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.SelectionEvent;
15 import org.eclipse.swt.events.SelectionListener;
16 import org.eclipse.swt.layout.GridData;
17 import org.eclipse.swt.layout.GridLayout;
18 import org.eclipse.swt.widgets.Combo;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.Label;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.swt.widgets.Text;
24
25 import eu.etaxonomy.cdm.api.service.IClassificationService;
26 import eu.etaxonomy.cdm.model.taxon.Classification;
27 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
28 import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
29 import eu.etaxonomy.taxeditor.store.CdmStore;
30
31 /**
32 * @author p.ciardelli
33 * @version $Id: $
34 */
35 public class TaxonNodeSelectionDialog extends AbstractFilteredCdmResourceSelectionDialog<TaxonNode> implements SelectionListener{
36
37 public static TaxonNode select(Shell shell, //ConversationHolder conversation,
38 String title, List<UUID> excludeTaxa, TaxonNode node, Classification classification) {
39 TaxonNodeSelectionDialog dialog = new TaxonNodeSelectionDialog(shell,
40 //conversation,
41 title,
42 excludeTaxa,
43 false,
44 node,
45 classification);
46 return getSelectionFromDialog(dialog);
47 }
48
49 private Combo classificationSelectionCombo;
50
51 private List<Classification> classifications;
52
53 private Classification selectedClassification;
54 private final List<UUID> excludeTaxa;
55
56 protected TaxonNodeSelectionDialog(Shell shell, //ConversationHolder conversation,
57 String title, List<UUID> excludeTaxa, boolean multi, TaxonNode node, Classification classification) {
58 super(shell, //conversation,
59 title, multi, TaxonNodeSelectionDialog.class.getCanonicalName(), node);
60
61 ILabelProvider labelProvider = new FilteredCdmResourceLabelProvider();
62
63 setListLabelProvider(labelProvider);
64 // setDetailsLabelProvider(labelProvider);
65 this.excludeTaxa = excludeTaxa;
66 if(classification != null){
67 selectedClassification = classification;
68 }
69
70 fillClassifications();
71 createClassificationSelectionCombo(shell);
72 }
73
74 /** {@inheritDoc} */
75 @Override
76 protected Control createDialogArea(Composite parent) {
77 Composite container = (Composite) super.createDialogArea(parent);
78 return createClassificationSelectionCombo(container);
79 }
80
81 /*
82 * currently disabled tree selection composite
83 */
84 private Control createClassificationSelectionCombo(Composite parent){
85 // classifications = CdmStore.getTaxonTreeService().list(null, null, null, null, null);
86
87 Composite classificationSelection = new Composite(parent, SWT.NULL);
88 classificationSelection.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
89
90 GridLayout layout = new GridLayout();
91 classificationSelection.setLayout(layout);
92
93 Label label = new Label(classificationSelection, SWT.NULL);
94 // TODO not working is not really true but leave it here to remind everyone that this is under construction
95 label.setText("Select Classification");
96 classificationSelectionCombo = new Combo(classificationSelection, SWT.BORDER | SWT.READ_ONLY);
97 classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true));
98
99 for(Classification tree : classifications){
100 classificationSelectionCombo.add(tree.getName().getText(), classifications.indexOf(tree));
101
102 }
103
104 classificationSelectionCombo.select(classifications.indexOf(selectedClassification));
105
106 // TODO remember last selection
107 classificationSelectionCombo.addSelectionListener(this);
108
109
110
111 return classificationSelection;
112 }
113
114 /** {@inheritDoc} */
115 @Override
116 protected String getTitle(TaxonNode taxonNode) {
117 if(taxonNode != null && taxonNode.getTaxon() != null){
118 return taxonNode.getTaxon().getTitleCache();
119 }
120
121 return "";
122 }
123
124 /** {@inheritDoc} */
125 @Override
126 protected TaxonNode getPersistentObject(UUID uuid) {
127 return CdmStore.getService(IClassificationService.class).getTaxonNodeByUuid(uuid);
128 }
129
130 /** {@inheritDoc} */
131 @Override
132 protected void search() {
133 // default to first tree
134 // TODO this will be problematic and can only be seen as workaround
135
136 Control control =getSearchField();
137 String pattern = null;
138 if (control != null){
139 pattern = ((Text)control).getText();
140 }
141
142
143 // fillClassifications();
144 if (pattern == null || pattern.equals("?")){
145 model = CdmStore.getService(IClassificationService.class).getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(selectedClassification.getUuid(), null, null);
146 }else{
147 model = CdmStore.getService(IClassificationService.class).getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(selectedClassification.getUuid(), limitOfInitialElements, pattern);
148 }
149 try {
150 fillContentProvider(null);
151 } catch (CoreException e) {
152 // TODO Auto-generated catch block
153 e.printStackTrace();
154 }
155 }
156
157 private void fillClassifications() {
158 if(classifications == null){
159 classifications = CdmStore.getService(IClassificationService.class).list(null, null, null, null, null);
160 Collections.sort(classifications, new Comparator<Classification>() {
161
162 @Override
163 public int compare(Classification o1, Classification o2) {
164 if (o1.equals(o2)){
165 return 0;
166 }
167 int result = o1.getTitleCache().compareTo(o2.getTitleCache());
168 if (result == 0){
169 return o1.getUuid().compareTo(o2.getUuid());
170 }
171 return result;
172 }
173 });
174 if (this.cdmBaseToBeFiltered == null){
175 selectedClassification = classifications.iterator().next();
176 } else {
177 selectedClassification = this.cdmBaseToBeFiltered.getClassification();
178 }
179 }
180 }
181
182 /** {@inheritDoc} */
183 @Override
184 protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
185 return null;
186 }
187
188 /** {@inheritDoc} */
189 @Override
190 protected String[] getNewWizardText() {
191 return null;
192 }
193
194 /** {@inheritDoc} */
195 @Override
196 public void widgetSelected(SelectionEvent e) {
197 selectedClassification = classifications.get(classificationSelectionCombo.getSelectionIndex());
198
199 // refresh();
200 if (getSearchField().getText() != null){
201 search();
202 }
203 }
204
205 /** {@inheritDoc} */
206 @Override
207 public void widgetDefaultSelected(SelectionEvent e) {}
208
209
210 }