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 {
30 private int fSelectionIndex;
32 private String[] fItems;
34 private Combo fComboControl;
36 private ModifyListener fModifyListener;
40 public ComboDialogField(int flags) {
42 fText = ""; //$NON-NLS-1$
43 fItems = new String[0];
48 // ------- layout helpers
51 * @see DialogField#doFillIntoGrid
53 public Control[] doFillIntoGrid(Composite parent, int nColumns) {
54 assertEnoughColumns(nColumns);
56 Label label = getLabelControl(parent);
57 label.setLayoutData(gridDataForLabel(1));
58 Combo combo = getComboControl(parent);
59 combo.setLayoutData(gridDataForCombo(nColumns - 1));
61 return new Control[] { label, combo };
65 * @see DialogField#getNumberOfControls
67 public int getNumberOfControls() {
71 protected static GridData gridDataForCombo(int span) {
72 GridData gd = new GridData();
73 gd.horizontalAlignment = GridData.FILL;
74 gd.grabExcessHorizontalSpace = false;
75 gd.horizontalSpan = span;
79 // ------- focus methods
82 * @see DialogField#setFocus
84 public boolean setFocus() {
85 if (isOkToUse(fComboControl)) {
86 fComboControl.setFocus();
91 // ------- ui creation
94 * Creates or returns the created combo control.
97 * The parent composite or <code>null</code> when the widget
98 * has already been created.
100 public Combo getComboControl(Composite parent) {
101 if (fComboControl == null) {
102 assertCompositeNotNull(parent);
103 fModifyListener = new ModifyListener() {
104 public void modifyText(ModifyEvent e) {
108 SelectionListener selectionListener = new SelectionListener() {
109 public void widgetSelected(SelectionEvent e) {
110 doSelectionChanged(e);
113 public void widgetDefaultSelected(SelectionEvent e) {
117 fComboControl = new Combo(parent, fFlags);
118 // moved up due to 1GEUNW2
119 fComboControl.setItems(fItems);
120 if (fSelectionIndex != -1) {
121 fComboControl.select(fSelectionIndex);
123 fComboControl.setText(fText);
125 fComboControl.setFont(parent.getFont());
126 fComboControl.addModifyListener(fModifyListener);
127 fComboControl.addSelectionListener(selectionListener);
128 fComboControl.setEnabled(isEnabled());
130 return fComboControl;
133 private void doModifyText(ModifyEvent e) {
134 if (isOkToUse(fComboControl)) {
135 fText = fComboControl.getText();
136 fSelectionIndex = fComboControl.getSelectionIndex();
138 dialogFieldChanged();
141 private void doSelectionChanged(SelectionEvent e) {
142 if (isOkToUse(fComboControl)) {
143 fItems = fComboControl.getItems();
144 fText = fComboControl.getText();
145 fSelectionIndex = fComboControl.getSelectionIndex();
147 dialogFieldChanged();
150 // ------ enable / disable management
153 * @see DialogField#updateEnableState
155 protected void updateEnableState() {
156 super.updateEnableState();
157 if (isOkToUse(fComboControl)) {
158 fComboControl.setEnabled(isEnabled());
162 // ------ text access
165 * Gets the combo items.
167 public String[] getItems() {
172 * Sets the combo items. Triggers a dialog-changed event.
174 public void setItems(String[] items) {
176 if (isOkToUse(fComboControl)) {
177 fComboControl.setItems(items);
179 dialogFieldChanged();
185 public String getText() {
190 * Sets the text. Triggers a dialog-changed event.
192 public void setText(String text) {
194 if (isOkToUse(fComboControl)) {
195 fComboControl.setText(text);
197 dialogFieldChanged();
204 public void selectItem(int index) {
205 if (isOkToUse(fComboControl)) {
206 fComboControl.select(index);
208 if (index >= 0 && index < fItems.length) {
209 fText = fItems[index];
210 fSelectionIndex = index;
213 dialogFieldChanged();
216 public int getSelectionIndex() {
217 return fSelectionIndex;
221 * Sets the text without triggering a dialog-changed event.
223 public void setTextWithoutUpdate(String text) {
225 if (isOkToUse(fComboControl)) {
226 fComboControl.removeModifyListener(fModifyListener);
227 fComboControl.setText(text);
228 fComboControl.addModifyListener(fModifyListener);