KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > TextInputWizardPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
12 package org.eclipse.jdt.internal.ui.refactoring;
13
14 import org.eclipse.core.runtime.Assert;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.ModifyEvent;
18 import org.eclipse.swt.events.ModifyListener;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Text;
21
22
23 import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler;
24
25 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
26 import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
27
28 /**
29  * A TextInputWizardPage is a simple UserInputWizardPage with facilities
30  * to create a text input field and validate its contents.
31  * The text is assumed to be a java identifier, hence CamelCase word jumping is installed.
32  */

33 public abstract class TextInputWizardPage extends UserInputWizardPage{
34
35     private String JavaDoc fInitialValue;
36     private Text fTextField;
37     
38     public static final String JavaDoc PAGE_NAME= "TextInputPage";//$NON-NLS-1$
39

40     /**
41      * Creates a new text input page.
42      * @param isLastUserPage <code>true</code> if this page is the wizard's last
43      * user input page. Otherwise <code>false</code>.
44      */

45     public TextInputWizardPage(String JavaDoc description, boolean isLastUserPage) {
46         this(description, isLastUserPage, ""); //$NON-NLS-1$
47
}
48     
49     /**
50      * Creates a new text input page.
51      * @param isLastUserPage <code>true</code> if this page is the wizard's last
52      * user input page. Otherwise <code>false</code>
53      * @param initialValue the initial value
54      */

55     public TextInputWizardPage(String JavaDoc description, boolean isLastUserPage, String JavaDoc initialValue) {
56         super(PAGE_NAME);
57         Assert.isNotNull(initialValue);
58         setDescription(description);
59         fInitialValue= initialValue;
60     }
61     
62     /**
63      * Returns whether the initial input is valid. Typically it is not, because the
64      * user is required to provide some information e.g. a new type name etc.
65      *
66      * @return <code>true</code> iff the input provided at initialization is valid
67      */

68     protected boolean isInitialInputValid(){
69         return false;
70     }
71     
72     /**
73      * Returns whether an empty string is a valid input. Typically it is not, because
74      * the user is required to provide some information e.g. a new type name etc.
75      *
76      * @return <code>true</code> iff an empty string is valid
77      */

78     protected boolean isEmptyInputValid(){
79         return false;
80     }
81     
82     /**
83      * Returns the content of the text input field.
84      *
85      * @return the content of the text input field. Returns <code>null</code> if
86      * not text input field has been created
87      */

88     protected String JavaDoc getText() {
89         if (fTextField == null)
90             return null;
91         return fTextField.getText();
92     }
93     
94     /**
95      * Sets the new text for the text field. Does nothing if the text field has not been created.
96      * @param text the new value
97      */

98     protected void setText(String JavaDoc text) {
99         if (fTextField == null)
100             return;
101         fTextField.setText(text);
102     }
103     
104     /**
105      * Returns the text entry field
106      *
107      * @return the text entry field
108      */

109     protected Text getTextField() {
110         return fTextField;
111     }
112     
113     /**
114      * Returns the initial value.
115      *
116      * @return the initial value
117      */

118     public String JavaDoc getInitialValue() {
119         return fInitialValue;
120     }
121     
122     /**
123      * Performs input validation. Returns a <code>RefactoringStatus</code> which
124      * describes the result of input validation. <code>Null<code> is interpreted
125      * as no error.
126      */

127     protected RefactoringStatus validateTextField(String JavaDoc text){
128         return null;
129     }
130     
131     protected Text createTextInputField(Composite parent) {
132         return createTextInputField(parent, SWT.BORDER);
133     }
134     
135     protected Text createTextInputField(Composite parent, int style) {
136         fTextField= new Text(parent, style);
137         fTextField.addModifyListener(new ModifyListener() {
138             public void modifyText(ModifyEvent e) {
139                 textModified(getText());
140             }
141         });
142         fTextField.setText(fInitialValue);
143         TextFieldNavigationHandler.install(fTextField);
144         return fTextField;
145     }
146     
147     /**
148      * Checks the page's state and issues a corresponding error message. The page validation
149      * is computed by calling <code>validatePage</code>.
150      */

151     protected void textModified(String JavaDoc text) {
152         if (! isEmptyInputValid() && "".equals(text)){ //$NON-NLS-1$
153
setPageComplete(false);
154             setErrorMessage(null);
155             restoreMessage();
156             return;
157         }
158         if ((! isInitialInputValid()) && fInitialValue.equals(text)){
159             setPageComplete(false);
160             setErrorMessage(null);
161             restoreMessage();
162             return;
163         }
164         
165         RefactoringStatus status= validateTextField(text);
166         if (status == null)
167             status= new RefactoringStatus();
168         setPageComplete(status);
169     }
170     
171     /**
172      * Subclasses can override if they want to restore the message differently.
173      * This implementation calls <code>setMessage(null)</code>, which clears the message
174      * thus exposing the description.
175      */

176     protected void restoreMessage(){
177         setMessage(null);
178     }
179     
180     /* (non-Javadoc)
181      * Method declared in IDialogPage
182      */

183     public void dispose() {
184         fTextField= null;
185     }
186     
187     /* (non-Javadoc)
188      * Method declared in WizardPage
189      */

190     public void setVisible(boolean visible) {
191         if (visible) {
192             textModified(getText());
193         }
194         super.setVisible(visible);
195         if (visible && fTextField != null) {
196             fTextField.setFocus();
197         }
198     }
199 }
200
Popular Tags