1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.taxeditor.ui.combo;
|
5
|
|
6
|
import java.util.ArrayList;
|
7
|
import java.util.Collections;
|
8
|
import java.util.Comparator;
|
9
|
import java.util.List;
|
10
|
|
11
|
import org.eclipse.jface.util.PropertyChangeEvent;
|
12
|
import org.eclipse.swt.SWT;
|
13
|
import org.eclipse.swt.events.SelectionEvent;
|
14
|
import org.eclipse.swt.widgets.Listener;
|
15
|
|
16
|
import eu.etaxonomy.cdm.api.service.IVocabularyService;
|
17
|
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
|
18
|
import eu.etaxonomy.cdm.model.term.TermType;
|
19
|
import eu.etaxonomy.cdm.model.term.TermVocabulary;
|
20
|
import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
|
21
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
22
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
23
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
24
|
import eu.etaxonomy.taxeditor.store.TermManager;
|
25
|
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
|
26
|
import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
|
27
|
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
|
28
|
|
29
|
/**
|
30
|
* @author n.hoffmann
|
31
|
* @created Nov 5, 2009
|
32
|
* @version 1.0
|
33
|
* @param <VOC>
|
34
|
*/
|
35
|
public class VocabularyComboElement<TERM extends DefinedTermBase<TERM>, VOC extends TermVocabulary<TERM>>
|
36
|
extends AbstractComboElement<VOC> {
|
37
|
|
38
|
private VOC emptyElement;
|
39
|
private static String EMPTY_ELEMENT_LABEL = "";
|
40
|
|
41
|
private ArrayList<VOC> terms;
|
42
|
|
43
|
private Comparator<VOC> vocComparator;
|
44
|
|
45
|
private final TermType termType;
|
46
|
|
47
|
public VocabularyComboElement(CdmFormFactory formFactory,
|
48
|
ICdmFormElement parentElement, TermType termType, String labelString, VOC selection,
|
49
|
int style) {
|
50
|
super(formFactory, parentElement);
|
51
|
|
52
|
this.termType = termType;
|
53
|
|
54
|
populateTerms(getVocabularies());
|
55
|
|
56
|
combo.addSelectionListener(this);
|
57
|
combo.addDisposeListener(this);
|
58
|
addContentProposalAdapter();
|
59
|
|
60
|
PreferencesUtil.getPreferenceStore().addPropertyChangeListener(this);
|
61
|
|
62
|
if (selection != null) {
|
63
|
setSelection(selection);
|
64
|
}
|
65
|
}
|
66
|
|
67
|
public VocabularyComboElement(CdmFormFactory formFactory,
|
68
|
ICdmFormElement parentElement, TermType termType, String labelString, TermVocabularyDto selection,
|
69
|
int style) {
|
70
|
super(formFactory, parentElement);
|
71
|
|
72
|
this.termType = termType;
|
73
|
|
74
|
populateTerms(getVocabularies());
|
75
|
|
76
|
combo.addSelectionListener(this);
|
77
|
combo.addDisposeListener(this);
|
78
|
addContentProposalAdapter();
|
79
|
|
80
|
PreferencesUtil.getPreferenceStore().addPropertyChangeListener(this);
|
81
|
|
82
|
if (selection != null) {
|
83
|
setSelectionDto(selection);
|
84
|
}
|
85
|
}
|
86
|
|
87
|
|
88
|
|
89
|
|
90
|
/**
|
91
|
* <p>Sets the selection of the combo to the given T object.</p>
|
92
|
* <p>Passing <code>null</code> to this method will set the selection to
|
93
|
* the empty element and effectively clear the selection</p>
|
94
|
*
|
95
|
* @param selection
|
96
|
* a T object or <code>null</code> to clear the selection
|
97
|
*/
|
98
|
@Override
|
99
|
public void setSelection(VOC selection) {
|
100
|
this.selection = selection;
|
101
|
|
102
|
Listener[] listeners = combo.getListeners(SWT.Selection);
|
103
|
|
104
|
// for (Listener listener : listeners) {
|
105
|
// combo.removeListener(SWT.Selection, listener);
|
106
|
// }
|
107
|
int selectedIndex;
|
108
|
if(selection == null){
|
109
|
// set selection to the emptyElement
|
110
|
selectedIndex = 0;
|
111
|
}else{
|
112
|
selectedIndex = terms.indexOf(selection);
|
113
|
if (selectedIndex == -1) {
|
114
|
createTermNotInPreferredTerms(selection);
|
115
|
selectedIndex = terms.indexOf(selection);
|
116
|
}
|
117
|
}
|
118
|
combo.select(selectedIndex);
|
119
|
|
120
|
// for (Listener listener : listeners) {
|
121
|
// combo.addListener(SWT.Selection, listener);
|
122
|
// }
|
123
|
}
|
124
|
|
125
|
|
126
|
public void setSelectionDto(TermVocabularyDto selection) {
|
127
|
Listener[] listeners = combo.getListeners(SWT.Selection);
|
128
|
|
129
|
// for (Listener listener : listeners) {
|
130
|
// combo.removeListener(SWT.Selection, listener);
|
131
|
// }
|
132
|
int selectedIndex = 0;
|
133
|
if(selection == null){
|
134
|
// set selection to the emptyElement
|
135
|
selectedIndex = 0;
|
136
|
}else{
|
137
|
for (VOC voc: terms){
|
138
|
|
139
|
if (voc != null && voc.getUuid().equals(selection.getUuid())){
|
140
|
selectedIndex = terms.indexOf(voc);
|
141
|
}
|
142
|
}
|
143
|
|
144
|
// if (selectedIndex == -1) {
|
145
|
// createTermNotInPreferredTerms(selection);
|
146
|
// selectedIndex = terms.indexOf(selection);
|
147
|
// }
|
148
|
}
|
149
|
if (selectedIndex > -1) {
|
150
|
combo.select(selectedIndex);
|
151
|
}
|
152
|
|
153
|
// for (Listener listener : listeners) {
|
154
|
// combo.addListener(SWT.Selection, listener);
|
155
|
// }
|
156
|
}
|
157
|
|
158
|
/**
|
159
|
* Fills the combo with elements and sets up the convenience functions
|
160
|
* for selection index
|
161
|
*
|
162
|
* @param preferredTerms
|
163
|
*/
|
164
|
private void populateTerms(List<VOC> preferredTerms) {
|
165
|
|
166
|
combo.removeAll();
|
167
|
|
168
|
terms = new ArrayList<VOC>();
|
169
|
|
170
|
int i = 1;
|
171
|
int index = 0;
|
172
|
|
173
|
// Add an empty element for when nothing was selected yet
|
174
|
combo.add(EMPTY_ELEMENT_LABEL);
|
175
|
terms.add(emptyElement);
|
176
|
|
177
|
if (vocComparator != null) {
|
178
|
Collections.sort(preferredTerms, vocComparator);
|
179
|
}else{
|
180
|
Collections.sort(preferredTerms, new Comparator<VOC>() {
|
181
|
|
182
|
@Override
|
183
|
public int compare(VOC o1, VOC o2) {
|
184
|
if (o1.equals(o2)){
|
185
|
return 0;
|
186
|
}
|
187
|
int result = ((TermVocabulary) o1).getTitleCache().toLowerCase().compareTo(((TermVocabulary)o2).getTitleCache().toLowerCase());
|
188
|
|
189
|
return result;
|
190
|
}
|
191
|
});
|
192
|
}
|
193
|
for (VOC term : preferredTerms) {
|
194
|
String label = getLabel(term);
|
195
|
if (label == null) {
|
196
|
if (term.getTitleCache() != null) {
|
197
|
label = term.getTitleCache();
|
198
|
MessagingUtils.warn(getClass(),
|
199
|
"Term does not have a default language representation: " + label
|
200
|
+ ", " + term.getUuid());
|
201
|
} else {
|
202
|
label = "Unknown";
|
203
|
MessagingUtils.warn(getClass(),
|
204
|
"Representation Label and TitleCache empty for term: "
|
205
|
+ term + ", " + term.getUuid());
|
206
|
}
|
207
|
|
208
|
}
|
209
|
|
210
|
combo.add(label);
|
211
|
combo.setData(label, term);
|
212
|
terms.add(term);
|
213
|
|
214
|
i++;
|
215
|
if (selection != null) {
|
216
|
if (selection.equals(term)) {
|
217
|
index = i;
|
218
|
}
|
219
|
}
|
220
|
}
|
221
|
|
222
|
if (selection != null && index == 0) {
|
223
|
createTermNotInPreferredTerms(selection);
|
224
|
}
|
225
|
|
226
|
combo.select(index);
|
227
|
}
|
228
|
|
229
|
protected List<VOC> getVocabularies(){
|
230
|
List<TermVocabulary<TERM>> list = CdmStore.getService(IVocabularyService.class).<TERM>findByTermType(termType, null);
|
231
|
return (List<VOC>) list;
|
232
|
}
|
233
|
|
234
|
/**
|
235
|
* May be overridden by derived classes if the desired label string does not
|
236
|
* reside in term.getLabel();
|
237
|
*
|
238
|
* @param term
|
239
|
* a T object.
|
240
|
* @return a {@link java.lang.String} object.
|
241
|
*/
|
242
|
protected String getLabel(VOC term) {
|
243
|
if (term == null){
|
244
|
return "";
|
245
|
}else{
|
246
|
return term.getLabel(CdmStore.getDefaultLanguage());
|
247
|
}
|
248
|
}
|
249
|
|
250
|
private void createTermNotInPreferredTerms(VOC term) {
|
251
|
List<VOC> preferredTerms = getVocabularies();
|
252
|
|
253
|
preferredTerms.add(term);
|
254
|
|
255
|
populateTerms(preferredTerms);
|
256
|
}
|
257
|
|
258
|
/** {@inheritDoc} */
|
259
|
@Override
|
260
|
public void widgetSelected(SelectionEvent e) {
|
261
|
int i = combo.getSelectionIndex();
|
262
|
if (i > -1){
|
263
|
selection = terms.get(i);
|
264
|
}else{
|
265
|
selection = null;
|
266
|
}
|
267
|
firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
|
268
|
}
|
269
|
|
270
|
/** {@inheritDoc} */
|
271
|
@Override
|
272
|
public void propertyChange(PropertyChangeEvent event) {
|
273
|
super.propertyChange(event);
|
274
|
if (event != null
|
275
|
&& PreferencesUtil.PREFERRED_TERMS_CHANGE.equals(event
|
276
|
.getProperty())) {
|
277
|
populateTerms(getVocabularies());
|
278
|
}
|
279
|
}
|
280
|
|
281
|
protected TermManager getTermManager() {
|
282
|
return CdmStore.getTermManager();
|
283
|
}
|
284
|
|
285
|
public int getVisibleItemCount(){
|
286
|
return combo.getVisibleItemCount();
|
287
|
}
|
288
|
|
289
|
public void removeEmptyElement(){
|
290
|
terms.remove(emptyElement);
|
291
|
combo.remove(EMPTY_ELEMENT_LABEL);
|
292
|
}
|
293
|
|
294
|
}
|