From 4cf127376d35b7ee469241c3fd2e57710d123d49 Mon Sep 17 00:00:00 2001 From: Simon Barner <barner@fortiss.org> Date: Thu, 11 Sep 2014 07:30:17 +0000 Subject: [PATCH] ExtendedCCombo - Do not add grid layout data that determines how the new ExtendedCCombo is added to its enclosing layout and do not set options in the constructor. Both violate the encapsulation and complicate the reuse of ExtendedCCombo in different contexts. - Override all add*() and remove() to ensure consistency of underlying CCombo and list of referenced objects. --- .../base/ui/widget/ExtendedCCombo.java | 95 +++++++++++++++---- 1 file changed, 74 insertions(+), 21 deletions(-) diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/widget/ExtendedCCombo.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/widget/ExtendedCCombo.java index 75d4620d5..2186aeb6b 100644 --- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/widget/ExtendedCCombo.java +++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/widget/ExtendedCCombo.java @@ -21,51 +21,104 @@ import java.util.ArrayList; import java.util.List; import org.eclipse.swt.custom.CCombo; -import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.Composite; /** - * Extention of {@link CCombo} that links the combo box entries with foreign data that can be + * Extension of {@link CCombo} that links the combo box entries with foreign data that can be * added using {@link #add(String, Object)} and queried using {@link #getObject(int)}. * - * @author diewald + * @author diewald, barner * @author $Author$ * @version $Rev$ * @ConQAT.Rating RED Hash: */ public class ExtendedCCombo<T> extends CCombo { - /** Correlates the entries in the combobox with arbitrary objects */ + /** Correlates the entries in the {@link CCombo} with arbitrary objects */ private List<T> objectReferenceList; - /** - * Constructor. - * - * A CCombo is created by this class. Additionally, the layout is defined. - */ + /** Constructs an {@link ExtendedCCombo} */ public ExtendedCCombo(Composite parent, int style) { super(parent, style); objectReferenceList = new ArrayList<T>(); + } - GridData propertyValueGridData = new GridData(); - propertyValueGridData.grabExcessHorizontalSpace = true; - propertyValueGridData.horizontalAlignment = GridData.FILL; + /** + * Adds a {@code comboString} to the {@link CCombo} and references the object + * {@code referencedObject} for this entry. + */ + public void add(String comboString, T referencedObject) { + super.add(comboString); - this.setLayoutData(propertyValueGridData); - this.setEditable(false); + objectReferenceList.add(referencedObject); } - /** Adds a String to the CCombo-box and references an Object for this entry. */ - public void add(String comboString, T referencedObject) { - if(comboString == null || comboString.isEmpty() || referencedObject == null) - return; + /** + * <p> + * {@inheritDoc} + * </p> + * <p> + * <b>NOTE:</b>This method references {@code null} with the given {@code comboString}. Use + * {@link #add(String, Object)} for actually linking an object with a given {@code comboString}. + * </p> + */ + @Override + public void add(String comboString, int index) { + super.add(comboString, index); - this.add(comboString); + objectReferenceList.add(index, null); + } - objectReferenceList.add(referencedObject); + /** + * <p> + * {@inheritDoc} + * </p> + * <p> + * <b>NOTE:</b>This method references {@code null} with the given {@code comboString}. Use + * {@link #add(String, Object)} for actually linking an object with a given {@code comboString}. + * </p> + */ + @Override + public void add(String string) { + add(string, null); + } + + /** {@inheritDoc} */ + @Override + public void removeAll() { + super.removeAll(); + objectReferenceList.clear(); + } + + /** {@inheritDoc} */ + @Override + public void remove(int index) { + super.remove(index); + objectReferenceList.remove(index); + } + + /** {@inheritDoc} */ + @Override + public void remove(int start, int end) { + super.remove(start, end); + // Elements are shifted to the left + // i <= end: super.remove() works in the range [start; end]. + for(int i = start; i <= end; i++) { + objectReferenceList.remove(start); + } + } + + /** {@inheritDoc} */ + @Override + public void remove(String string) { + int index = indexOf(string); + if(index != -1) { + super.remove(string); + objectReferenceList.remove(index); + } } - /** Returns the object(type T) stored at the given index. */ + /** Returns the object (type T) stored at the given index. */ public T getObject(int index) { return objectReferenceList.get(index); } -- GitLab