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