KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > ui > util > 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.search.internal.ui.util;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.SelectionAdapter;
15 import org.eclipse.swt.events.SelectionEvent;
16 import org.eclipse.swt.layout.GridData;
17 import org.eclipse.swt.widgets.Combo;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Control;
20
21 import org.eclipse.jface.preference.FieldEditor;
22 import org.eclipse.jface.util.Assert;
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  * XXX: Note this is a copy from org.eclipse.debug.internal.ui.preferences
28  * This class can be removed once bug 24928 is fixed.
29  *
30  * @since 2.1
31  */

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

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

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

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

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

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

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

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

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

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

131     public int getNumberOfControls() {
132         return 2;
133     }
134
135     /*
136      * Lazily create and return the Combo control.
137      */

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

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

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