Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(28)

Unified Diff: editor/tools/plugins/com.google.dart.tools.ui/src/com/google/dart/tools/ui/internal/refactoring/TextInputWizardPage.java

Issue 9834028: Initial implementation of "Rename Local Variable" refactoring. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: LInked/inline mode for rename Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: editor/tools/plugins/com.google.dart.tools.ui/src/com/google/dart/tools/ui/internal/refactoring/TextInputWizardPage.java
diff --git a/editor/tools/plugins/com.google.dart.tools.ui/src/com/google/dart/tools/ui/internal/refactoring/TextInputWizardPage.java b/editor/tools/plugins/com.google.dart.tools.ui/src/com/google/dart/tools/ui/internal/refactoring/TextInputWizardPage.java
new file mode 100644
index 0000000000000000000000000000000000000000..ce846dcdeee352d84b38bd128c9c38eb05189fbf
--- /dev/null
+++ b/editor/tools/plugins/com.google.dart.tools.ui/src/com/google/dart/tools/ui/internal/refactoring/TextInputWizardPage.java
@@ -0,0 +1,187 @@
+package com.google.dart.tools.ui.internal.refactoring;
+
+import com.google.dart.tools.ui.internal.dialogs.TextFieldNavigationHandler;
+
+import org.eclipse.core.runtime.Assert;
+import org.eclipse.ltk.core.refactoring.RefactoringStatus;
+import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Text;
+
+/**
+ * A TextInputWizardPage is a simple UserInputWizardPage with facilities to create a text input
+ * field and validate its contents. The text is assumed to be a Dart identifier, hence CamelCase
+ * word jumping is installed.
+ */
+public abstract class TextInputWizardPage extends UserInputWizardPage {
+
+ private final String fInitialValue;
+ private Text fTextField;
+
+ public static final String PAGE_NAME = "TextInputPage";//$NON-NLS-1$
+
+ /**
+ * Creates a new text input page.
+ *
+ * @param isLastUserPage <code>true</code> if this page is the wizard's last user input page.
+ * Otherwise <code>false</code>.
+ */
+ public TextInputWizardPage(String description, boolean isLastUserPage) {
+ this(description, isLastUserPage, ""); //$NON-NLS-1$
+ }
+
+ /**
+ * Creates a new text input page.
+ *
+ * @param isLastUserPage <code>true</code> if this page is the wizard's last user input page.
+ * Otherwise <code>false</code>
+ * @param initialValue the initial value
+ */
+ public TextInputWizardPage(String description, boolean isLastUserPage, String initialValue) {
+ super(PAGE_NAME);
+ Assert.isNotNull(initialValue);
+ setDescription(description);
+ fInitialValue = initialValue;
+ }
+
+ @Override
+ public void dispose() {
+ fTextField = null;
+ }
+
+ /**
+ * Returns the initial value.
+ *
+ * @return the initial value
+ */
+ public String getInitialValue() {
+ return fInitialValue;
+ }
+
+ @Override
+ public void setVisible(boolean visible) {
+ if (visible) {
+ textModified(getText());
+ }
+ super.setVisible(visible);
+ if (visible && fTextField != null) {
+ fTextField.setFocus();
+ }
+ }
+
+ protected Text createTextInputField(Composite parent) {
+ return createTextInputField(parent, SWT.BORDER);
+ }
+
+ protected Text createTextInputField(Composite parent, int style) {
+ fTextField = new Text(parent, style);
+ fTextField.addModifyListener(new ModifyListener() {
+ @Override
+ public void modifyText(ModifyEvent e) {
+ textModified(getText());
+ }
+ });
+ fTextField.setText(fInitialValue);
+ TextFieldNavigationHandler.install(fTextField);
+ return fTextField;
+ }
+
+ /**
+ * Returns the content of the text input field.
+ *
+ * @return the content of the text input field. Returns <code>null</code> if not text input field
+ * has been created
+ */
+ protected String getText() {
+ if (fTextField == null) {
+ return null;
+ }
+ return fTextField.getText();
+ }
+
+ /**
+ * Returns the text entry field
+ *
+ * @return the text entry field
+ */
+ protected Text getTextField() {
+ return fTextField;
+ }
+
+ /**
+ * Returns whether an empty string is a valid input. Typically it is not, because the user is
+ * required to provide some information e.g. a new type name etc.
+ *
+ * @return <code>true</code> iff an empty string is valid
+ */
+ protected boolean isEmptyInputValid() {
+ return false;
+ }
+
+ /**
+ * Returns whether the initial input is valid. Typically it is not, because the user is required
+ * to provide some information e.g. a new type name etc.
+ *
+ * @return <code>true</code> iff the input provided at initialization is valid
+ */
+ protected boolean isInitialInputValid() {
+ return false;
+ }
+
+ /**
+ * Subclasses can override if they want to restore the message differently. This implementation
+ * calls <code>setMessage(null)</code>, which clears the message thus exposing the description.
+ */
+ protected void restoreMessage() {
+ setMessage(null);
+ }
+
+ /**
+ * Sets the new text for the text field. Does nothing if the text field has not been created.
+ *
+ * @param text the new value
+ */
+ protected void setText(String text) {
+ if (fTextField == null) {
+ return;
+ }
+ fTextField.setText(text);
+ }
+
+ /**
+ * Checks the page's state and issues a corresponding error message. The page validation is
+ * computed by calling <code>validatePage</code>.
+ */
+ protected void textModified(String text) {
+ if (!isEmptyInputValid() && "".equals(text)) { //$NON-NLS-1$
+ setPageComplete(false);
+ setErrorMessage(null);
+ restoreMessage();
+ return;
+ }
+ if (!isInitialInputValid() && fInitialValue.equals(text)) {
+ setPageComplete(false);
+ setErrorMessage(null);
+ restoreMessage();
+ return;
+ }
+
+ RefactoringStatus status = validateTextField(text);
+ if (status == null) {
+ status = new RefactoringStatus();
+ }
+ setPageComplete(status);
+ }
+
+ /**
+ * Performs input validation. Returns a <code>RefactoringStatus</code> which describes the result
+ * of input validation. <code>Null<code> is interpreted
+ * as no error.
+ */
+ protected RefactoringStatus validateTextField(String text) {
+ return null;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698