2 * Copyright (C) 2016 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.taxeditor
.ui
.combo
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Collections
;
13 import java
.util
.Comparator
;
14 import java
.util
.List
;
16 import org
.eclipse
.jface
.util
.PropertyChangeEvent
;
17 import org
.eclipse
.swt
.SWT
;
18 import org
.eclipse
.swt
.events
.SelectionEvent
;
19 import org
.eclipse
.swt
.widgets
.Listener
;
21 import eu
.etaxonomy
.cdm
.api
.service
.IVocabularyService
;
22 import eu
.etaxonomy
.cdm
.model
.term
.DefinedTermBase
;
23 import eu
.etaxonomy
.cdm
.model
.term
.TermType
;
24 import eu
.etaxonomy
.cdm
.model
.term
.TermVocabulary
;
25 import eu
.etaxonomy
.cdm
.persistence
.dto
.TermVocabularyDto
;
26 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
27 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
28 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
29 import eu
.etaxonomy
.taxeditor
.store
.TermManager
;
30 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
31 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmPropertyChangeEvent
;
32 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
36 * @created Nov 5, 2009
38 public class VocabularyComboElement
<TERM
extends DefinedTermBase
<TERM
>, VOC
extends TermVocabulary
<TERM
>>
39 extends AbstractComboElement
<VOC
> {
41 private VOC emptyElement
;
42 private static String EMPTY_ELEMENT_LABEL
= "";
44 private ArrayList
<VOC
> terms
;
46 private Comparator
<VOC
> vocComparator
;
48 private final TermType termType
;
50 public VocabularyComboElement(CdmFormFactory formFactory
,
51 ICdmFormElement parentElement
, TermType termType
, String labelString
,
52 VOC selection
, int style
) {
53 super(formFactory
, parentElement
);
55 this.termType
= termType
;
57 populateTerms(getVocabularies());
59 combo
.addSelectionListener(this);
60 combo
.addDisposeListener(this);
61 addContentProposalAdapter();
63 PreferencesUtil
.getPreferenceStore().addPropertyChangeListener(this);
65 if (selection
!= null) {
66 setSelection(selection
);
70 public VocabularyComboElement(CdmFormFactory formFactory
,
71 ICdmFormElement parentElement
, TermType termType
, String labelString
,
72 TermVocabularyDto selection
, int style
) {
73 super(formFactory
, parentElement
);
75 this.termType
= termType
;
77 populateTerms(getVocabularies());
79 combo
.addSelectionListener(this);
80 combo
.addDisposeListener(this);
81 addContentProposalAdapter();
83 PreferencesUtil
.getPreferenceStore().addPropertyChangeListener(this);
85 if (selection
!= null) {
86 setSelectionDto(selection
);
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>
96 * a T object or <code>null</code> to clear the selection
99 public void setSelection(VOC selection
) {
100 this.selection
= selection
;
102 Listener
[] listeners
= combo
.getListeners(SWT
.Selection
);
104 // for (Listener listener : listeners) {
105 // combo.removeListener(SWT.Selection, listener);
108 if(selection
== null){
109 // set selection to the emptyElement
112 selectedIndex
= terms
.indexOf(selection
);
113 if (selectedIndex
== -1) {
114 createTermNotInPreferredTerms(selection
);
115 selectedIndex
= terms
.indexOf(selection
);
118 combo
.select(selectedIndex
);
120 // for (Listener listener : listeners) {
121 // combo.addListener(SWT.Selection, listener);
125 public void setSelectionDto(TermVocabularyDto selection
) {
126 Listener
[] listeners
= combo
.getListeners(SWT
.Selection
);
128 // for (Listener listener : listeners) {
129 // combo.removeListener(SWT.Selection, listener);
131 int selectedIndex
= 0;
132 if(selection
== null){
133 // set selection to the emptyElement
136 for (VOC voc
: terms
){
138 if (voc
!= null && voc
.getUuid().equals(selection
.getUuid())){
139 selectedIndex
= terms
.indexOf(voc
);
143 // if (selectedIndex == -1) {
144 // createTermNotInPreferredTerms(selection);
145 // selectedIndex = terms.indexOf(selection);
148 if (selectedIndex
> -1) {
149 combo
.select(selectedIndex
);
152 // for (Listener listener : listeners) {
153 // combo.addListener(SWT.Selection, listener);
158 * Fills the combo with elements and sets up the convenience functions
159 * for selection index
161 * @param preferredTerms
163 private void populateTerms(List
<VOC
> preferredTerms
) {
167 terms
= new ArrayList
<VOC
>();
172 // Add an empty element for when nothing was selected yet
173 combo
.add(EMPTY_ELEMENT_LABEL
);
174 terms
.add(emptyElement
);
176 if (vocComparator
!= null) {
177 Collections
.sort(preferredTerms
, vocComparator
);
179 Collections
.sort(preferredTerms
, new Comparator
<VOC
>() {
182 public int compare(VOC o1
, VOC o2
) {
186 int result
= ((TermVocabulary
) o1
).getTitleCache().toLowerCase().compareTo(((TermVocabulary
)o2
).getTitleCache().toLowerCase());
192 for (VOC term
: preferredTerms
) {
193 String label
= getLabel(term
);
195 if (term
.getTitleCache() != null) {
196 label
= term
.getTitleCache();
197 MessagingUtils
.warn(getClass(),
198 "Term does not have a default language representation: " + label
199 + ", " + term
.getUuid());
202 MessagingUtils
.warn(getClass(),
203 "Representation Label and TitleCache empty for term: "
204 + term
+ ", " + term
.getUuid());
210 combo
.setData(label
, term
);
214 if (selection
!= null) {
215 if (selection
.equals(term
)) {
221 if (selection
!= null && index
== 0) {
222 createTermNotInPreferredTerms(selection
);
228 protected List
<VOC
> getVocabularies(){
229 List
<TermVocabulary
<TERM
>> list
= CdmStore
.getService(IVocabularyService
.class).<TERM
>findByTermType(termType
, null);
230 return (List
<VOC
>) list
;
234 * May be overridden by derived classes if the desired label string does not
235 * reside in term.getLabel();
239 * @return a {@link java.lang.String} object.
241 protected String
getLabel(VOC term
) {
245 return term
.getLabel(CdmStore
.getDefaultLanguage());
249 private void createTermNotInPreferredTerms(VOC term
) {
250 List
<VOC
> preferredTerms
= getVocabularies();
252 preferredTerms
.add(term
);
254 populateTerms(preferredTerms
);
258 public void widgetSelected(SelectionEvent e
) {
259 int i
= combo
.getSelectionIndex();
261 selection
= terms
.get(i
);
265 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e
));
269 public void propertyChange(PropertyChangeEvent event
) {
270 super.propertyChange(event
);
272 && PreferencesUtil
.PREFERRED_TERMS_CHANGE
.equals(event
274 populateTerms(getVocabularies());
278 protected TermManager
getTermManager() {
279 return CdmStore
.getTermManager();
282 public int getVisibleItemCount(){
283 return combo
.getVisibleItemCount();
286 public void removeEmptyElement(){
287 terms
.remove(emptyElement
);
288 combo
.remove(EMPTY_ELEMENT_LABEL
);