001: package net.refractions.udig.validation.ui;
002:
003: import java.util.ArrayList;
004:
005: import org.eclipse.jface.viewers.CellEditor;
006: import org.eclipse.jface.viewers.ComboBoxCellEditor;
007: import org.eclipse.jface.viewers.TextCellEditor;
008: import org.eclipse.swt.SWT;
009: import org.eclipse.swt.widgets.Composite;
010: import org.eclipse.swt.widgets.Control;
011: import org.eclipse.swt.widgets.Table;
012: import org.eclipse.swt.widgets.TableItem;
013: import org.geotools.validation.dto.ArgumentDTO;
014:
015: public class AmbiguousCellEditor extends CellEditor {
016:
017: private static final int defaultStyle = SWT.SINGLE;
018:
019: TextCellEditor textCellEditor;
020: ComboBoxCellEditor comboBoxCellEditor;
021: CellEditor cellEditor;
022: Composite parent; //store the parent for "lazy loading"
023:
024: Table table; //we'll save a table reference for future use
025: String[] layers; //list of layers to offer (comboBox labels)
026: String[] typeRefs; //list of actual values to store (comboBox values)
027:
028: public AmbiguousCellEditor(Composite parent, Table table,
029: ArrayList<String> layersList, ArrayList<String> typeRefsList) {
030: this (parent, defaultStyle);
031: this .table = table;
032: this .typeRefs = typeRefsList.toArray(new String[typeRefsList
033: .size()]);
034: this .layers = layersList.toArray(new String[layersList.size()]);
035:
036: }
037:
038: public AmbiguousCellEditor(Composite parent, int style) {
039: super (parent, style);
040: }
041:
042: @Override
043: protected Control createControl(Composite parent) {
044: // normally one would create a control here, but instead we'll create
045: // the controls the first time getControl() is called
046: this .parent = parent;
047: //return cellEditor.getControl();
048: return null;
049: }
050:
051: @Override
052: public Control getControl() {
053: if (textCellEditor == null)
054: createAmbiguousControls();
055: selectAmbiguousControl();
056: return cellEditor.getControl();
057: }
058:
059: @Override
060: protected Object doGetValue() {
061: if (cellEditor instanceof ComboBoxCellEditor) {
062: Object value = cellEditor.getValue();
063: if (value instanceof Integer) {
064: int intVal = ((Integer) value).intValue();
065: if (intVal == -1) {
066: //the user selected nothing (clicked the combo, didn't select anything, and clicked elsewhere)
067: return ""; //$NON-NLS-1$
068: }
069: //translate the integer (comboBox index) into a string value (typeRef)
070: Object newValue = typeRefs[intVal];
071: return newValue;
072: } else {
073: return ""; //$NON-NLS-1$
074: }
075: }
076: return cellEditor.getValue();
077: }
078:
079: @Override
080: protected void doSetFocus() {
081: cellEditor.setFocus();
082: }
083:
084: @Override
085: protected void doSetValue(Object value) {
086: if (cellEditor == null) {
087: createAmbiguousControls();
088: selectAmbiguousControl();
089: }
090: if (cellEditor instanceof ComboBoxCellEditor) {
091: // //translate string value (from "layers") into an integer (comboBox index)
092: int newValue = -1;
093: for (int i = 0; i < typeRefs.length; i++) {
094: if (typeRefs[i].equals(value)) {
095: newValue = i;
096: break;
097: }
098: }
099: cellEditor.setValue(newValue); //use an index, rather than string value
100: } else {
101: cellEditor.setValue(value);
102: }
103: }
104:
105: private void createAmbiguousControls() {
106: textCellEditor = new TextCellEditor(parent);
107: comboBoxCellEditor = new ComboBoxCellEditor(parent, layers);
108: }
109:
110: private void selectAmbiguousControl() {
111: TableItem item = table.getItem(table.getSelectionIndex());
112: ArgumentDTO argDTO = (ArgumentDTO) item.getData();
113: if (argDTO.getName().toLowerCase().contains("typeref")) { //$NON-NLS-1$
114: cellEditor = comboBoxCellEditor;
115: } else {
116: cellEditor = textCellEditor;
117: }
118: }
119: }
|