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.SWT;
14 import org.eclipse.swt.events.ModifyEvent;
15 import org.eclipse.swt.events.ModifyListener;
16 import org.eclipse.swt.layout.GridData;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.swt.widgets.Label;
20 import org.eclipse.swt.widgets.Text;
23 * Dialog field containing a label and a text control.
25 public class StringDialogField extends DialogField {
29 private Text fTextControl;
31 private ModifyListener fModifyListener;
33 public StringDialogField() {
35 fText = ""; //$NON-NLS-1$
38 // ------- layout helpers
41 * @see DialogField#doFillIntoGrid
43 public Control[] doFillIntoGrid(Composite parent, int nColumns) {
44 assertEnoughColumns(nColumns);
46 Label label = getLabelControl(parent);
47 label.setLayoutData(gridDataForLabel(1));
48 Text text = getTextControl(parent);
49 text.setLayoutData(gridDataForText(nColumns - 1));
51 return new Control[] { label, text };
55 * @see DialogField#getNumberOfControls
57 public int getNumberOfControls() {
61 protected static GridData gridDataForText(int span) {
62 GridData gd = new GridData();
63 gd.horizontalAlignment = GridData.FILL;
64 gd.grabExcessHorizontalSpace = false;
65 gd.horizontalSpan = span;
69 // ------- focus methods
72 * @see DialogField#setFocus
74 public boolean setFocus() {
75 if (isOkToUse(fTextControl)) {
76 fTextControl.setFocus();
77 fTextControl.setSelection(0, fTextControl.getText().length());
82 // ------- ui creation
85 * Creates or returns the created text control.
88 * The parent composite or <code>null</code> when the widget
89 * has already been created.
91 public Text getTextControl(Composite parent) {
92 if (fTextControl == null) {
93 assertCompositeNotNull(parent);
94 fModifyListener = new ModifyListener() {
95 public void modifyText(ModifyEvent e) {
100 fTextControl = new Text(parent, SWT.SINGLE | SWT.BORDER);
101 // moved up due to 1GEUNW2
102 fTextControl.setText(fText);
103 fTextControl.setFont(parent.getFont());
104 fTextControl.addModifyListener(fModifyListener);
106 fTextControl.setEnabled(isEnabled());
111 private void doModifyText(ModifyEvent e) {
112 if (isOkToUse(fTextControl)) {
113 fText = fTextControl.getText();
115 dialogFieldChanged();
118 // ------ enable / disable management
121 * @see DialogField#updateEnableState
123 protected void updateEnableState() {
124 super.updateEnableState();
125 if (isOkToUse(fTextControl)) {
126 fTextControl.setEnabled(isEnabled());
130 // ------ text access
133 * Gets the text. Can not be <code>null</code>
135 public String getText() {
140 * Sets the text. Triggers a dialog-changed event.
142 public void setText(String text) {
144 if (isOkToUse(fTextControl)) {
145 fTextControl.setText(text);
147 dialogFieldChanged();
152 * Sets the text without triggering a dialog-changed event.
154 public void setTextWithoutUpdate(String text) {
156 if (isOkToUse(fTextControl)) {
157 fTextControl.removeModifyListener(fModifyListener);
158 fTextControl.setText(text);
159 fTextControl.addModifyListener(fModifyListener);