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 {
28 private Text fTextControl;
29 private ModifyListener fModifyListener;
31 public StringDialogField() {
33 fText= ""; //$NON-NLS-1$
36 // ------- layout helpers
39 * @see DialogField#doFillIntoGrid
41 public Control[] doFillIntoGrid(Composite parent, int nColumns) {
42 assertEnoughColumns(nColumns);
44 Label label= getLabelControl(parent);
45 label.setLayoutData(gridDataForLabel(1));
46 Text text= getTextControl(parent);
47 text.setLayoutData(gridDataForText(nColumns - 1));
49 return new Control[] { label, text };
53 * @see DialogField#getNumberOfControls
55 public int getNumberOfControls() {
59 protected static GridData gridDataForText(int span) {
60 GridData gd= new GridData();
61 gd.horizontalAlignment= GridData.FILL;
62 gd.grabExcessHorizontalSpace= false;
63 gd.horizontalSpan= span;
67 // ------- focus methods
70 * @see DialogField#setFocus
72 public boolean setFocus() {
73 if (isOkToUse(fTextControl)) {
74 fTextControl.setFocus();
75 fTextControl.setSelection(0, fTextControl.getText().length());
80 // ------- ui creation
83 * Creates or returns the created text control.
84 * @param parent The parent composite or <code>null</code> when the widget has
85 * already been created.
87 public Text getTextControl(Composite parent) {
88 if (fTextControl == null) {
89 assertCompositeNotNull(parent);
90 fModifyListener= new ModifyListener() {
91 public void modifyText(ModifyEvent e) {
96 fTextControl= new Text(parent, SWT.SINGLE | SWT.BORDER);
97 // moved up due to 1GEUNW2
98 fTextControl.setText(fText);
99 fTextControl.setFont(parent.getFont());
100 fTextControl.addModifyListener(fModifyListener);
102 fTextControl.setEnabled(isEnabled());
107 private void doModifyText(ModifyEvent e) {
108 if (isOkToUse(fTextControl)) {
109 fText= fTextControl.getText();
111 dialogFieldChanged();
114 // ------ enable / disable management
117 * @see DialogField#updateEnableState
119 protected void updateEnableState() {
120 super.updateEnableState();
121 if (isOkToUse(fTextControl)) {
122 fTextControl.setEnabled(isEnabled());
126 // ------ text access
129 * Gets the text. Can not be <code>null</code>
131 public String getText() {
136 * Sets the text. Triggers a dialog-changed event.
138 public void setText(String text) {
140 if (isOkToUse(fTextControl)) {
141 fTextControl.setText(text);
143 dialogFieldChanged();
148 * Sets the text without triggering a dialog-changed event.
150 public void setTextWithoutUpdate(String text) {
152 if (isOkToUse(fTextControl)) {
153 fTextControl.removeModifyListener(fModifyListener);
154 fTextControl.setText(text);
155 fTextControl.addModifyListener(fModifyListener);