KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > launcher > ComboFieldEditor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.ui.launcher;
12
13
14 import org.eclipse.jface.preference.FieldEditor;
15 import org.eclipse.jface.util.Assert;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.widgets.Combo;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23
24 /**
25  * A field editor for a combo box that allows the drop-down selection of one of a list of items.
26  */

27 public class ComboFieldEditor extends FieldEditor {
28
29     /**
30      * The <code>Combo</code> widget.
31      */

32     private Combo fCombo;
33     
34     /**
35      * The value (not the name) of the currently selected item in the Combo widget.
36      */

37     private String JavaDoc fValue;
38     
39     /**
40      * The names (labels) and underlying values to populate the combo widget. These should be
41      * arranged as: { {name1, value1}, {name2, value2}, ...}
42      */

43     private String JavaDoc[][] fEntryNamesAndValues;
44
45     public ComboFieldEditor(String JavaDoc name, String JavaDoc labelText, String JavaDoc[][] entryNamesAndValues, Composite parent) {
46         init(name, labelText);
47         Assert.isTrue(checkArray(entryNamesAndValues));
48         fEntryNamesAndValues = entryNamesAndValues;
49         createControl(parent);
50     }
51
52     /**
53      * Checks whether given <code>String[][]</code> is of "type"
54      * <code>String[][2]</code>.
55      *
56      * @return <code>true</code> if it is ok, and <code>false</code> otherwise
57      */

58     private boolean checkArray(String JavaDoc[][] table) {
59         if (table == null) {
60             return false;
61         }
62         for (int i = 0; i < table.length; i++) {
63             String JavaDoc[] array = table[i];
64             if (array == null || array.length != 2) {
65                 return false;
66             }
67         }
68         return true;
69     }
70
71     /**
72      * @see FieldEditor#adjustForNumColumns(int)
73      */

74     protected void adjustForNumColumns(int numColumns) {
75         if (numColumns > 1) {
76             Control control = getLabelControl();
77             int left = numColumns;
78             if (control != null) {
79                 ((GridData)control.getLayoutData()).horizontalSpan = 1;
80                 left = left - 1;
81             }
82             ((GridData)fCombo.getLayoutData()).horizontalSpan = left;
83         } else {
84             Control control = getLabelControl();
85             if (control != null) {
86                 ((GridData)control.getLayoutData()).horizontalSpan = 1;
87             }
88             ((GridData)fCombo.getLayoutData()).horizontalSpan = 1;
89         }
90     }
91
92     /**
93      * @see FieldEditor#doFillIntoGrid(Composite, int)
94      */

95     protected void doFillIntoGrid(Composite parent, int numColumns) {
96         int comboC = 1;
97         if (numColumns > 1) {
98             comboC = numColumns - 1;
99         }
100         Control control = getLabelControl(parent);
101         GridData gd = new GridData();
102         gd.horizontalSpan = 1;
103         control.setLayoutData(gd);
104         control = getComboBoxControl(parent);
105         gd = new GridData();
106         gd.horizontalSpan = comboC;
107         gd.horizontalAlignment = GridData.FILL;
108         control.setLayoutData(gd);
109         control.setFont(parent.getFont());
110     }
111
112     /**
113      * @see FieldEditor#doLoad()
114      */

115     protected void doLoad() {
116         updateComboForValue(getPreferenceStore().getString(getPreferenceName()));
117     }
118
119     /**
120      * @see FieldEditor#doLoadDefault()
121      */

122     protected void doLoadDefault() {
123         updateComboForValue(getPreferenceStore().getDefaultString(getPreferenceName()));
124     }
125
126     /**
127      * @see FieldEditor#doStore()
128      */

129     protected void doStore() {
130         if (fValue == null) {
131             getPreferenceStore().setToDefault(getPreferenceName());
132             return;
133         }
134     
135         getPreferenceStore().setValue(getPreferenceName(), fValue);
136     }
137
138     /**
139      * @see FieldEditor#getNumberOfControls()
140      */

141     public int getNumberOfControls() {
142         return 1;
143     }
144
145     /**
146      * Lazily create and return the Combo control.
147      */

148     public Combo getComboBoxControl(Composite parent) {
149         if (fCombo == null) {
150             fCombo = new Combo(parent, SWT.READ_ONLY);
151             fCombo.setFont(parent.getFont());
152             for (int i = 0; i < fEntryNamesAndValues.length; i++) {
153                 fCombo.add(fEntryNamesAndValues[i][0], i);
154             }
155             
156             fCombo.addSelectionListener(new SelectionAdapter() {
157                 public void widgetSelected(SelectionEvent evt) {
158                     String JavaDoc oldValue = fValue;
159                     String JavaDoc name = fCombo.getText();
160                     fValue = getValueForName(name);
161                     setPresentsDefaultValue(false);
162                     fireValueChanged(VALUE, oldValue, fValue);
163                 }
164             });
165         }
166         return fCombo;
167     }
168     
169     /**
170      * Given the name (label) of an entry, return the corresponding value.
171      */

172     protected String JavaDoc getValueForName(String JavaDoc name) {
173         for (int i = 0; i < fEntryNamesAndValues.length; i++) {
174             String JavaDoc[] entry = fEntryNamesAndValues[i];
175             if (name.equals(entry[0])) {
176                 return entry[1];
177             }
178         }
179         return fEntryNamesAndValues[0][0];
180     }
181     
182     /**
183      * Set the name in the combo widget to match the specified value.
184      */

185     protected void updateComboForValue(String JavaDoc value) {
186         fValue = value;
187         for (int i = 0; i < fEntryNamesAndValues.length; i++) {
188             if (value.equals(fEntryNamesAndValues[i][1])) {
189                 fCombo.setText(fEntryNamesAndValues[i][0]);
190                 return;
191             }
192         }
193         if (fEntryNamesAndValues.length > 0) {
194             fValue = fEntryNamesAndValues[0][1];
195         }
196     }
197 }
198
Popular Tags