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