KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > preference > ComboFieldEditor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.jface.preference;
12
13
14 import org.eclipse.core.runtime.Assert;
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.SelectionAdapter;
17 import org.eclipse.swt.events.SelectionEvent;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.widgets.Combo;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22
23 /**
24  * A field editor for a combo box that allows the drop-down selection of one of
25  * a list of items.
26  *
27  * @since 3.3
28  */

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

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

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

45     private String JavaDoc[][] fEntryNamesAndValues;
46
47     /**
48      * Create the combo box field editor.
49      *
50      * @param name the name of the preference this field editor works on
51      * @param labelText the label text of the field editor
52      * @param entryNamesAndValues the names (labels) and underlying values to populate the combo widget. These should be
53      * arranged as: { {name1, value1}, {name2, value2}, ...}
54      * @param parent the parent composite
55      */

56     public ComboFieldEditor(String JavaDoc name, String JavaDoc labelText, String JavaDoc[][] entryNamesAndValues, Composite parent) {
57         init(name, labelText);
58         Assert.isTrue(checkArray(entryNamesAndValues));
59         fEntryNamesAndValues = entryNamesAndValues;
60         createControl(parent);
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 JavaDoc[][] table) {
70         if (table == null) {
71             return false;
72         }
73         for (int i = 0; i < table.length; i++) {
74             String JavaDoc[] array = table[i];
75             if (array == null || array.length != 2) {
76                 return false;
77             }
78         }
79         return true;
80     }
81
82     /* (non-Javadoc)
83      * @see org.eclipse.jface.preference.FieldEditor#adjustForNumColumns(int)
84      */

85     protected void adjustForNumColumns(int numColumns) {
86         if (numColumns > 1) {
87             Control control = getLabelControl();
88             int left = numColumns;
89             if (control != null) {
90                 ((GridData)control.getLayoutData()).horizontalSpan = 1;
91                 left = left - 1;
92             }
93             ((GridData)fCombo.getLayoutData()).horizontalSpan = left;
94         } else {
95             Control control = getLabelControl();
96             if (control != null) {
97                 ((GridData)control.getLayoutData()).horizontalSpan = 1;
98             }
99             ((GridData)fCombo.getLayoutData()).horizontalSpan = 1;
100         }
101     }
102
103     /* (non-Javadoc)
104      * @see org.eclipse.jface.preference.FieldEditor#doFillIntoGrid(org.eclipse.swt.widgets.Composite, int)
105      */

106     protected void doFillIntoGrid(Composite parent, int numColumns) {
107         int comboC = 1;
108         if (numColumns > 1) {
109             comboC = numColumns - 1;
110         }
111         Control control = getLabelControl(parent);
112         GridData gd = new GridData();
113         gd.horizontalSpan = 1;
114         control.setLayoutData(gd);
115         control = getComboBoxControl(parent);
116         gd = new GridData();
117         gd.horizontalSpan = comboC;
118         gd.horizontalAlignment = GridData.FILL;
119         control.setLayoutData(gd);
120         control.setFont(parent.getFont());
121     }
122
123     /* (non-Javadoc)
124      * @see org.eclipse.jface.preference.FieldEditor#doLoad()
125      */

126     protected void doLoad() {
127         updateComboForValue(getPreferenceStore().getString(getPreferenceName()));
128     }
129
130     /* (non-Javadoc)
131      * @see org.eclipse.jface.preference.FieldEditor#doLoadDefault()
132      */

133     protected void doLoadDefault() {
134         updateComboForValue(getPreferenceStore().getDefaultString(getPreferenceName()));
135     }
136
137     /* (non-Javadoc)
138      * @see org.eclipse.jface.preference.FieldEditor#doStore()
139      */

140     protected void doStore() {
141         if (fValue == null) {
142             getPreferenceStore().setToDefault(getPreferenceName());
143             return;
144         }
145         getPreferenceStore().setValue(getPreferenceName(), fValue);
146     }
147
148     /* (non-Javadoc)
149      * @see org.eclipse.jface.preference.FieldEditor#getNumberOfControls()
150      */

151     public int getNumberOfControls() {
152         return 2;
153     }
154
155     /*
156      * Lazily create and return the Combo control.
157      */

158     private Combo getComboBoxControl(Composite parent) {
159         if (fCombo == null) {
160             fCombo = new Combo(parent, SWT.READ_ONLY);
161             fCombo.setFont(parent.getFont());
162             for (int i = 0; i < fEntryNamesAndValues.length; i++) {
163                 fCombo.add(fEntryNamesAndValues[i][0], i);
164             }
165             
166             fCombo.addSelectionListener(new SelectionAdapter() {
167                 public void widgetSelected(SelectionEvent evt) {
168                     String JavaDoc oldValue = fValue;
169                     String JavaDoc name = fCombo.getText();
170                     fValue = getValueForName(name);
171                     setPresentsDefaultValue(false);
172                     fireValueChanged(VALUE, oldValue, fValue);
173                 }
174             });
175         }
176         return fCombo;
177     }
178     
179     /*
180      * Given the name (label) of an entry, return the corresponding value.
181      */

182     private String JavaDoc getValueForName(String JavaDoc name) {
183         for (int i = 0; i < fEntryNamesAndValues.length; i++) {
184             String JavaDoc[] entry = fEntryNamesAndValues[i];
185             if (name.equals(entry[0])) {
186                 return entry[1];
187             }
188         }
189         return fEntryNamesAndValues[0][0];
190     }
191     
192     /*
193      * Set the name in the combo widget to match the specified value.
194      */

195     private void updateComboForValue(String JavaDoc value) {
196         fValue = value;
197         for (int i = 0; i < fEntryNamesAndValues.length; i++) {
198             if (value.equals(fEntryNamesAndValues[i][1])) {
199                 fCombo.setText(fEntryNamesAndValues[i][0]);
200                 return;
201             }
202         }
203         if (fEntryNamesAndValues.length > 0) {
204             fValue = fEntryNamesAndValues[0][1];
205             fCombo.setText(fEntryNamesAndValues[0][0]);
206         }
207     }
208 }
209
Popular Tags