X-Git-Url: https://dev.e-taxonomy.eu/gitweb/taxeditor.git/blobdiff_plain/38fbea8ed8d0291a9c3dc4b395b1575d4f38ec99..d2f3794a26d9c4ecef509c22c28cf7ec02f24fc8:/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/combo/TermComboElement.java
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/combo/TermComboElement.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/combo/TermComboElement.java
index f05cbe1c4..c2f5a806f 100644
--- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/combo/TermComboElement.java
+++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/combo/TermComboElement.java
@@ -1,198 +1,126 @@
/**
- *
+ *
*/
package eu.etaxonomy.taxeditor.ui.combo;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.SWT;
-import org.eclipse.swt.events.DisposeEvent;
-import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionEvent;
-import org.eclipse.swt.events.SelectionListener;
-import org.eclipse.swt.graphics.Color;
-import org.eclipse.swt.widgets.Combo;
-import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
-import eu.etaxonomy.cdm.model.common.DefinedTerm;
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
import eu.etaxonomy.cdm.model.common.TermType;
+import eu.etaxonomy.cdm.model.common.TermVocabulary;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
+import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
-import eu.etaxonomy.taxeditor.preference.Resources;
import eu.etaxonomy.taxeditor.store.CdmStore;
-import eu.etaxonomy.taxeditor.store.StoreUtil;
import eu.etaxonomy.taxeditor.store.TermManager;
-import eu.etaxonomy.taxeditor.ui.campanula.compatibility.ICdmFormElement;
-import eu.etaxonomy.taxeditor.ui.element.AbstractCdmFormElement;
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
-import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
-import eu.etaxonomy.taxeditor.ui.element.ISelectable;
-import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
+import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
/**
- *
- * Abstract AbstractTermComboElement class.
- *
- *
* @author n.hoffmann
* @created Nov 5, 2009
* @version 1.0
* @param
*/
public class TermComboElement
- extends AbstractCdmFormElement implements SelectionListener,
- DisposeListener, IEnableableFormElement, ISelectable {
-
- private static final int DEFAULT_VISIBLE_ITEMS = 10;
-
- private T selection;
+ extends AbstractComboElement {
private T emptyElement;
private static String EMPTY_ELEMENT_LABEL = "";
- protected Label label;
- private Combo combo;
-
private ArrayList terms;
private Comparator termComparator;
- private Class termClass;
+ private final TermType termType;
+ private final TermVocabulary termVocabulary;
+ private final Class termClass;
private List customPreferredTerms;
- /**
- *
- * Constructor for AbstractTermComboElement.
- *
- *
- * @param parentElement
- * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
- * object.
- * @param style
- * a int.
- * @param formFactory
- * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
- * object.
- * @param labelString
- * a {@link java.lang.String} object.
- * @param selection
- * a T object.
- * @param visibleItems
- * a int.
- * @param
- * a T object.
- */
- public TermComboElement(CdmFormFactory formFactory,
- ICdmFormElement parentElement, TermType termType, String labelString, T selection,
- int style) {
- super(formFactory, parentElement);
-
- this.termClass = (Class) DefinedTerm.class;
-
- if (labelString != null) {
- label = formFactory.createLabel(getLayoutComposite(), labelString);
- addControl(label);
- }
-
- // create combo
- combo = new Combo(getLayoutComposite(), SWT.BORDER | SWT.READ_ONLY);
- addControl(combo);
- combo.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
- combo.setVisibleItemCount(DEFAULT_VISIBLE_ITEMS);
-
- populateTerms(filterDefinedTermByTermType(getPreferredTerms(), termType));
+ private boolean useAbbrevLabel = false;
+ private boolean addEmptyElement;
- combo.addSelectionListener(this);
- combo.addDisposeListener(this);
- PreferencesUtil.getPreferenceStore().addPropertyChangeListener(this);
-
- if (selection != null) {
- setSelection(selection);
- }
+ public TermComboElement(CdmFormFactory formFactory,
+ ICdmFormElement parentElement, TermType termType, String labelString, T selection, boolean addEmptyElement,
+ int style, boolean useAbbrevLabel) {
+ this(formFactory, parentElement, null, termType, null, labelString, selection, addEmptyElement, style, useAbbrevLabel);
}
- /**
- *
- * Constructor for AbstractTermComboElement.
- *
- *
- * @param parentElement
- * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
- * object.
- * @param style
- * a int.
- * @param formFactory
- * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
- * object.
- * @param labelString
- * a {@link java.lang.String} object.
- * @param selection
- * a T object.
- * @param visibleItems
- * a int.
- * @param
- * a T object.
- */
public TermComboElement(CdmFormFactory formFactory,
- ICdmFormElement parentElement, Class termClass, String labelString, T selection,
- int style) {
- super(formFactory, parentElement);
-
- this.termClass = termClass;
-
- if (labelString != null) {
- label = formFactory.createLabel(getLayoutComposite(), labelString);
- addControl(label);
- }
-
- // create combo
- combo = new Combo(getLayoutComposite(), SWT.BORDER | SWT.READ_ONLY);
- addControl(combo);
- combo.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
- combo.setVisibleItemCount(DEFAULT_VISIBLE_ITEMS);
-
- populateTerms(getPreferredTerms());
-
- combo.addSelectionListener(this);
- combo.addDisposeListener(this);
- PreferencesUtil.getPreferenceStore().addPropertyChangeListener(this);
-
- if (selection != null) {
- setSelection(selection);
- }
+ ICdmFormElement parentElement, TermVocabulary> termVocabulary, String labelString, T selection, boolean addEmptyElement,
+ int style) {
+ this(formFactory, parentElement, null, null, termVocabulary, labelString, selection, addEmptyElement, style, false);
}
- /**
- *
- * Getter for the field selection
.
- *
- *
- * @return a T object.
- */
- public T getSelection() {
- return selection;
+ public TermComboElement(CdmFormFactory formFactory,
+ ICdmFormElement parentElement, Class termClass, String labelString, T selection, boolean addEmptyElement,
+ int style) {
+ this(formFactory, parentElement, termClass, null, null, labelString, selection, addEmptyElement, style, false);
+ }
+ public TermComboElement(CdmFormFactory formFactory,
+ ICdmFormElement parentElement, Class termClass, String labelString, T selection, boolean addEmptyElement,
+ int style, boolean useAbbrevLabel) {
+ this(formFactory, parentElement, termClass, null, null, labelString, selection, addEmptyElement, style, useAbbrevLabel);
+
+ }
+
+ private TermComboElement(CdmFormFactory formFactory,
+ ICdmFormElement parentElement, Class termClass, TermType termType, TermVocabulary> termVocabulary, String labelString, T selection, boolean addEmptyElement,
+ int style, boolean useAbbrevLabel) {
+ super(formFactory, parentElement);
+
+ this.termType = termType;
+ this.termVocabulary = termVocabulary;
+ this.termClass = termClass;
+ this.addEmptyElement = addEmptyElement;
+ this.useAbbrevLabel = useAbbrevLabel;
+ if (labelString != null) {
+ label.setText(labelString);
+ }
+
+ if(termType!=null){
+ //TODO try to remove generic T and avoid classes to be used
+ populateTerms((List) getTermManager().getPreferredTerms(termType));
+ }
+ else if(termVocabulary!=null){
+ populateTerms((List) getTermManager().getPreferredTerms(termVocabulary));
+ }
+ else if(this.termClass!=null){
+ populateTerms(getPreferredTerms());
+ }
+
+ combo.addSelectionListener(this);
+ combo.addDisposeListener(this);
+ PreferencesUtil.getPreferenceStore().addPropertyChangeListener(this);
+
+ if (selection != null) {
+ setSelection(selection);
+ }
}
/**
* Sets the selection of the combo to the given T object.
- * Passing null
to this method will set the selection to
+ *
Passing null
to this method will set the selection to
* the empty element and effectively clear the selection
- *
+ *
* @param selection
* a T object or null
to clear the selection
*/
- public void setSelection(T selection) {
- this.selection = selection;
-
+ @Override
+ public void setSelection(T selection) {
this.selection = selection;
-
+
Listener[] listeners = combo.getListeners(SWT.Selection);
for (Listener listener : listeners) {
@@ -219,7 +147,7 @@ public class TermComboElement
/**
* Fills the combo with elements and sets up the convenience functions
* for selection index
- *
+ *
* @param preferredTerms
*/
private void populateTerms(List preferredTerms) {
@@ -231,9 +159,11 @@ public class TermComboElement
int i = 1;
int index = 0;
- // Add an empty element for when nothing was selected yet
- combo.add(EMPTY_ELEMENT_LABEL);
- terms.add(emptyElement);
+ if(addEmptyElement){
+ // Add an empty element for when nothing was selected yet
+ combo.add(EMPTY_ELEMENT_LABEL);
+ terms.add(emptyElement);
+ }
if (termComparator != null) {
Collections.sort(preferredTerms, termComparator);
@@ -243,12 +173,12 @@ public class TermComboElement
if (label == null) {
if (term.getTitleCache() != null) {
label = term.getTitleCache();
- StoreUtil.warn(getClass(),
+ MessagingUtils.warn(getClass(),
"Term does not have a default language representation: " + label
+ ", " + term.getUuid());
} else {
label = "Unknown";
- StoreUtil.warn(getClass(),
+ MessagingUtils.warn(getClass(),
"Representation Label and TitleCache empty for term: "
+ term + ", " + term.getUuid());
}
@@ -273,33 +203,27 @@ public class TermComboElement
combo.select(index);
}
- /*
- * (non-Javadoc)
- * @see eu.etaxonomy.taxeditor.forms.IEnableableFormElement#setEnabled(boolean)
- */
- /** {@inheritDoc} */
- public void setEnabled(boolean enabled) {
- combo.setEnabled(enabled);
- }
-
- /**
- *
- * preferredTerms
- *
- *
- * @return a {@link java.util.List} object.
- */
protected List getPreferredTerms(){
+ List preferredTerms = new ArrayList();
if (customPreferredTerms != null){
return customPreferredTerms;
}
- return getTermManager().getPreferredTerms(termClass);
+ else if(termType!=null){
+ preferredTerms = getTermManager().getPreferredTerms(termType);
+ }
+ else if(termVocabulary!=null){
+ preferredTerms = getTermManager().getPreferredTerms(termVocabulary);
+ }
+ if(termClass!=null){
+ preferredTerms = getTermManager().getPreferredTerms(termClass);
+ }
+ return preferredTerms;
}
/**
* May be overridden by derived classes if the desired label string does not
* reside in term.getLabel();
- *
+ *
* @param term
* a T object.
* @return a {@link java.lang.String} object.
@@ -308,13 +232,30 @@ public class TermComboElement
if (term == null){
return "";
}else{
- return term.getLabel(CdmStore.getDefaultLanguage());
+ String termLabel = null;
+ if (useAbbrevLabel){
+ termLabel = term.getIdInVocabulary();
+ }else{
+ termLabel = term.getLabel(CdmStore.getDefaultLanguage());
+ }
+ if (termLabel == null){
+ termLabel = term.getLabel();
+ }
+ if(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_VOCABULARY_ID_FOR_TERM_LABELS)
+ && term.getVocabulary()!=null){
+ String vocLabel = term.getVocabulary().getLabel(CdmStore.getDefaultLanguage());
+ if (vocLabel == null){
+ vocLabel = term.getVocabulary().getLabel();
+ }
+ termLabel += " ["+vocLabel+"]";
+ }
+ return termLabel;
}
}
/**
- *
- *
+ *
+ *
* @param term
*/
private void createTermNotInPreferredTerms(T term) {
@@ -325,51 +266,16 @@ public class TermComboElement
populateTerms(preferredTerms);
}
- /**
- *
- * addSelectionListener
- *
- *
- * @param listener
- * a {@link org.eclipse.swt.events.SelectionListener} object.
- */
- public void addSelectionListener(SelectionListener listener) {
- combo.addSelectionListener(listener);
- }
-
- /**
- *
- * removeSelectionListener
- *
- *
- * @param listener
- * a {@link org.eclipse.swt.events.SelectionListener} object.
- */
- public void removeSelectionListener(SelectionListener listener) {
- combo.removeSelectionListener(listener);
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt
- * .events.SelectionEvent)
- */
/** {@inheritDoc} */
- public void widgetSelected(SelectionEvent e) {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
selection = terms.get(combo.getSelectionIndex());
firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
}
- /*
- * (non-Javadoc)
- *
- * @see java.beans.PropertyChangeListener#propertyChange(java.beans.
- * PropertyChangeEvent)
- */
/** {@inheritDoc} */
- public void propertyChange(PropertyChangeEvent event) {
+ @Override
+ public void propertyChange(PropertyChangeEvent event) {
super.propertyChange(event);
if (event != null
&& PreferencesUtil.PREFERRED_TERMS_CHANGE.equals(event
@@ -378,67 +284,21 @@ public class TermComboElement
}
}
- /** {@inheritDoc} */
- public void setSelected(boolean selected) {
- setBackground(selected ? SELECTED : getPersistentBackground());
- }
-
- /** {@inheritDoc} */
- public void widgetDisposed(DisposeEvent e) {
- PreferencesUtil.getPreferenceStore().removePropertyChangeListener(this);
- }
-
- // not used
- /** {@inheritDoc} */
- public void widgetDefaultSelected(SelectionEvent e) {
- }
-
- /** {@inheritDoc} */
- public void setIrrelevant(boolean irrelevant) {
- String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT
- : Resources.COLOR_COMPOSITE_BACKGROUND;
-
- Color color = StoreUtil.getColor(colorId);
- combo.setBackground(color);
- }
-
- /** {@inheritDoc} */
- @Override
- public void setBackground(Color color) {
- if (label != null)
- label.setBackground(color);
- }
-
- /**
- *
- */
protected TermManager getTermManager() {
return CdmStore.getTermManager();
}
-
- /**
- *
- * @return
- */
+
public int getVisibleItemCount(){
return combo.getVisibleItemCount();
}
-
- /**
- *
- * @param count
- */
- public void setVisibleItemCount(int count){
- combo.setVisibleItemCount(count);
- }
/**
* A {@link List} of term objects may be passed to this combo box. In this case, the default behaviour
- * of displaying the preferred terms for the T type will be overridden and the combo will only display the
+ * of displaying the preferred terms for the T type will be overridden and the combo will only display the
* given terms. Also, any previous selection will be reseted.
- *
+ *
* To return to the default of displaying the preferred terms, simply pass null
.
- *
+ *
* @param terms a {@link List} of T objects or null
for default preferred terms
*/
public void setTerms(List terms) {
@@ -446,14 +306,15 @@ public class TermComboElement
customPreferredTerms = terms;
populateTerms(customPreferredTerms);
}
-
- private List filterDefinedTermByTermType(List terms, TermType termType) {
- List definedTerms = new ArrayList();
- for(T term : terms){
- if(term.getTermType().equals(termType)) {
- definedTerms.add(term);
- }
- }
- return definedTerms;
+
+ public void removeEmptyElement(){
+ if(addEmptyElement){
+ if(terms.contains(emptyElement)){
+ terms.remove(emptyElement);
+ }
+ if(Arrays.asList(combo.getItems()).contains(EMPTY_ELEMENT_LABEL)){
+ combo.remove(EMPTY_ELEMENT_LABEL);
+ }
+ }
}
}