KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > dialogfields > ComboDialogField


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.ui.wizards.dialogfields;
12
13 import org.eclipse.swt.events.ModifyEvent;
14 import org.eclipse.swt.events.ModifyListener;
15 import org.eclipse.swt.events.SelectionEvent;
16 import org.eclipse.swt.events.SelectionListener;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.widgets.Combo;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.Label;
22
23 /**
24  * Dialog field containing a label and a combo control.
25  */

26 public class ComboDialogField extends DialogField {
27         
28     private String JavaDoc fText;
29     private int fSelectionIndex;
30     private String JavaDoc[] fItems;
31     private Combo fComboControl;
32     private ModifyListener fModifyListener;
33     private int fFlags;
34     
35     public ComboDialogField(int flags) {
36         super();
37         fText= ""; //$NON-NLS-1$
38
fItems= new String JavaDoc[0];
39         fFlags= flags;
40         fSelectionIndex= -1;
41     }
42             
43     // ------- layout helpers
44

45     /*
46      * @see DialogField#doFillIntoGrid
47      */

48     public Control[] doFillIntoGrid(Composite parent, int nColumns) {
49         assertEnoughColumns(nColumns);
50         
51         Label label= getLabelControl(parent);
52         label.setLayoutData(gridDataForLabel(1));
53         Combo combo= getComboControl(parent);
54         combo.setLayoutData(gridDataForCombo(nColumns - 1));
55         
56         return new Control[] { label, combo };
57     }
58
59     /*
60      * @see DialogField#getNumberOfControls
61      */

62     public int getNumberOfControls() {
63         return 2;
64     }
65     
66     protected static GridData gridDataForCombo(int span) {
67         GridData gd= new GridData();
68         gd.horizontalAlignment= GridData.FILL;
69         gd.grabExcessHorizontalSpace= false;
70         gd.horizontalSpan= span;
71         return gd;
72     }
73     
74     // ------- focus methods
75

76     /*
77      * @see DialogField#setFocus
78      */

79     public boolean setFocus() {
80         if (isOkToUse(fComboControl)) {
81             fComboControl.setFocus();
82         }
83         return true;
84     }
85         
86     // ------- ui creation
87

88     /**
89      * Creates or returns the created combo control.
90      * @param parent The parent composite or <code>null</code> when the widget has
91      * already been created.
92      */

93     public Combo getComboControl(Composite parent) {
94         if (fComboControl == null) {
95             assertCompositeNotNull(parent);
96             fModifyListener= new ModifyListener() {
97                 public void modifyText(ModifyEvent e) {
98                     doModifyText(e);
99                 }
100             };
101             SelectionListener selectionListener= new SelectionListener() {
102                 public void widgetSelected(SelectionEvent e) {
103                     doSelectionChanged(e);
104                 }
105                 
106                 public void widgetDefaultSelected(SelectionEvent e) { }
107             };
108             
109             fComboControl= new Combo(parent, fFlags);
110             // moved up due to 1GEUNW2
111
fComboControl.setItems(fItems);
112             if (fSelectionIndex != -1) {
113                 fComboControl.select(fSelectionIndex);
114             } else {
115                 fComboControl.setText(fText);
116             }
117             fComboControl.setFont(parent.getFont());
118             fComboControl.addModifyListener(fModifyListener);
119             fComboControl.addSelectionListener(selectionListener);
120             fComboControl.setEnabled(isEnabled());
121         }
122         return fComboControl;
123     }
124     
125     private void doModifyText(ModifyEvent e) {
126         if (isOkToUse(fComboControl)) {
127             fText= fComboControl.getText();
128             fSelectionIndex= fComboControl.getSelectionIndex();
129         }
130         dialogFieldChanged();
131     }
132     
133     private void doSelectionChanged(SelectionEvent e) {
134         if (isOkToUse(fComboControl)) {
135             fItems= fComboControl.getItems();
136             fText= fComboControl.getText();
137             fSelectionIndex= fComboControl.getSelectionIndex();
138         }
139         dialogFieldChanged();
140     }
141     
142     // ------ enable / disable management
143

144     /*
145      * @see DialogField#updateEnableState
146      */

147     protected void updateEnableState() {
148         super.updateEnableState();
149         if (isOkToUse(fComboControl)) {
150             fComboControl.setEnabled(isEnabled());
151         }
152     }
153         
154     // ------ text access
155

156     /**
157      * Gets the combo items.
158      */

159     public String JavaDoc[] getItems() {
160         return fItems;
161     }
162     
163     /**
164      * Sets the combo items. Triggers a dialog-changed event.
165      */

166     public void setItems(String JavaDoc[] items) {
167         fItems= items;
168         if (isOkToUse(fComboControl)) {
169             fComboControl.setItems(items);
170         }
171         dialogFieldChanged();
172     }
173     
174     /**
175      * Gets the text.
176      */

177     public String JavaDoc getText() {
178         return fText;
179     }
180     
181     /**
182      * Sets the text. Triggers a dialog-changed event.
183      */

184     public void setText(String JavaDoc text) {
185         fText= text;
186         if (isOkToUse(fComboControl)) {
187             fComboControl.setText(text);
188         } else {
189             dialogFieldChanged();
190         }
191     }
192
193     /**
194      * Selects an item.
195      */

196     public boolean selectItem(int index) {
197         boolean success= false;
198         if (isOkToUse(fComboControl)) {
199             fComboControl.select(index);
200             success= fComboControl.getSelectionIndex() == index;
201         } else {
202             if (index >= 0 && index < fItems.length) {
203                 fText= fItems[index];
204                 fSelectionIndex= index;
205                 success= true;
206             }
207         }
208         if (success) {
209             dialogFieldChanged();
210         }
211         return success;
212     }
213     
214     /**
215      * Selects an item.
216      */

217     public boolean selectItem(String JavaDoc name) {
218         for (int i= 0; i < fItems.length; i++) {
219             if (fItems[i].equals(name)) {
220                 return selectItem(i);
221             }
222         }
223         return false;
224     }
225     
226     
227     public int getSelectionIndex() {
228         return fSelectionIndex;
229     }
230     
231
232     /**
233      * Sets the text without triggering a dialog-changed event.
234      */

235     public void setTextWithoutUpdate(String JavaDoc text) {
236         fText= text;
237         if (isOkToUse(fComboControl)) {
238             fComboControl.removeModifyListener(fModifyListener);
239             fComboControl.setText(text);
240             fComboControl.addModifyListener(fModifyListener);
241         }
242     }
243     
244     /* (non-Javadoc)
245      * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField#refresh()
246      */

247     public void refresh() {
248         super.refresh();
249         setTextWithoutUpdate(fText);
250     }
251     
252 }
253
Popular Tags