]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/controls/DynamicComboFieldEditor.java
Enhancements to district functionalities and code
[simantics/district.git] / org.simantics.district.imports.ui / src / org / simantics / district / imports / ui / controls / DynamicComboFieldEditor.java
1 package org.simantics.district.imports.ui.controls;
2
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;
12
13 /**
14  * A field editor for a combo box that allows the drop-down selection of one of
15  * a list of items.
16  *
17  * @since 3.3
18  */
19 public class DynamicComboFieldEditor extends FieldEditor {
20
21         /**
22          * The <code>Combo</code> widget.
23          */
24         private Combo fCombo;
25
26         /**
27          * The value (not the name) of the currently selected item in the Combo widget.
28          */
29         private String fValue;
30
31         /**
32          * The names (labels) and underlying values to populate the combo widget.  These should be
33          * arranged as: { {name1, value1}, {name2, value2}, ...}
34          */
35         private String[][] fEntryNamesAndValues;
36
37         /**
38          * Create the combo box field editor.
39          *
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
45          */
46         
47         public DynamicComboFieldEditor(String name, String labelText, Composite parent) {
48             init(name, labelText);
49             createControl(parent);
50         }
51         
52         public DynamicComboFieldEditor(String name, String labelText, String[][] entryNamesAndValues, Composite parent) {
53                 init(name, labelText);
54                 fEntryNamesAndValues = entryNamesAndValues;
55                 createControl(parent);
56         }
57         
58         public void updateCombo(String[][] fEntryNamesAndValues) {
59             this.fEntryNamesAndValues = fEntryNamesAndValues;
60             updateComboBoxControl();
61         }
62
63         /**
64          * Checks whether given <code>String[][]</code> is of "type"
65          * <code>String[][2]</code>.
66          *
67          * @return <code>true</code> if it is ok, and <code>false</code> otherwise
68          */
69         private boolean checkArray(String[][] table) {
70                 if (table == null) {
71                         return false;
72                 }
73                 for (int i = 0; i < table.length; i++) {
74                         String[] array = table[i];
75                         if (array == null || array.length != 2) {
76                                 return false;
77                         }
78                 }
79                 return true;
80         }
81
82         @Override
83         protected void adjustForNumColumns(int numColumns) {
84                 if (numColumns > 1) {
85                         Control control = getLabelControl();
86                         int left = numColumns;
87                         if (control != null) {
88                                 ((GridData)control.getLayoutData()).horizontalSpan = 1;
89                                 left = left - 1;
90                         }
91                         ((GridData)fCombo.getLayoutData()).horizontalSpan = left;
92                 } else {
93                         Control control = getLabelControl();
94                         if (control != null) {
95                                 ((GridData)control.getLayoutData()).horizontalSpan = 1;
96                         }
97                         ((GridData)fCombo.getLayoutData()).horizontalSpan = 1;
98                 }
99         }
100
101         @Override
102         protected void doFillIntoGrid(Composite parent, int numColumns) {
103                 int comboC = 1;
104                 if (numColumns > 1) {
105                         comboC = numColumns - 1;
106                 }
107                 Control control = getLabelControl(parent);
108                 GridData gd = new GridData();
109                 gd.horizontalSpan = 1;
110                 control.setLayoutData(gd);
111                 control = getComboBoxControl(parent);
112                 gd = new GridData();
113                 gd.horizontalSpan = comboC;
114                 gd.horizontalAlignment = GridData.FILL;
115                 control.setLayoutData(gd);
116                 control.setFont(parent.getFont());
117         }
118
119         @Override
120         protected void doLoad() {
121                 updateComboForValue(getPreferenceStore().getString(getPreferenceName()));
122         }
123
124         @Override
125         protected void doLoadDefault() {
126                 updateComboForValue(getPreferenceStore().getDefaultString(getPreferenceName()));
127         }
128
129         @Override
130         protected void doStore() {
131                 if (fValue == null) {
132                         getPreferenceStore().setToDefault(getPreferenceName());
133                         return;
134                 }
135                 getPreferenceStore().setValue(getPreferenceName(), fValue);
136         }
137
138         @Override
139         public int getNumberOfControls() {
140                 return 2;
141         }
142
143         /*
144          * Lazily create and return the Combo control.
145          */
146         private Combo getComboBoxControl(Composite parent) {
147                 if (fCombo == null) {
148             fCombo = new Combo(parent, SWT.READ_ONLY);
149             fCombo.setFont(parent.getFont());
150
151             fCombo.addSelectionListener(new SelectionAdapter() {
152                 @Override
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);
159                 }
160             });
161                 }
162                 updateComboBoxControl();
163                 return fCombo;
164         }
165         
166         private void updateComboBoxControl() {
167             fCombo.removeAll();
168             if (fEntryNamesAndValues != null) {
169             for (int i = 0; i < fEntryNamesAndValues.length; i++) {
170                 fCombo.add(fEntryNamesAndValues[i][0], i);
171             }
172             }
173         }
174
175         /*
176          * Given the name (label) of an entry, return the corresponding value.
177          */
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])) {
182                                 return entry[1];
183                         }
184                 }
185                 return fEntryNamesAndValues[0][0];
186         }
187
188         /*
189          * Set the name in the combo widget to match the specified value.
190          */
191         private void updateComboForValue(String value) {
192                 fValue = value;
193                 for (int i = 0; i < fEntryNamesAndValues.length; i++) {
194                         if (value.equals(fEntryNamesAndValues[i][1])) {
195                                 fCombo.setText(fEntryNamesAndValues[i][0]);
196                                 return;
197                         }
198                 }
199                 if (fEntryNamesAndValues.length > 0) {
200                         fValue = fEntryNamesAndValues[0][1];
201                         fCombo.setText(fEntryNamesAndValues[0][0]);
202                 }
203         }
204
205         @Override
206         public void setEnabled(boolean enabled, Composite parent) {
207                 super.setEnabled(enabled, parent);
208                 getComboBoxControl(parent).setEnabled(enabled);
209         }
210
211     public void addComboListener(SelectionListener selectionListener) {
212         fCombo.addSelectionListener(selectionListener);
213     }
214     
215     public String getValue() {
216         return fValue;
217     }
218 }