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