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