001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.refactoring;
011:
012: import org.eclipse.core.runtime.Assert;
013:
014: import org.eclipse.swt.SWT;
015: import org.eclipse.swt.events.ModifyEvent;
016: import org.eclipse.swt.events.ModifyListener;
017: import org.eclipse.swt.widgets.Composite;
018: import org.eclipse.swt.widgets.Text;
019:
020: import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler;
021:
022: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
023: import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
024:
025: /**
026: * A TextInputWizardPage is a simple UserInputWizardPage with facilities
027: * to create a text input field and validate its contents.
028: * The text is assumed to be a java identifier, hence CamelCase word jumping is installed.
029: */
030: public abstract class TextInputWizardPage extends UserInputWizardPage {
031:
032: private String fInitialValue;
033: private Text fTextField;
034:
035: public static final String PAGE_NAME = "TextInputPage";//$NON-NLS-1$
036:
037: /**
038: * Creates a new text input page.
039: * @param isLastUserPage <code>true</code> if this page is the wizard's last
040: * user input page. Otherwise <code>false</code>.
041: */
042: public TextInputWizardPage(String description,
043: boolean isLastUserPage) {
044: this (description, isLastUserPage, ""); //$NON-NLS-1$
045: }
046:
047: /**
048: * Creates a new text input page.
049: * @param isLastUserPage <code>true</code> if this page is the wizard's last
050: * user input page. Otherwise <code>false</code>
051: * @param initialValue the initial value
052: */
053: public TextInputWizardPage(String description,
054: boolean isLastUserPage, String initialValue) {
055: super (PAGE_NAME);
056: Assert.isNotNull(initialValue);
057: setDescription(description);
058: fInitialValue = initialValue;
059: }
060:
061: /**
062: * Returns whether the initial input is valid. Typically it is not, because the
063: * user is required to provide some information e.g. a new type name etc.
064: *
065: * @return <code>true</code> iff the input provided at initialization is valid
066: */
067: protected boolean isInitialInputValid() {
068: return false;
069: }
070:
071: /**
072: * Returns whether an empty string is a valid input. Typically it is not, because
073: * the user is required to provide some information e.g. a new type name etc.
074: *
075: * @return <code>true</code> iff an empty string is valid
076: */
077: protected boolean isEmptyInputValid() {
078: return false;
079: }
080:
081: /**
082: * Returns the content of the text input field.
083: *
084: * @return the content of the text input field. Returns <code>null</code> if
085: * not text input field has been created
086: */
087: protected String getText() {
088: if (fTextField == null)
089: return null;
090: return fTextField.getText();
091: }
092:
093: /**
094: * Sets the new text for the text field. Does nothing if the text field has not been created.
095: * @param text the new value
096: */
097: protected void setText(String text) {
098: if (fTextField == null)
099: return;
100: fTextField.setText(text);
101: }
102:
103: /**
104: * Returns the text entry field
105: *
106: * @return the text entry field
107: */
108: protected Text getTextField() {
109: return fTextField;
110: }
111:
112: /**
113: * Returns the initial value.
114: *
115: * @return the initial value
116: */
117: public String getInitialValue() {
118: return fInitialValue;
119: }
120:
121: /**
122: * Performs input validation. Returns a <code>RefactoringStatus</code> which
123: * describes the result of input validation. <code>Null<code> is interpreted
124: * as no error.
125: */
126: protected RefactoringStatus validateTextField(String text) {
127: return null;
128: }
129:
130: protected Text createTextInputField(Composite parent) {
131: return createTextInputField(parent, SWT.BORDER);
132: }
133:
134: protected Text createTextInputField(Composite parent, int style) {
135: fTextField = new Text(parent, style);
136: fTextField.addModifyListener(new ModifyListener() {
137: public void modifyText(ModifyEvent e) {
138: textModified(getText());
139: }
140: });
141: fTextField.setText(fInitialValue);
142: TextFieldNavigationHandler.install(fTextField);
143: return fTextField;
144: }
145:
146: /**
147: * Checks the page's state and issues a corresponding error message. The page validation
148: * is computed by calling <code>validatePage</code>.
149: */
150: protected void textModified(String text) {
151: if (!isEmptyInputValid() && "".equals(text)) { //$NON-NLS-1$
152: setPageComplete(false);
153: setErrorMessage(null);
154: restoreMessage();
155: return;
156: }
157: if ((!isInitialInputValid()) && fInitialValue.equals(text)) {
158: setPageComplete(false);
159: setErrorMessage(null);
160: restoreMessage();
161: return;
162: }
163:
164: RefactoringStatus status = validateTextField(text);
165: if (status == null)
166: status = new RefactoringStatus();
167: setPageComplete(status);
168: }
169:
170: /**
171: * Subclasses can override if they want to restore the message differently.
172: * This implementation calls <code>setMessage(null)</code>, which clears the message
173: * thus exposing the description.
174: */
175: protected void restoreMessage() {
176: setMessage(null);
177: }
178:
179: /* (non-Javadoc)
180: * Method declared in IDialogPage
181: */
182: public void dispose() {
183: fTextField = null;
184: }
185:
186: /* (non-Javadoc)
187: * Method declared in WizardPage
188: */
189: public void setVisible(boolean visible) {
190: if (visible) {
191: textModified(getText());
192: }
193: super.setVisible(visible);
194: if (visible && fTextField != null) {
195: fTextField.setFocus();
196: }
197: }
198: }
|