1 package org.simantics.district.imports.ui.controls;
3 import org.eclipse.jface.preference.FieldEditor;
4 import org.eclipse.swt.SWT;
5 import org.eclipse.swt.events.SelectionAdapter;
6 import org.eclipse.swt.events.SelectionEvent;
7 import org.eclipse.swt.events.SelectionListener;
8 import org.eclipse.swt.layout.GridData;
9 import org.eclipse.swt.widgets.Combo;
10 import org.eclipse.swt.widgets.Composite;
11 import org.eclipse.swt.widgets.Control;
14 * A field editor for a combo box that allows the drop-down selection of one of
19 public class DynamicComboFieldEditor extends FieldEditor {
22 * The <code>Combo</code> widget.
27 * The value (not the name) of the currently selected item in the Combo widget.
29 private String fValue;
32 * The names (labels) and underlying values to populate the combo widget. These should be
33 * arranged as: { {name1, value1}, {name2, value2}, ...}
35 private String[][] fEntryNamesAndValues;
38 * Create the combo box field editor.
40 * @param name the name of the preference this field editor works on
41 * @param labelText the label text of the field editor
42 * @param entryNamesAndValues the names (labels) and underlying values to populate the combo widget. These should be
43 * arranged as: { {name1, value1}, {name2, value2}, ...}
44 * @param parent the parent composite
47 public DynamicComboFieldEditor(String name, String labelText, Composite parent) {
48 init(name, labelText);
49 createControl(parent);
52 public DynamicComboFieldEditor(String name, String labelText, String[][] entryNamesAndValues, Composite parent) {
53 init(name, labelText);
54 fEntryNamesAndValues = entryNamesAndValues;
55 createControl(parent);
58 public void updateCombo(String[][] fEntryNamesAndValues) {
59 this.fEntryNamesAndValues = fEntryNamesAndValues;
60 updateComboBoxControl();
64 * Checks whether given <code>String[][]</code> is of "type"
65 * <code>String[][2]</code>.
67 * @return <code>true</code> if it is ok, and <code>false</code> otherwise
69 private boolean checkArray(String[][] table) {
73 for (int i = 0; i < table.length; i++) {
74 String[] array = table[i];
75 if (array == null || array.length != 2) {
83 protected void adjustForNumColumns(int numColumns) {
85 Control control = getLabelControl();
86 int left = numColumns;
87 if (control != null) {
88 ((GridData)control.getLayoutData()).horizontalSpan = 1;
91 ((GridData)fCombo.getLayoutData()).horizontalSpan = left;
93 Control control = getLabelControl();
94 if (control != null) {
95 ((GridData)control.getLayoutData()).horizontalSpan = 1;
97 ((GridData)fCombo.getLayoutData()).horizontalSpan = 1;
102 protected void doFillIntoGrid(Composite parent, int numColumns) {
104 if (numColumns > 1) {
105 comboC = numColumns - 1;
107 Control control = getLabelControl(parent);
108 GridData gd = new GridData();
109 gd.horizontalSpan = 1;
110 control.setLayoutData(gd);
111 control = getComboBoxControl(parent);
113 gd.horizontalSpan = comboC;
114 gd.horizontalAlignment = GridData.FILL;
115 control.setLayoutData(gd);
116 control.setFont(parent.getFont());
120 protected void doLoad() {
121 updateComboForValue(getPreferenceStore().getString(getPreferenceName()));
125 protected void doLoadDefault() {
126 updateComboForValue(getPreferenceStore().getDefaultString(getPreferenceName()));
130 protected void doStore() {
131 if (fValue == null) {
132 getPreferenceStore().setToDefault(getPreferenceName());
135 getPreferenceStore().setValue(getPreferenceName(), fValue);
139 public int getNumberOfControls() {
144 * Lazily create and return the Combo control.
146 private Combo getComboBoxControl(Composite parent) {
147 if (fCombo == null) {
148 fCombo = new Combo(parent, SWT.READ_ONLY);
149 fCombo.setFont(parent.getFont());
151 fCombo.addSelectionListener(new SelectionAdapter() {
153 public void widgetSelected(SelectionEvent evt) {
154 String oldValue = fValue;
155 String name = fCombo.getText();
156 fValue = getValueForName(name);
157 setPresentsDefaultValue(false);
158 fireValueChanged(VALUE, oldValue, fValue);
162 updateComboBoxControl();
166 private void updateComboBoxControl() {
168 if (fEntryNamesAndValues != null) {
169 for (int i = 0; i < fEntryNamesAndValues.length; i++) {
170 fCombo.add(fEntryNamesAndValues[i][0], i);
176 * Given the name (label) of an entry, return the corresponding value.
178 private String getValueForName(String name) {
179 for (int i = 0; i < fEntryNamesAndValues.length; i++) {
180 String[] entry = fEntryNamesAndValues[i];
181 if (name.equals(entry[0])) {
185 return fEntryNamesAndValues[0][0];
189 * Set the name in the combo widget to match the specified value.
191 private void updateComboForValue(String value) {
193 for (int i = 0; i < fEntryNamesAndValues.length; i++) {
194 if (value.equals(fEntryNamesAndValues[i][1])) {
195 fCombo.setText(fEntryNamesAndValues[i][0]);
199 if (fEntryNamesAndValues.length > 0) {
200 fValue = fEntryNamesAndValues[0][1];
201 fCombo.setText(fEntryNamesAndValues[0][0]);
206 public void setEnabled(boolean enabled, Composite parent) {
207 super.setEnabled(enabled, parent);
208 getComboBoxControl(parent).setEnabled(enabled);
211 public void addComboListener(SelectionListener selectionListener) {
212 fCombo.addSelectionListener(selectionListener);
215 public String getValue() {