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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.wizards.dialogfields;
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;
24 * Dialog field containing a label and a combo control.
26 public class ComboDialogField extends DialogField {
29 private int fSelectionIndex;
30 private String[] fItems;
31 private Combo fComboControl;
32 private ModifyListener fModifyListener;
35 public ComboDialogField(int flags) {
37 fText= ""; //$NON-NLS-1$
38 fItems= new String[0];
43 // ------- layout helpers
46 * @see DialogField#doFillIntoGrid
48 public Control[] doFillIntoGrid(Composite parent, int nColumns) {
49 assertEnoughColumns(nColumns);
51 Label label= getLabelControl(parent);
52 label.setLayoutData(gridDataForLabel(1));
53 Combo combo= getComboControl(parent);
54 combo.setLayoutData(gridDataForCombo(nColumns - 1));
56 return new Control[] { label, combo };
60 * @see DialogField#getNumberOfControls
62 public int getNumberOfControls() {
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;
74 // ------- focus methods
77 * @see DialogField#setFocus
79 public boolean setFocus() {
80 if (isOkToUse(fComboControl)) {
81 fComboControl.setFocus();
86 // ------- ui creation
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.
93 public Combo getComboControl(Composite parent) {
94 if (fComboControl == null) {
95 assertCompositeNotNull(parent);
96 fModifyListener= new ModifyListener() {
97 public void modifyText(ModifyEvent e) {
101 SelectionListener selectionListener= new SelectionListener() {
102 public void widgetSelected(SelectionEvent e) {
103 doSelectionChanged(e);
106 public void widgetDefaultSelected(SelectionEvent e) { };
109 fComboControl= new Combo(parent, fFlags);
110 // moved up due to 1GEUNW2
111 fComboControl.setItems(fItems);
112 if (fSelectionIndex != -1) {
113 fComboControl.select(fSelectionIndex);
115 fComboControl.setText(fText);
117 fComboControl.setFont(parent.getFont());
118 fComboControl.addModifyListener(fModifyListener);
119 fComboControl.addSelectionListener(selectionListener);
120 fComboControl.setEnabled(isEnabled());
122 return fComboControl;
125 private void doModifyText(ModifyEvent e) {
126 if (isOkToUse(fComboControl)) {
127 fText= fComboControl.getText();
128 fSelectionIndex= fComboControl.getSelectionIndex();
130 dialogFieldChanged();
133 private void doSelectionChanged(SelectionEvent e) {
134 if (isOkToUse(fComboControl)) {
135 fItems= fComboControl.getItems();
136 fText= fComboControl.getText();
137 fSelectionIndex= fComboControl.getSelectionIndex();
139 dialogFieldChanged();
142 // ------ enable / disable management
145 * @see DialogField#updateEnableState
147 protected void updateEnableState() {
148 super.updateEnableState();
149 if (isOkToUse(fComboControl)) {
150 fComboControl.setEnabled(isEnabled());
154 // ------ text access
157 * Gets the combo items.
159 public String[] getItems() {
164 * Sets the combo items. Triggers a dialog-changed event.
166 public void setItems(String[] items) {
168 if (isOkToUse(fComboControl)) {
169 fComboControl.setItems(items);
171 dialogFieldChanged();
177 public String getText() {
182 * Sets the text. Triggers a dialog-changed event.
184 public void setText(String text) {
186 if (isOkToUse(fComboControl)) {
187 fComboControl.setText(text);
189 dialogFieldChanged();
196 public void selectItem(int index) {
197 if (isOkToUse(fComboControl)) {
198 fComboControl.select(index);
200 if (index >= 0 && index < fItems.length) {
201 fText= fItems[index];
202 fSelectionIndex= index;
205 dialogFieldChanged();
208 public int getSelectionIndex() {
209 return fSelectionIndex;
214 * Sets the text without triggering a dialog-changed event.
216 public void setTextWithoutUpdate(String text) {
218 if (isOkToUse(fComboControl)) {
219 fComboControl.removeModifyListener(fModifyListener);
220 fComboControl.setText(text);
221 fComboControl.addModifyListener(fModifyListener);