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 @SuppressWarnings("unused")
70 private boolean checkArray(String[][] table) {
74 for (int i = 0; i < table.length; i++) {
75 String[] array = table[i];
76 if (array == null || array.length != 2) {
84 protected void adjustForNumColumns(int numColumns) {
86 Control control = getLabelControl();
87 int left = numColumns;
88 if (control != null) {
89 ((GridData)control.getLayoutData()).horizontalSpan = 1;
92 ((GridData)fCombo.getLayoutData()).horizontalSpan = left;
94 Control control = getLabelControl();
95 if (control != null) {
96 ((GridData)control.getLayoutData()).horizontalSpan = 1;
98 ((GridData)fCombo.getLayoutData()).horizontalSpan = 1;
103 protected void doFillIntoGrid(Composite parent, int numColumns) {
105 if (numColumns > 1) {
106 comboC = numColumns - 1;
108 Control control = getLabelControl(parent);
109 GridData gd = new GridData();
110 gd.horizontalSpan = 1;
111 control.setLayoutData(gd);
112 control = getComboBoxControl(parent);
114 gd.horizontalSpan = comboC;
115 gd.horizontalAlignment = GridData.FILL;
116 control.setLayoutData(gd);
117 control.setFont(parent.getFont());
121 protected void doLoad() {
122 updateComboForValue(getPreferenceStore().getString(getPreferenceName()));
126 protected void doLoadDefault() {
127 updateComboForValue(getPreferenceStore().getDefaultString(getPreferenceName()));
131 protected void doStore() {
132 if (fValue == null) {
133 getPreferenceStore().setToDefault(getPreferenceName());
136 getPreferenceStore().setValue(getPreferenceName(), fValue);
140 public int getNumberOfControls() {
145 * Lazily create and return the Combo control.
147 private Combo getComboBoxControl(Composite parent) {
148 if (fCombo == null) {
149 fCombo = new Combo(parent, SWT.READ_ONLY);
150 fCombo.setFont(parent.getFont());
152 fCombo.addSelectionListener(new SelectionAdapter() {
154 public void widgetSelected(SelectionEvent evt) {
155 String oldValue = fValue;
156 String name = fCombo.getText();
157 fValue = getValueForName(name);
158 setPresentsDefaultValue(false);
159 fireValueChanged(VALUE, oldValue, fValue);
163 updateComboBoxControl();
167 private void updateComboBoxControl() {
169 if (fEntryNamesAndValues != null) {
170 for (int i = 0; i < fEntryNamesAndValues.length; i++) {
171 fCombo.add(fEntryNamesAndValues[i][0], i);
177 * Given the name (label) of an entry, return the corresponding value.
179 private String getValueForName(String name) {
180 for (int i = 0; i < fEntryNamesAndValues.length; i++) {
181 String[] entry = fEntryNamesAndValues[i];
182 if (name.equals(entry[0])) {
186 return fEntryNamesAndValues[0][0];
190 * Set the name in the combo widget to match the specified value.
192 private void updateComboForValue(String value) {
194 for (int i = 0; i < fEntryNamesAndValues.length; i++) {
195 if (value.equals(fEntryNamesAndValues[i][1])) {
196 fCombo.setText(fEntryNamesAndValues[i][0]);
200 if (fEntryNamesAndValues.length > 0) {
201 fValue = fEntryNamesAndValues[0][1];
202 fCombo.setText(fEntryNamesAndValues[0][0]);
207 public void setEnabled(boolean enabled, Composite parent) {
208 super.setEnabled(enabled, parent);
209 getComboBoxControl(parent).setEnabled(enabled);
212 public void addComboListener(SelectionListener selectionListener) {
213 fCombo.addSelectionListener(selectionListener);
216 public String getValue() {