Changes:
[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         private int fSelectionIndex;
30         private String[] 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[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[] getItems() {
160                 return fItems;
161         }
162         
163         /**
164          * Sets the combo items. Triggers a dialog-changed event.
165          */
166         public void setItems(String[] 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 getText() {
178                 return fText;
179         }
180         
181         /**
182          * Sets the text. Triggers a dialog-changed event.
183          */
184         public void setText(String 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 void selectItem(int index) {
197                 if (isOkToUse(fComboControl)) {
198                         fComboControl.select(index);
199                 } else {
200                         if (index >= 0 && index < fItems.length) {
201                                 fText= fItems[index];
202                                 fSelectionIndex= index; 
203                         }
204                 }
205                 dialogFieldChanged();
206         }
207         
208         public int getSelectionIndex() {
209                 return fSelectionIndex;
210         }
211         
212
213         /**
214          * Sets the text without triggering a dialog-changed event.
215          */
216         public void setTextWithoutUpdate(String text) {
217                 fText= text;
218                 if (isOkToUse(fComboControl)) {
219                         fComboControl.removeModifyListener(fModifyListener);
220                         fComboControl.setText(text);
221                         fComboControl.addModifyListener(fModifyListener);
222                 }
223         }
224         
225 }