Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / 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         private Text fTextControl;
29         private ModifyListener fModifyListener;
30         
31         public StringDialogField() {
32                 super();
33                 fText= ""; //$NON-NLS-1$
34         }
35                         
36         // ------- layout helpers
37                 
38         /*
39          * @see DialogField#doFillIntoGrid
40          */
41         public Control[] doFillIntoGrid(Composite parent, int nColumns) {
42                 assertEnoughColumns(nColumns);
43                 
44                 Label label= getLabelControl(parent);
45                 label.setLayoutData(gridDataForLabel(1));
46                 Text text= getTextControl(parent);
47                 text.setLayoutData(gridDataForText(nColumns - 1));
48                 
49                 return new Control[] { label, text };
50         } 
51
52         /*
53          * @see DialogField#getNumberOfControls
54          */
55         public int getNumberOfControls() {
56                 return 2;       
57         }
58         
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;
64                 return gd;
65         }       
66         
67         // ------- focus methods
68         
69         /*
70          * @see DialogField#setFocus
71          */
72         public boolean setFocus() {
73                 if (isOkToUse(fTextControl)) {
74                         fTextControl.setFocus();
75                         fTextControl.setSelection(0, fTextControl.getText().length());
76                 }
77                 return true;
78         }
79                 
80         // ------- ui creation                  
81
82         /**
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.
86          */             
87         public Text getTextControl(Composite parent) {
88                 if (fTextControl == null) {
89                         assertCompositeNotNull(parent);
90                         fModifyListener= new ModifyListener() {
91                                 public void modifyText(ModifyEvent e) {
92                                         doModifyText(e);
93                                 }
94                         };
95                         
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);
101                         
102                         fTextControl.setEnabled(isEnabled());
103                 }
104                 return fTextControl;
105         }
106         
107         private void doModifyText(ModifyEvent e) {
108                 if (isOkToUse(fTextControl)) {
109                         fText= fTextControl.getText();
110                 }
111                 dialogFieldChanged();
112         }               
113         
114         // ------ enable / disable management
115         
116         /*
117          * @see DialogField#updateEnableState
118          */             
119         protected void updateEnableState() {
120                 super.updateEnableState();              
121                 if (isOkToUse(fTextControl)) {
122                         fTextControl.setEnabled(isEnabled());
123                 }       
124         }               
125                 
126         // ------ text access 
127         
128         /**
129          * Gets the text. Can not be <code>null</code>
130          */     
131         public String getText() {
132                 return fText;
133         }
134         
135         /**
136          * Sets the text. Triggers a dialog-changed event.
137          */
138         public void setText(String text) {
139                 fText= text;
140                 if (isOkToUse(fTextControl)) {
141                         fTextControl.setText(text);
142                 } else {
143                         dialogFieldChanged();
144                 }       
145         }
146
147         /**
148          * Sets the text without triggering a dialog-changed event.
149          */
150         public void setTextWithoutUpdate(String text) {
151                 fText= text;
152                 if (isOkToUse(fTextControl)) {
153                         fTextControl.removeModifyListener(fModifyListener);
154                         fTextControl.setText(text);
155                         fTextControl.addModifyListener(fModifyListener);
156                 }
157         }
158         
159 }