]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/controls/DynamicComboFieldEditor.java
Elimination of compiler warnings.
[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         @SuppressWarnings("unused")
70         private boolean checkArray(String[][] table) {
71                 if (table == null) {
72                         return false;
73                 }
74                 for (int i = 0; i < table.length; i++) {
75                         String[] array = table[i];
76                         if (array == null || array.length != 2) {
77                                 return false;
78                         }
79                 }
80                 return true;
81         }
82
83         @Override
84         protected void adjustForNumColumns(int numColumns) {
85                 if (numColumns > 1) {
86                         Control control = getLabelControl();
87                         int left = numColumns;
88                         if (control != null) {
89                                 ((GridData)control.getLayoutData()).horizontalSpan = 1;
90                                 left = left - 1;
91                         }
92                         ((GridData)fCombo.getLayoutData()).horizontalSpan = left;
93                 } else {
94                         Control control = getLabelControl();
95                         if (control != null) {
96                                 ((GridData)control.getLayoutData()).horizontalSpan = 1;
97                         }
98                         ((GridData)fCombo.getLayoutData()).horizontalSpan = 1;
99                 }
100         }
101
102         @Override
103         protected void doFillIntoGrid(Composite parent, int numColumns) {
104                 int comboC = 1;
105                 if (numColumns > 1) {
106                         comboC = numColumns - 1;
107                 }
108                 Control control = getLabelControl(parent);
109                 GridData gd = new GridData();
110                 gd.horizontalSpan = 1;
111                 control.setLayoutData(gd);
112                 control = getComboBoxControl(parent);
113                 gd = new GridData();
114                 gd.horizontalSpan = comboC;
115                 gd.horizontalAlignment = GridData.FILL;
116                 control.setLayoutData(gd);
117                 control.setFont(parent.getFont());
118         }
119
120         @Override
121         protected void doLoad() {
122                 updateComboForValue(getPreferenceStore().getString(getPreferenceName()));
123         }
124
125         @Override
126         protected void doLoadDefault() {
127                 updateComboForValue(getPreferenceStore().getDefaultString(getPreferenceName()));
128         }
129
130         @Override
131         protected void doStore() {
132                 if (fValue == null) {
133                         getPreferenceStore().setToDefault(getPreferenceName());
134                         return;
135                 }
136                 getPreferenceStore().setValue(getPreferenceName(), fValue);
137         }
138
139         @Override
140         public int getNumberOfControls() {
141                 return 2;
142         }
143
144         /*
145          * Lazily create and return the Combo control.
146          */
147         private Combo getComboBoxControl(Composite parent) {
148                 if (fCombo == null) {
149             fCombo = new Combo(parent, SWT.READ_ONLY);
150             fCombo.setFont(parent.getFont());
151
152             fCombo.addSelectionListener(new SelectionAdapter() {
153                 @Override
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);
160                 }
161             });
162                 }
163                 updateComboBoxControl();
164                 return fCombo;
165         }
166         
167         private void updateComboBoxControl() {
168             fCombo.removeAll();
169             if (fEntryNamesAndValues != null) {
170             for (int i = 0; i < fEntryNamesAndValues.length; i++) {
171                 fCombo.add(fEntryNamesAndValues[i][0], i);
172             }
173             }
174         }
175
176         /*
177          * Given the name (label) of an entry, return the corresponding value.
178          */
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])) {
183                                 return entry[1];
184                         }
185                 }
186                 return fEntryNamesAndValues[0][0];
187         }
188
189         /*
190          * Set the name in the combo widget to match the specified value.
191          */
192         private void updateComboForValue(String value) {
193                 fValue = value;
194                 for (int i = 0; i < fEntryNamesAndValues.length; i++) {
195                         if (value.equals(fEntryNamesAndValues[i][1])) {
196                                 fCombo.setText(fEntryNamesAndValues[i][0]);
197                                 return;
198                         }
199                 }
200                 if (fEntryNamesAndValues.length > 0) {
201                         fValue = fEntryNamesAndValues[0][1];
202                         fCombo.setText(fEntryNamesAndValues[0][0]);
203                 }
204         }
205
206         @Override
207         public void setEnabled(boolean enabled, Composite parent) {
208                 super.setEnabled(enabled, parent);
209                 getComboBoxControl(parent).setEnabled(enabled);
210         }
211
212     public void addComboListener(SelectionListener selectionListener) {
213         fCombo.addSelectionListener(selectionListener);
214     }
215     
216     public String getValue() {
217         return fValue;
218     }
219 }