Skip to content
Snippets Groups Projects
Commit 4cf12737 authored by Simon Barner's avatar Simon Barner
Browse files

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.
parent 554da314
No related branches found
No related tags found
No related merge requests found
......@@ -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);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment