1) Moved net.sourceforge.phpeclipse.ui\src\net\sourceforge\phpdt back to net.sourcefo...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / wizards / dialogfields / ComboDialogField.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.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 fText;
29
30         private int fSelectionIndex;
31
32         private String[] fItems;
33
34         private Combo fComboControl;
35
36         private ModifyListener fModifyListener;
37
38         private int fFlags;
39
40         public ComboDialogField(int flags) {
41                 super();
42                 fText = ""; //$NON-NLS-1$
43                 fItems = new String[0];
44                 fFlags = flags;
45                 fSelectionIndex = -1;
46         }
47
48         // ------- layout helpers
49
50         /*
51          * @see DialogField#doFillIntoGrid
52          */
53         public Control[] doFillIntoGrid(Composite parent, int nColumns) {
54                 assertEnoughColumns(nColumns);
55
56                 Label label = getLabelControl(parent);
57                 label.setLayoutData(gridDataForLabel(1));
58                 Combo combo = getComboControl(parent);
59                 combo.setLayoutData(gridDataForCombo(nColumns - 1));
60
61                 return new Control[] { label, combo };
62         }
63
64         /*
65          * @see DialogField#getNumberOfControls
66          */
67         public int getNumberOfControls() {
68                 return 2;
69         }
70
71         private static GridData gridDataForCombo(int span) {
72                 GridData gd = new GridData();
73                 gd.horizontalAlignment = GridData.FILL;
74                 gd.grabExcessHorizontalSpace = false;
75                 gd.horizontalSpan = span;
76                 return gd;
77         }
78
79         // ------- focus methods
80
81         /*
82          * @see DialogField#setFocus
83          */
84         public boolean setFocus() {
85                 if (isOkToUse(fComboControl)) {
86                         fComboControl.setFocus();
87                 }
88                 return true;
89         }
90
91         // ------- ui creation
92
93         /**
94          * Creates or returns the created combo control.
95          * 
96          * @param parent
97          *            The parent composite or <code>null</code> when the widget
98          *            has already been created.
99          */
100         public Combo getComboControl(Composite parent) {
101                 if (fComboControl == null) {
102                         assertCompositeNotNull(parent);
103                         fModifyListener = new ModifyListener() {
104                                 public void modifyText(ModifyEvent e) {
105                                         doModifyText(e);
106                                 }
107                         };
108                         SelectionListener selectionListener = new SelectionListener() {
109                                 public void widgetSelected(SelectionEvent e) {
110                                         doSelectionChanged(e);
111                                 }
112
113                                 public void widgetDefaultSelected(SelectionEvent e) {
114                                 };
115                         };
116
117                         fComboControl = new Combo(parent, fFlags);
118                         // moved up due to 1GEUNW2
119                         fComboControl.setItems(fItems);
120                         if (fSelectionIndex != -1) {
121                                 fComboControl.select(fSelectionIndex);
122                         } else {
123                                 fComboControl.setText(fText);
124                         }
125                         fComboControl.setFont(parent.getFont());
126                         fComboControl.addModifyListener(fModifyListener);
127                         fComboControl.addSelectionListener(selectionListener);
128                         fComboControl.setEnabled(isEnabled());
129                 }
130                 return fComboControl;
131         }
132
133         private void doModifyText(ModifyEvent e) {
134                 if (isOkToUse(fComboControl)) {
135                         fText = fComboControl.getText();
136                         fSelectionIndex = fComboControl.getSelectionIndex();
137                 }
138                 dialogFieldChanged();
139         }
140
141         private void doSelectionChanged(SelectionEvent e) {
142                 if (isOkToUse(fComboControl)) {
143                         fItems = fComboControl.getItems();
144                         fText = fComboControl.getText();
145                         fSelectionIndex = fComboControl.getSelectionIndex();
146                 }
147                 dialogFieldChanged();
148         }
149
150         // ------ enable / disable management
151
152         /*
153          * @see DialogField#updateEnableState
154          */
155         protected void updateEnableState() {
156                 super.updateEnableState();
157                 if (isOkToUse(fComboControl)) {
158                         fComboControl.setEnabled(isEnabled());
159                 }
160         }
161
162         // ------ text access
163
164         /**
165          * Gets the combo items.
166          */
167         public String[] getItems() {
168                 return fItems;
169         }
170
171         /**
172          * Sets the combo items. Triggers a dialog-changed event.
173          */
174         public void setItems(String[] items) {
175                 fItems = items;
176                 if (isOkToUse(fComboControl)) {
177                         fComboControl.setItems(items);
178                 }
179                 dialogFieldChanged();
180         }
181
182         /**
183          * Gets the text.
184          */
185         public String getText() {
186                 return fText;
187         }
188
189         /**
190          * Sets the text. Triggers a dialog-changed event.
191          */
192         public void setText(String text) {
193                 fText = text;
194                 if (isOkToUse(fComboControl)) {
195                         fComboControl.setText(text);
196                 } else {
197                         dialogFieldChanged();
198                 }
199         }
200
201         /**
202          * Selects an item.
203          */
204         public void selectItem(int index) {
205                 if (isOkToUse(fComboControl)) {
206                         fComboControl.select(index);
207                 } else {
208                         if (index >= 0 && index < fItems.length) {
209                                 fText = fItems[index];
210                                 fSelectionIndex = index;
211                         }
212                 }
213                 dialogFieldChanged();
214         }
215
216         public int getSelectionIndex() {
217                 return fSelectionIndex;
218         }
219
220         /**
221          * Sets the text without triggering a dialog-changed event.
222          */
223 //      public void setTextWithoutUpdate(String text) {
224 //              fText = text;
225 //              if (isOkToUse(fComboControl)) {
226 //                      fComboControl.removeModifyListener(fModifyListener);
227 //                      fComboControl.setText(text);
228 //                      fComboControl.addModifyListener(fModifyListener);
229 //              }
230 //      }
231
232 }