65471caf068ca09532ccc5cb71c4925f307ed8f0
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / wizards / dialogfields / StringDialogField.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.wizards.dialogfields;
12
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;
21
22 /**
23  * Dialog field containing a label and a text control.
24  */
25 public class StringDialogField extends DialogField {
26
27         private String fText;
28
29         private Text fTextControl;
30
31         private ModifyListener fModifyListener;
32
33         public StringDialogField() {
34                 super();
35                 fText = ""; //$NON-NLS-1$
36         }
37
38         // ------- layout helpers
39
40         /*
41          * @see DialogField#doFillIntoGrid
42          */
43         public Control[] doFillIntoGrid(Composite parent, int nColumns) {
44                 assertEnoughColumns(nColumns);
45
46                 Label label = getLabelControl(parent);
47                 label.setLayoutData(gridDataForLabel(1));
48                 Text text = getTextControl(parent);
49                 text.setLayoutData(gridDataForText(nColumns - 1));
50
51                 return new Control[] { label, text };
52         }
53
54         /*
55          * @see DialogField#getNumberOfControls
56          */
57         public int getNumberOfControls() {
58                 return 2;
59         }
60
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;
66                 return gd;
67         }
68
69         // ------- focus methods
70
71         /*
72          * @see DialogField#setFocus
73          */
74         public boolean setFocus() {
75                 if (isOkToUse(fTextControl)) {
76                         fTextControl.setFocus();
77                         fTextControl.setSelection(0, fTextControl.getText().length());
78                 }
79                 return true;
80         }
81
82         // ------- ui creation
83
84         /**
85          * Creates or returns the created text control.
86          * 
87          * @param parent
88          *            The parent composite or <code>null</code> when the widget
89          *            has already been created.
90          */
91         public Text getTextControl(Composite parent) {
92                 if (fTextControl == null) {
93                         assertCompositeNotNull(parent);
94                         fModifyListener = new ModifyListener() {
95                                 public void modifyText(ModifyEvent e) {
96                                         doModifyText(e);
97                                 }
98                         };
99
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);
105
106                         fTextControl.setEnabled(isEnabled());
107                 }
108                 return fTextControl;
109         }
110
111         private void doModifyText(ModifyEvent e) {
112                 if (isOkToUse(fTextControl)) {
113                         fText = fTextControl.getText();
114                 }
115                 dialogFieldChanged();
116         }
117
118         // ------ enable / disable management
119
120         /*
121          * @see DialogField#updateEnableState
122          */
123         protected void updateEnableState() {
124                 super.updateEnableState();
125                 if (isOkToUse(fTextControl)) {
126                         fTextControl.setEnabled(isEnabled());
127                 }
128         }
129
130         // ------ text access
131
132         /**
133          * Gets the text. Can not be <code>null</code>
134          */
135         public String getText() {
136                 return fText;
137         }
138
139         /**
140          * Sets the text. Triggers a dialog-changed event.
141          */
142         public void setText(String text) {
143                 fText = text;
144                 if (isOkToUse(fTextControl)) {
145                         fTextControl.setText(text);
146                 } else {
147                         dialogFieldChanged();
148                 }
149         }
150
151         /**
152          * Sets the text without triggering a dialog-changed event.
153          */
154 //      public void setTextWithoutUpdate(String text) {
155 //              fText = text;
156 //              if (isOkToUse(fTextControl)) {
157 //                      fTextControl.removeModifyListener(fModifyListener);
158 //                      fTextControl.setText(text);
159 //                      fTextControl.addModifyListener(fModifyListener);
160 //              }
161 //      }
162
163 }