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.element.AbstractCdmFormElement;
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
-import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
-import eu.etaxonomy.taxeditor.ui.element.ISelectable;
-import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
/**
- * <p>
- * Abstract AbstractTermComboElement class.
- * </p>
- *
* @author n.hoffmann
* @created Nov 5, 2009
* @version 1.0
* @param <T>
*/
public class TermComboElement<T extends DefinedTermBase>
- extends AbstractCdmFormElement implements SelectionListener,
- DisposeListener, IEnableableFormElement, ISelectable {
-
- private static final int DEFAULT_VISIBLE_ITEMS = 10;
-
- private T selection;
+ extends AbstractComboElement<T> {
private T emptyElement;
private static String EMPTY_ELEMENT_LABEL = "";
- protected Label label;
- private final Combo combo;
-
private ArrayList<T> terms;
private Comparator<T> termComparator;
+ private final TermType termType;
+ private final TermVocabulary termVocabulary;
private final Class<T> termClass;
private List<T> customPreferredTerms;
- /**
- * <p>
- * Constructor for AbstractTermComboElement.
- * </p>
- *
- * @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 <T>
- * a T object.
- */
- public TermComboElement(CdmFormFactory formFactory,
- ICdmFormElement parentElement, TermType termType, String labelString, T selection,
- int style) {
- super(formFactory, parentElement);
-
- this.termClass = (Class<T>) 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);
}
- /**
- * <p>
- * Constructor for AbstractTermComboElement.
- * </p>
- *
- * @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 <T>
- * a T object.
- */
public TermComboElement(CdmFormFactory formFactory,
- ICdmFormElement parentElement, Class<T> 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);
+ ICdmFormElement parentElement, TermVocabulary<?> termVocabulary, String labelString, T selection, boolean addEmptyElement,
+ int style) {
+ this(formFactory, parentElement, null, null, termVocabulary, labelString, selection, addEmptyElement, style, false);
+ }
- populateTerms(getPreferredTerms());
+ public TermComboElement(CdmFormFactory formFactory,
+ ICdmFormElement parentElement, Class<T> 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<T> 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<T> 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);
+ }
- combo.addSelectionListener(this);
- combo.addDisposeListener(this);
- PreferencesUtil.getPreferenceStore().addPropertyChangeListener(this);
+ if(termType!=null){
+ //TODO try to remove generic T and avoid classes to be used
+ populateTerms((List<T>) getTermManager().getPreferredTerms(termType));
+ }
+ else if(termVocabulary!=null){
+ populateTerms((List<T>) getTermManager().getPreferredTerms(termVocabulary));
+ }
+ else if(this.termClass!=null){
+ populateTerms(getPreferredTerms());
+ }
- if (selection != null) {
- setSelection(selection);
- }
- }
+ combo.addSelectionListener(this);
+ combo.addDisposeListener(this);
+ PreferencesUtil.getPreferenceStore().addPropertyChangeListener(this);
- /**
- * <p>
- * Getter for the field <code>selection</code>.
- * </p>
- *
- * @return a T object.
- */
- public T getSelection() {
- return selection;
+ if (selection != null) {
+ setSelection(selection);
+ }
}
/**
* @param selection
* a T object or <code>null</code> 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);
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);
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());
}
combo.select(index);
}
- /*
- * (non-Javadoc)
- * @see eu.etaxonomy.taxeditor.forms.IEnableableFormElement#setEnabled(boolean)
- */
- /** {@inheritDoc} */
- @Override
- public void setEnabled(boolean enabled) {
- combo.setEnabled(enabled);
- }
-
- /* (non-Javadoc)
- * @see eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement#isEnabled()
- */
- @Override
- public boolean isEnabled() {
- return combo.isEnabled();
- }
-
- /**
- * <p>
- * preferredTerms
- * </p>
- *
- * @return a {@link java.util.List} object.
- */
protected List<T> getPreferredTerms(){
+ List<T> preferredTerms = new ArrayList<T>();
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;
}
/**
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;
}
}
populateTerms(preferredTerms);
}
- /**
- * <p>
- * addSelectionListener
- * </p>
- *
- * @param listener
- * a {@link org.eclipse.swt.events.SelectionListener} object.
- */
- public void addSelectionListener(SelectionListener listener) {
- combo.addSelectionListener(listener);
- }
-
- /**
- * <p>
- * removeSelectionListener
- * </p>
- *
- * @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} */
@Override
public void widgetSelected(SelectionEvent e) {
firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
}
- /*
- * (non-Javadoc)
- *
- * @see java.beans.PropertyChangeListener#propertyChange(java.beans.
- * PropertyChangeEvent)
- */
/** {@inheritDoc} */
@Override
public void propertyChange(PropertyChangeEvent event) {
}
}
- /** {@inheritDoc} */
- @Override
- public void setSelected(boolean selected) {
- setBackground(selected ? SELECTED : getPersistentBackground());
- }
-
- /** {@inheritDoc} */
- @Override
- public void widgetDisposed(DisposeEvent e) {
- PreferencesUtil.getPreferenceStore().removePropertyChangeListener(this);
- }
-
- // not used
- /** {@inheritDoc} */
- @Override
- public void widgetDefaultSelected(SelectionEvent e) {
- }
-
- /** {@inheritDoc} */
- @Override
- 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);
- }
-
/**
* <p>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
populateTerms(customPreferredTerms);
}
- private List<T> filterDefinedTermByTermType(List<T> terms, TermType termType) {
- List<T> definedTerms = new ArrayList<T>();
- 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);
+ }
+ }
}
}