KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > dialogfields > StringDialogField


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.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 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
23
24 import org.eclipse.jdt.internal.ui.refactoring.contentassist.ControlContentAssistHelper;
25
26 /**
27  * Dialog field containing a label and a text control.
28  */

29 public class StringDialogField extends DialogField {
30         
31     private String JavaDoc fText;
32     private Text fTextControl;
33     private ModifyListener fModifyListener;
34     private IContentAssistProcessor fContentAssistProcessor;
35     
36     public StringDialogField() {
37         super();
38         fText= ""; //$NON-NLS-1$
39
}
40             
41     public void setContentAssistProcessor(IContentAssistProcessor processor) {
42         fContentAssistProcessor= processor;
43         if (fContentAssistProcessor != null && isOkToUse(fTextControl)) {
44             ControlContentAssistHelper.createTextContentAssistant(fTextControl, fContentAssistProcessor);
45         }
46     }
47
48     public IContentAssistProcessor getContentAssistProcessor() {
49         return fContentAssistProcessor;
50     }
51     
52     // ------- layout helpers
53

54     /*
55      * @see DialogField#doFillIntoGrid
56      */

57     public Control[] doFillIntoGrid(Composite parent, int nColumns) {
58         assertEnoughColumns(nColumns);
59         
60         Label label= getLabelControl(parent);
61         label.setLayoutData(gridDataForLabel(1));
62         Text text= getTextControl(parent);
63         text.setLayoutData(gridDataForText(nColumns - 1));
64         
65         return new Control[] { label, text };
66     }
67
68     /*
69      * @see DialogField#getNumberOfControls
70      */

71     public int getNumberOfControls() {
72         return 2;
73     }
74     
75     protected static GridData gridDataForText(int span) {
76         GridData gd= new GridData();
77         gd.horizontalAlignment= GridData.FILL;
78         gd.grabExcessHorizontalSpace= false;
79         gd.horizontalSpan= span;
80         return gd;
81     }
82     
83     // ------- focus methods
84

85     /*
86      * @see DialogField#setFocus
87      */

88     public boolean setFocus() {
89         if (isOkToUse(fTextControl)) {
90             fTextControl.setFocus();
91             fTextControl.setSelection(0, fTextControl.getText().length());
92         }
93         return true;
94     }
95         
96     // ------- ui creation
97

98     /**
99      * Creates or returns the created text control.
100      * @param parent The parent composite or <code>null</code> when the widget has
101      * already been created.
102      */

103     public Text getTextControl(Composite parent) {
104         if (fTextControl == null) {
105             assertCompositeNotNull(parent);
106             fModifyListener= new ModifyListener() {
107                 public void modifyText(ModifyEvent e) {
108                     doModifyText(e);
109                 }
110             };
111             
112             fTextControl= new Text(parent, SWT.SINGLE | SWT.BORDER);
113             // moved up due to 1GEUNW2
114
fTextControl.setText(fText);
115             fTextControl.setFont(parent.getFont());
116             fTextControl.addModifyListener(fModifyListener);
117             
118             fTextControl.setEnabled(isEnabled());
119             if (fContentAssistProcessor != null) {
120                 ControlContentAssistHelper.createTextContentAssistant(fTextControl, fContentAssistProcessor);
121             }
122         }
123         return fTextControl;
124     }
125     
126     private void doModifyText(ModifyEvent e) {
127         if (isOkToUse(fTextControl)) {
128             fText= fTextControl.getText();
129         }
130         dialogFieldChanged();
131     }
132     
133     // ------ enable / disable management
134

135     /*
136      * @see DialogField#updateEnableState
137      */

138     protected void updateEnableState() {
139         super.updateEnableState();
140         if (isOkToUse(fTextControl)) {
141             fTextControl.setEnabled(isEnabled());
142         }
143     }
144         
145     // ------ text access
146

147     /**
148      * Gets the text. Can not be <code>null</code>
149      */

150     public String JavaDoc getText() {
151         return fText;
152     }
153     
154     /**
155      * Sets the text. Triggers a dialog-changed event.
156      */

157     public void setText(String JavaDoc text) {
158         fText= text;
159         if (isOkToUse(fTextControl)) {
160             fTextControl.setText(text);
161         } else {
162             dialogFieldChanged();
163         }
164     }
165
166     /**
167      * Sets the text without triggering a dialog-changed event.
168      */

169     public void setTextWithoutUpdate(String JavaDoc text) {
170         fText= text;
171         if (isOkToUse(fTextControl)) {
172             fTextControl.removeModifyListener(fModifyListener);
173             fTextControl.setText(text);
174             fTextControl.addModifyListener(fModifyListener);
175         }
176     }
177     
178     /* (non-Javadoc)
179      * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField#refresh()
180      */

181     public void refresh() {
182         super.refresh();
183         if (isOkToUse(fTextControl)) {
184             setTextWithoutUpdate(fText);
185         }
186     }
187     
188 }
189
Popular Tags