001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.wizards.dialogfields;
011:
012: import org.eclipse.swt.events.ModifyEvent;
013: import org.eclipse.swt.events.ModifyListener;
014: import org.eclipse.swt.events.SelectionEvent;
015: import org.eclipse.swt.events.SelectionListener;
016: import org.eclipse.swt.layout.GridData;
017: import org.eclipse.swt.widgets.Combo;
018: import org.eclipse.swt.widgets.Composite;
019: import org.eclipse.swt.widgets.Control;
020: import org.eclipse.swt.widgets.Label;
021:
022: /**
023: * Dialog field containing a label and a combo control.
024: */
025: public class ComboDialogField extends DialogField {
026:
027: private String fText;
028: private int fSelectionIndex;
029: private String[] fItems;
030: private Combo fComboControl;
031: private ModifyListener fModifyListener;
032: private int fFlags;
033:
034: public ComboDialogField(int flags) {
035: super ();
036: fText = ""; //$NON-NLS-1$
037: fItems = new String[0];
038: fFlags = flags;
039: fSelectionIndex = -1;
040: }
041:
042: // ------- layout helpers
043:
044: /*
045: * @see DialogField#doFillIntoGrid
046: */
047: public Control[] doFillIntoGrid(Composite parent, int nColumns) {
048: assertEnoughColumns(nColumns);
049:
050: Label label = getLabelControl(parent);
051: label.setLayoutData(gridDataForLabel(1));
052: Combo combo = getComboControl(parent);
053: combo.setLayoutData(gridDataForCombo(nColumns - 1));
054:
055: return new Control[] { label, combo };
056: }
057:
058: /*
059: * @see DialogField#getNumberOfControls
060: */
061: public int getNumberOfControls() {
062: return 2;
063: }
064:
065: protected static GridData gridDataForCombo(int span) {
066: GridData gd = new GridData();
067: gd.horizontalAlignment = GridData.FILL;
068: gd.grabExcessHorizontalSpace = false;
069: gd.horizontalSpan = span;
070: return gd;
071: }
072:
073: // ------- focus methods
074:
075: /*
076: * @see DialogField#setFocus
077: */
078: public boolean setFocus() {
079: if (isOkToUse(fComboControl)) {
080: fComboControl.setFocus();
081: }
082: return true;
083: }
084:
085: // ------- ui creation
086:
087: /**
088: * Creates or returns the created combo control.
089: * @param parent The parent composite or <code>null</code> when the widget has
090: * already been created.
091: */
092: public Combo getComboControl(Composite parent) {
093: if (fComboControl == null) {
094: assertCompositeNotNull(parent);
095: fModifyListener = new ModifyListener() {
096: public void modifyText(ModifyEvent e) {
097: doModifyText(e);
098: }
099: };
100: SelectionListener selectionListener = new SelectionListener() {
101: public void widgetSelected(SelectionEvent e) {
102: doSelectionChanged(e);
103: }
104:
105: public void widgetDefaultSelected(SelectionEvent e) {
106: }
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 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 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: public int getSelectionIndex() {
227: return fSelectionIndex;
228: }
229:
230: /**
231: * Sets the text without triggering a dialog-changed event.
232: */
233: public void setTextWithoutUpdate(String text) {
234: fText = text;
235: if (isOkToUse(fComboControl)) {
236: fComboControl.removeModifyListener(fModifyListener);
237: fComboControl.setText(text);
238: fComboControl.addModifyListener(fModifyListener);
239: }
240: }
241:
242: /* (non-Javadoc)
243: * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField#refresh()
244: */
245: public void refresh() {
246: super.refresh();
247: setTextWithoutUpdate(fText);
248: }
249:
250: }
|