KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > preferences > 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.team.internal.ui.preferences;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.jface.preference.FieldEditor;
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 a list of items.
25  *
26  * XXX: Note this is a copy from org.eclipse.debug.internal.ui.preferences
27  * This class can be removed once bug 24928 is fixed.
28  *
29  * @since 2.1
30  */

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

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

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

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

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

78     protected void adjustForNumColumns(int numColumns) {
79         Control control= getLabelControl();
80         if (control != null) {
81             ((GridData)control.getLayoutData()).horizontalSpan= numColumns;
82         }
83         ((GridData)fCombo.getLayoutData()).horizontalSpan= numColumns;
84     }
85
86     /*
87      * @see FieldEditor#doFillIntoGrid(Composite, int)
88      */

89     protected void doFillIntoGrid(Composite parent, int numColumns) {
90         Control control= getLabelControl(parent);
91         GridData gd= new GridData();
92         gd.horizontalSpan= numColumns;
93         control.setLayoutData(gd);
94         control= getComboBoxControl(parent);
95         gd= new GridData();
96         gd.horizontalSpan= numColumns;
97         control.setLayoutData(gd);
98     }
99
100     /*
101      * @see FieldEditor#doLoad()
102      */

103     protected void doLoad() {
104         updateComboForValue(getPreferenceStore().getString(getPreferenceName()));
105     }
106
107     /*
108      * @see FieldEditor#doLoadDefault()
109      */

110     protected void doLoadDefault() {
111         updateComboForValue(getPreferenceStore().getDefaultString(getPreferenceName()));
112     }
113
114     /*
115      * @see FieldEditor#doStore()
116      */

117     protected void doStore() {
118         if (fValue == null) {
119             getPreferenceStore().setToDefault(getPreferenceName());
120             return;
121         }
122     
123         getPreferenceStore().setValue(getPreferenceName(), fValue);
124     }
125
126     /*
127      * @see FieldEditor#getNumberOfControls()
128      */

129     public int getNumberOfControls() {
130         return 1;
131     }
132
133     /**
134      * Lazily create and return the Combo control.
135      */

136     public Combo getComboBoxControl(Composite parent) {
137         if (fCombo == null) {
138             fCombo= new Combo(parent, SWT.READ_ONLY);
139             for (int i= 0; i < fEntryNamesAndValues.length; i++) {
140                 fCombo.add(fEntryNamesAndValues[i][0], i);
141             }
142             fCombo.setFont(parent.getFont());
143             fCombo.addSelectionListener(new SelectionAdapter() {
144                 public void widgetSelected(SelectionEvent evt) {
145                     String JavaDoc oldValue= fValue;
146                     String JavaDoc name= fCombo.getText();
147                     fValue= getValueForName(name);
148                     setPresentsDefaultValue(false);
149                     fireValueChanged(VALUE, oldValue, fValue);
150                 }
151             });
152         }
153         return fCombo;
154     }
155     
156     /**
157      * Given the name (label) of an entry, return the corresponding value.
158      */

159     protected String JavaDoc getValueForName(String JavaDoc name) {
160         for (int i= 0; i < fEntryNamesAndValues.length; i++) {
161             String JavaDoc[] entry= fEntryNamesAndValues[i];
162             if (name.equals(entry[0])) {
163                 return entry[1];
164             }
165         }
166         return fEntryNamesAndValues[0][0];
167     }
168     
169     /**
170      * Set the name in the combo widget to match the specified value.
171      */

172     protected void updateComboForValue(String JavaDoc value) {
173         fValue= value;
174         for (int i= 0; i < fEntryNamesAndValues.length; i++) {
175             if (value.equals(fEntryNamesAndValues[i][1])) {
176                 fCombo.setText(fEntryNamesAndValues[i][0]);
177                 return;
178             }
179         }
180         if (fEntryNamesAndValues.length > 0) {
181             fValue= fEntryNamesAndValues[0][1];
182             fCombo.setText(fEntryNamesAndValues[0][0]);
183         }
184     }
185 }
186
Popular Tags