Refactoringaction: net.sourceforge.phpdt.ltk.ui.actions.RenameLocalVariable
authoraxelcl <axelcl>
Wed, 18 Jan 2006 20:36:28 +0000 (20:36 +0000)
committeraxelcl <axelcl>
Wed, 18 Jan 2006 20:36:28 +0000 (20:36 +0000)
This refactoring replaces any $-variable inside a function or method declaration
The action can detect the scope of a PHP function or class method.
This refactoring replaces variables inside double quoted strings.
This refactoring replaces a variable in a preceding PHPDoc comment.
This refactoring replaces a variable in a line or block comment.

This refactoring doesn't look for global variables inside the function or method.
This refactoring doesn't look correctly for "self::" and "$this" tokens inside the function or method.

net.sourceforge.phpeclipse/plugin.xml
net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/core/RenameIdentifierInfo.java
net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/core/RenameLocalVariableDelegate.java
net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/ui/UITexts.java
net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/ui/actions/RenameLocalVariable.java
net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/ui/uitexts.properties
net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/ui/wizards/RenameLocalVariablePage.java
net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ltk/ui/wizards/RenameLocalVariableWizard.java

index c747152..dd4922d 100644 (file)
                id="net.sourceforge.phpeclipse.actions.PHPOpenDeclarationEditorAction">
          </action>
       </viewerContribution>
- <!--     <viewerContribution
+     <viewerContribution
         targetID="#PHPEditorContext"
-        id="net.sourceforge.phpdt.ltk.renamePHPIdentifier">
+        id="net.sourceforge.phpdt.ltk.renameLocalVariable">
         <action label="Rename Local Variable in Functions/Methods"
                 class="net.sourceforge.phpdt.ltk.ui.actions.RenameLocalVariable"
                 menubarPath="additions"
                 id="net.sourceforge.phpdt.ltk.ui.actions.RenameLocalVariable"/>
       </viewerContribution>
-      <viewerContribution
+  <!--    <viewerContribution
         targetID="#PHPEditorContext"
         id="net.sourceforge.phpdt.ltk.renamePHPIdentifier">
         <action label="Rename PHP Identifier"
                 class="net.sourceforge.phpdt.ltk.ui.actions.RenamePHPIdentifier"
                 menubarPath="additions"
                 id="net.sourceforge.phpdt.ltk.ui.actions.RenamePHPIdentifier"/>
-      </viewerContribution>  -->
+      </viewerContribution> -->
       <viewerContribution
             targetID="#PHPRulerContext"
             id="net.sourceforge.phpdt.ui.PHPEditorPopupActions">
index 60a9457..6951116 100644 (file)
@@ -36,6 +36,12 @@ public class RenameIdentifierInfo {
        // projects than the current one
        private boolean allProjects;
 
+       private boolean renameDQString;
+
+       private boolean renamePHPdoc;
+
+       private boolean renameOtherComments;
+
        private SourceMethod method;
 
        public int getOffset() {
@@ -93,4 +99,28 @@ public class RenameIdentifierInfo {
        public void setMethod(SourceMethod method) {
                this.method = method;
        }
+
+       public boolean isRenameDQString() {
+               return renameDQString;
+       }
+
+       public void setRenameDQString(boolean renameDQString) {
+               this.renameDQString = renameDQString;
+       }
+
+       public boolean isRenameOtherComments() {
+               return renameOtherComments;
+       }
+
+       public void setRenameOtherComments(boolean renameOtherComments) {
+               this.renameOtherComments = renameOtherComments;
+       }
+
+       public boolean isRenamePHPdoc() {
+               return renamePHPdoc;
+       }
+
+       public void setRenamePHPdoc(boolean renamePHPdoc) {
+               this.renamePHPdoc = renamePHPdoc;
+       }
 }
index dfa9ce1..077d3bb 100644 (file)
@@ -35,145 +35,197 @@ import org.eclipse.ltk.core.refactoring.participants.ValidateEditChecker;
  */
 public class RenameLocalVariableDelegate extends RenameIdentifierDelegate {
 
-       public RenameLocalVariableDelegate(final RenameIdentifierInfo info) {
-               super(info);
-       }
-
-       RefactoringStatus checkInitialConditions() {
-               RefactoringStatus result = new RefactoringStatus();
-               IFile sourceFile = info.getSourceFile();
-               if (sourceFile == null || !sourceFile.exists()) {
-                       result.addFatalError(CoreTexts.renamePropertyDelegate_noSourceFile);
-               } else if (info.getSourceFile().isReadOnly()) {
-                       result.addFatalError(CoreTexts.renamePropertyDelegate_roFile);
-               } else if (isEmpty(info.getOldName())) {
-                       // || !isPropertyKey( info.getSourceFile(), info.getOldName() ) ) {
-                       result.addFatalError(CoreTexts.renamePropertyDelegate_noPHPKey);
-               }
-               return result;
-       }
-
-       RefactoringStatus checkFinalConditions(final IProgressMonitor pm, final CheckConditionsContext ctxt) {
-               RefactoringStatus result = new RefactoringStatus();
-               pm.beginTask(CoreTexts.renamePropertyDelegate_checking, 100);
-               // do something long-running here: traverse the entire project (or even
-               // workspace) to look for all *.p files with the same bundle
-               // base name
-               IFile file = info.getSourceFile();
-               IProject project = file.getProject();
-               try {
-                       SourceMethod method = info.getMethod();
-                       ISourceRange range = method.getSourceRange();
-                       if (project.isNatureEnabled(PHPeclipsePlugin.PHP_NATURE_ID)) {
-                               determineMethodOffsets(file, range.getOffset(), range.getLength(), result);
-                       }
-               } catch (CoreException e) {
-                       String msg = "Project: " + project.getLocation().toOSString() + " CoreException " + e.getMessage();
-                       result.addError(msg);
-               } catch (Exception e) {
-                       String msg = "Project: " + project.getLocation().toOSString() + " Exception " + e.getMessage();
-                       result.addError(msg);
-               }
-
-               pm.worked(50);
-
-               if (ctxt != null) {
-                       IFile[] files = new IFile[phpFiles.size()];
-                       phpFiles.keySet().toArray(files);
-                       IConditionChecker checker = ctxt.getChecker(ValidateEditChecker.class);
-                       ValidateEditChecker editChecker = (ValidateEditChecker) checker;
-                       editChecker.addFiles(files);
-               }
-               pm.done();
-               return result;
-       }
-
-       protected void createChange(final IProgressMonitor pm, final CompositeChange rootChange) {
-               try {
-                       pm.beginTask(CoreTexts.renamePropertyDelegate_collectingChanges, 100);
-                       // all files in the same bundle
-                       rootChange.addAll(createChangesForContainer(pm));
-               } finally {
-                       pm.done();
-               }
-       }
-
-       // finds the offsets of the identifier to rename
-       // usually, this would be the job of a proper parser;
-       // using a primitive brute-force approach here
-       private void determineMethodOffsets(final IFile file, int offset, int length, final RefactoringStatus status) {
-               ArrayList matches = new ArrayList();
-               try {
-                       String content = readFileContent(file, status);
-                       String methodString = content.substring(offset, offset + length);
-                       Scanner scanner = new Scanner(true, false);
-                       scanner.setSource(methodString.toCharArray());
-                       scanner.setPHPMode(true);
-                       String wordStr = info.getOldName();
-                       char[] word = wordStr.toCharArray();
-
-                       int fToken = ITerminalSymbols.TokenNameEOF;
-                       String dqStr;
-                       int dqOffset;
-                       int index;
-                       try {
-                               fToken = scanner.getNextToken();
-                               while (fToken != ITerminalSymbols.TokenNameEOF) {
-                                       if (fToken == ITerminalSymbols.TokenNameVariable) {
-                                               if (scanner.equalsCurrentTokenSource(word)) {
-                                                       // the current variable token is equal to the given word
-                                                       matches.add(Integer.valueOf(scanner.getCurrentTokenStartPosition() + offset));
-                                               }
-                                       } else if (fToken == ITerminalSymbols.TokenNameStringDoubleQuote) {
-                                               // determine the word in double quoted strings:
-                                               dqStr = new String(scanner.getCurrentTokenSource());
-                                               dqOffset = scanner.getCurrentTokenStartPosition();
-                                               index = -1;
-                                               while ((index = dqStr.indexOf(wordStr, index + 1)) >= 0) {
-                                                       matches.add(Integer.valueOf(offset + dqOffset + index));
-                                               }
-                                       }
-                                       fToken = scanner.getNextToken();
-                               }
-
-                       } catch (InvalidInputException e) {
-                               String msg = "File: " + file.getLocation().toOSString() + " InvalidInputException " + e.getMessage();
-                               status.addError(msg);
-                       } catch (SyntaxError e) {
-                               String msg = "File: " + file.getLocation().toOSString() + " SyntaxError " + e.getMessage();
-                               status.addError(msg);
-                       }
-
-               } catch (Exception e) {
-                       String msg = "File: " + file.getLocation().toOSString() + " Exception " + e.getMessage();
-                       status.addError(msg);
-               }
-               if (matches.size() > 0) {
-                       phpFiles.put(file, matches);
-               }
-       }
-
-       private String readFileContent(final IFile file, final RefactoringStatus refStatus) {
-               String result = null;
-               try {
-                       InputStream is = file.getContents();
-                       byte[] buf = new byte[1024];
-                       ByteArrayOutputStream bos = new ByteArrayOutputStream();
-                       int len = is.read(buf);
-                       while (len > 0) {
-                               bos.write(buf, 0, len);
-                               len = is.read(buf);
-                       }
-                       is.close();
-                       result = new String(bos.toByteArray());
-               } catch (Exception ex) {
-                       String msg = ex.toString();
-                       refStatus.addFatalError(msg);
-                       String pluginId = PHPeclipsePlugin.getPluginId();
-                       IStatus status = new Status(IStatus.ERROR, pluginId, 0, msg, ex);
-                       PHPeclipsePlugin.getDefault().getLog().log(status);
-               }
-               return result;
-       }
+  public RenameLocalVariableDelegate(final RenameIdentifierInfo info) {
+    super(info);
+  }
+
+  RefactoringStatus checkInitialConditions() {
+    RefactoringStatus result = new RefactoringStatus();
+    IFile sourceFile = info.getSourceFile();
+    if (sourceFile == null || !sourceFile.exists()) {
+      result.addFatalError(CoreTexts.renamePropertyDelegate_noSourceFile);
+    } else if (info.getSourceFile().isReadOnly()) {
+      result.addFatalError(CoreTexts.renamePropertyDelegate_roFile);
+    } else if (isEmpty(info.getOldName())) {
+      // || !isPropertyKey( info.getSourceFile(), info.getOldName() ) ) {
+      result.addFatalError(CoreTexts.renamePropertyDelegate_noPHPKey);
+    }
+    return result;
+  }
+
+  RefactoringStatus checkFinalConditions(final IProgressMonitor pm, final CheckConditionsContext ctxt) {
+    RefactoringStatus result = new RefactoringStatus();
+    pm.beginTask(CoreTexts.renamePropertyDelegate_checking, 100);
+    // do something long-running here: traverse the entire project (or even
+    // workspace) to look for all *.p files with the same bundle
+    // base name
+    IFile file = info.getSourceFile();
+    IProject project = file.getProject();
+    try {
+      SourceMethod method = info.getMethod();
+      ISourceRange range = method.getSourceRange();
+      if (project.isNatureEnabled(PHPeclipsePlugin.PHP_NATURE_ID)) {
+        determineMethodOffsets(file, range.getOffset(), range.getLength(), result);
+      }
+    } catch (CoreException e) {
+      String msg = "Project: " + project.getLocation().toOSString() + " CoreException " + e.getMessage();
+      result.addError(msg);
+    } catch (Exception e) {
+      String msg = "Project: " + project.getLocation().toOSString() + " Exception " + e.getMessage();
+      result.addError(msg);
+    }
+
+    pm.worked(50);
+
+    if (ctxt != null) {
+      IFile[] files = new IFile[phpFiles.size()];
+      phpFiles.keySet().toArray(files);
+      IConditionChecker checker = ctxt.getChecker(ValidateEditChecker.class);
+      ValidateEditChecker editChecker = (ValidateEditChecker) checker;
+      editChecker.addFiles(files);
+    }
+    pm.done();
+    return result;
+  }
+
+  protected void createChange(final IProgressMonitor pm, final CompositeChange rootChange) {
+    try {
+      pm.beginTask(CoreTexts.renamePropertyDelegate_collectingChanges, 100);
+      // all files in the same bundle
+      rootChange.addAll(createChangesForContainer(pm));
+    } finally {
+      pm.done();
+    }
+  }
+
+  private void determineMethodOffsets(final IFile file, int offset, int length, final RefactoringStatus status) {
+    ArrayList matches = new ArrayList();
+    try {
+      String content = readFileContent(file, status);
+
+      //
+      // Find a PHPdoc directly before the method
+      //
+      Scanner firstScanner = new Scanner(true, false);
+      firstScanner.setSource(content.toCharArray());
+      int fToken = ITerminalSymbols.TokenNameEOF;
+      int start = 0;
+      int phpdocStart = -1;
+      try {
+        fToken = firstScanner.getNextToken();
+        while (fToken != ITerminalSymbols.TokenNameEOF && start < offset) {
+          if (fToken == ITerminalSymbols.TokenNameCOMMENT_PHPDOC) {
+            phpdocStart = firstScanner.getCurrentTokenStartPosition();
+          } else {
+            phpdocStart = -1;
+          }
+          fToken = firstScanner.getNextToken();
+          start = firstScanner.getCurrentTokenStartPosition();
+        }
+
+      } catch (InvalidInputException e) {
+        String msg = "File: " + file.getLocation().toOSString() + " InvalidInputException " + e.getMessage();
+        status.addError(msg);
+      } catch (SyntaxError e) {
+        String msg = "File: " + file.getLocation().toOSString() + " SyntaxError " + e.getMessage();
+        status.addError(msg);
+      }
+
+      //
+      // Find matches for the word in the PHPdoc+method declaration
+      //
+      if (phpdocStart >= 0 && phpdocStart < offset) {
+        length += offset - phpdocStart;
+        offset = phpdocStart;
+      }
+      String methodString = content.substring(offset, offset + length);
+      Scanner secondScanner = new Scanner(true, false);
+      secondScanner.setSource(methodString.toCharArray());
+      secondScanner.setPHPMode(true);
+      String wordStr = info.getOldName();
+      boolean renameDQString = info.isRenameDQString();
+      boolean renamePHPdoc = info.isRenamePHPdoc();
+      boolean renameOtherComments = info.isRenameOtherComments();
+      char[] word = wordStr.toCharArray();
+
+      fToken = ITerminalSymbols.TokenNameEOF;
+      // double quoted string
+      String tokenString;
+      // double quoted string offset
+      int tokenOffset;
+      int index;
+      try {
+        fToken = secondScanner.getNextToken();
+        while (fToken != ITerminalSymbols.TokenNameEOF) {
+          if (fToken == ITerminalSymbols.TokenNameVariable) {
+            if (secondScanner.equalsCurrentTokenSource(word)) {
+              // the current variable token is equal to the given word
+              matches.add(Integer.valueOf(secondScanner.getCurrentTokenStartPosition() + offset));
+            }
+          } else if (fToken == ITerminalSymbols.TokenNameStringDoubleQuote && renameDQString) {
+            // determine the word in double quoted strings:
+            tokenString = new String(secondScanner.getCurrentTokenSource());
+            tokenOffset = secondScanner.getCurrentTokenStartPosition();
+            index = -1;
+            while ((index = tokenString.indexOf(wordStr, index + 1)) >= 0) {
+              matches.add(Integer.valueOf(offset + tokenOffset + index));
+            }
+          } else if (fToken == ITerminalSymbols.TokenNameCOMMENT_PHPDOC && renamePHPdoc) {
+            tokenString = new String(secondScanner.getCurrentTokenSource());
+            tokenOffset = secondScanner.getCurrentTokenStartPosition();
+            index = -1;
+            while ((index = tokenString.indexOf(wordStr, index + 1)) >= 0) {
+              matches.add(Integer.valueOf(offset + tokenOffset + index));
+            }
+          } else if ( (fToken == ITerminalSymbols.TokenNameCOMMENT_BLOCK || fToken == ITerminalSymbols.TokenNameCOMMENT_LINE) && renameOtherComments) {
+            tokenString = new String(secondScanner.getCurrentTokenSource());
+            tokenOffset = secondScanner.getCurrentTokenStartPosition();
+            index = -1;
+            while ((index = tokenString.indexOf(wordStr, index + 1)) >= 0) {
+              matches.add(Integer.valueOf(offset + tokenOffset + index));
+            }
+          }
+          fToken = secondScanner.getNextToken();
+        }
+
+      } catch (InvalidInputException e) {
+        String msg = "File: " + file.getLocation().toOSString() + " InvalidInputException " + e.getMessage();
+        status.addError(msg);
+      } catch (SyntaxError e) {
+        String msg = "File: " + file.getLocation().toOSString() + " SyntaxError " + e.getMessage();
+        status.addError(msg);
+      }
+
+    } catch (Exception e) {
+      String msg = "File: " + file.getLocation().toOSString() + " Exception " + e.getMessage();
+      status.addError(msg);
+    }
+    if (matches.size() > 0) {
+      phpFiles.put(file, matches);
+    }
+  }
+
+  private String readFileContent(final IFile file, final RefactoringStatus refStatus) {
+    String result = null;
+    try {
+      InputStream is = file.getContents();
+      byte[] buf = new byte[1024];
+      ByteArrayOutputStream bos = new ByteArrayOutputStream();
+      int len = is.read(buf);
+      while (len > 0) {
+        bos.write(buf, 0, len);
+        len = is.read(buf);
+      }
+      is.close();
+      result = new String(bos.toByteArray());
+    } catch (Exception ex) {
+      String msg = ex.toString();
+      refStatus.addFatalError(msg);
+      String pluginId = PHPeclipsePlugin.getPluginId();
+      IStatus status = new Status(IStatus.ERROR, pluginId, 0, msg, ex);
+      PHPeclipsePlugin.getDefault().getLog().log(status);
+    }
+    return result;
+  }
 
 }
index a70f518..adac1e0 100644 (file)
@@ -33,4 +33,11 @@ public class UITexts {
        public static String renameLocalVariable_refuseDlg_title;
 
        public static String renameLocalVariable_refuseDlg_message;
+
+       public static String renameLocalVariable_cbDQStrings;
+
+       public static String renameLocalVariable_cbPHPdoc;
+
+       public static String renameLocalVariable_cbOtherDoc;
+
 }
index 9bc7415..0b1a91c 100644 (file)
@@ -51,9 +51,6 @@ public class RenameLocalVariable implements IEditorActionDelegate {
 
        private RenameIdentifierInfo info = new RenameIdentifierInfo();
 
-       // interface methods of IEditorActionDelegate
-       // ///////////////////////////////////////////
-
        public void setActiveEditor(final IAction action, final IEditorPart targetEditor) {
                this.targetEditor = targetEditor;
                onPHPFile = false;
@@ -86,17 +83,17 @@ public class RenameLocalVariable implements IEditorActionDelegate {
                                                                SourceMethod method = (SourceMethod) findEnclosingElement(point.x, unit, IJavaElement.METHOD);
                                                                if (word == null || word.charAt(0) != '$' || method == null || !(method instanceof SourceMethod)) {
                                                                        refuseLocalVariable();
+                                                               } else {
+                                                                       applySelection((ITextSelection) selection, word, point, method);
+                                                                       if (saveAll()) {
+                                                                               openWizard();
+                                                                       }
                                                                }
-                                                               applySelection((ITextSelection) selection, word, point, method);
-
                                                        } catch (BadLocationException e) {
                                                        }
                                                }
                                        }
                                }
-                               if (saveAll()) {
-                                       openWizard();
-                               }
                        }
                }
        }
@@ -148,6 +145,7 @@ public class RenameLocalVariable implements IEditorActionDelegate {
                String message = UITexts.renameLocalVariable_refuseDlg_message;
                MessageDialog.openInformation(getShell(), title, message);
        }
+
        private void refuse() {
                String title = UITexts.renameProperty_refuseDlg_title;
                String message = UITexts.renameProperty_refuseDlg_message;
index 197714f..219e39d 100644 (file)
@@ -5,4 +5,7 @@ renamePropertyInputPage_cbUpdateBundle=Update all PHP files in the same project
 renamePropertyInputPage_cbAllProjects=Update all PHP projects in the workspace (forces preview)
 
 renameLocalVariable_refuseDlg_title=Rename Local PHP variable
-renameLocalVariable_refuseDlg_message=The selected identifier is no local PHP variable.
\ No newline at end of file
+renameLocalVariable_refuseDlg_message=The selected identifier is no local PHP variable.
+renameLocalVariable_cbDQStrings=Rename variable inside double quoted strings
+renameLocalVariable_cbPHPdoc=Rename variable inside PHPdoc comments
+renameLocalVariable_cbOtherDoc=Rename variable inside other PHP comments (line or block)
\ No newline at end of file
index 9a536ce..3da0b98 100644 (file)
@@ -2,6 +2,7 @@
 // See http://leiffrenzel.de
 package net.sourceforge.phpdt.ltk.ui.wizards;
 
+import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
 import net.sourceforge.phpdt.ltk.core.RenameIdentifierInfo;
 import net.sourceforge.phpdt.ltk.ui.UITexts;
 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
@@ -12,6 +13,8 @@ import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.KeyAdapter;
 import org.eclipse.swt.events.KeyEvent;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Button;
@@ -19,134 +22,192 @@ import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Text;
 
-
-/** <p>the input page for the Rename Property refactoring, where users can
-  * control the effects of the refactoring; to be shown in the wizard.</p>
-  *
-  * <p>We let the user enter the new name for the property, and we let her
-  * decide whether other property files in the bundle should be affected, and
-  * whether the operation is supposed to span the entire workspace or only
-  * the current project.</p>
-  *
-  */
+/**
+ * <p>
+ * the input page for the Rename Property refactoring, where users can control
+ * the effects of the refactoring; to be shown in the wizard.
+ * </p>
+ *
+ * <p>
+ * We let the user enter the new name for the property, and we let her decide
+ * whether other property files in the bundle should be affected, and whether
+ * the operation is supposed to span the entire workspace or only the current
+ * project.
+ * </p>
+ *
+ */
 public class RenameLocalVariablePage extends UserInputWizardPage {
 
-  private static final String DS_KEY = RenameLocalVariablePage.class.getName();
-  private static final String DS_UPDATE_BUNDLE = "UPDATE_BUNDLE"; //$NON-NLS-1$
-  private static final String DS_ALL_PROJECTS  = "ALL_PROJECTS"; //$NON-NLS-1$
-
-  private final RenameIdentifierInfo info;
-
-  private IDialogSettings dialogSettings;
-  private Text txtNewName;
-
-
-  public RenameLocalVariablePage( final RenameIdentifierInfo info ) {
-    super( RenameLocalVariablePage.class.getName() );
-    this.info = info;
-    initDialogSettings();
-  }
-
-
-  // interface methods of UserInputWizardPage
-  ///////////////////////////////////////////
-
-  public void createControl( final Composite parent ) {
-    Composite composite = createRootComposite( parent );
-    setControl( composite );
-
-    createLblNewName( composite );
-    createTxtNewName( composite );
-    createCbUpdateBundle( composite );
-    createCbAllProjects( composite );
-
-    validate();
-  }
-
-
-  // UI creation methods
-  //////////////////////
-
-  private Composite createRootComposite( final Composite parent ) {
-    Composite result = new Composite( parent, SWT.NONE );
-    GridLayout gridLayout = new GridLayout( 2, false );
-    gridLayout.marginWidth = 10;
-    gridLayout.marginHeight = 10;
-    result.setLayout( gridLayout );
-    initializeDialogUnits( result );
-    Dialog.applyDialogFont( result );
-    return result;
-  }
-
-  private void createLblNewName( final Composite composite ) {
-    Label lblNewName = new Label( composite, SWT.NONE );
-    lblNewName.setText( UITexts.renamePropertyInputPage_lblNewName );
-  }
-
-  private void createTxtNewName(Composite composite) {
-    txtNewName = new Text( composite, SWT.BORDER );
-    txtNewName.setText( info.getOldName() );
-    txtNewName.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
-    txtNewName.selectAll();
-    txtNewName.addKeyListener( new KeyAdapter() {
-      public void keyReleased( final KeyEvent e ) {
-        info.setNewName( txtNewName.getText() );
-        validate();
-      }
-    } );
-  }
-
-  private void createCbUpdateBundle( final Composite composite ) {
-    String texts = UITexts.renamePropertyInputPage_cbUpdateBundle;
-    initUpdateBundleOption();
-  }
-
-  private void createCbAllProjects( final Composite composite ) {
-    String text = UITexts.renamePropertyInputPage_cbAllProjects;
-    initAllProjectsOption();
-  }
-
-  private Button createCheckbox( final Composite composite,
-                                 final String text ) {
-    Button result = new Button( composite, SWT.CHECK );
-    result.setText( text );
-
-    GridData gridData = new GridData( GridData.FILL_HORIZONTAL );
-    gridData.horizontalSpan = 2;
-    result.setLayoutData( gridData );
-
-    return result;
-  }
-
-
-  // helping methods
-  //////////////////
-
-  private void initDialogSettings() {
-    IDialogSettings ds = PHPeclipsePlugin.getDefault().getDialogSettings();
-    dialogSettings = ds.getSection( DS_KEY );
-    if( dialogSettings == null ) {
-      dialogSettings = ds.addNewSection( DS_KEY );
-      // init default values
-      dialogSettings.put( DS_UPDATE_BUNDLE, true );
-      dialogSettings.put( DS_ALL_PROJECTS, false );
-    }
-  }
-
-  private void validate() {
-    String txt = txtNewName.getText();
-    setPageComplete( txt.length() > 0 && !txt.equals( info.getOldName() ) );
-  }
-
-  private void initUpdateBundleOption() {
-//    boolean updateRefs = dialogSettings.getBoolean( DS_UPDATE_BUNDLE );
-//    cbUpdateBundle.setSelection( updateRefs );
-//    info.setUpdateProject( updateRefs );
-  }
-
-  private void initAllProjectsOption() {
-//    boolean allProjects = dialogSettings.getBoolean( DS_ALL_PROJECTS );
-//    cbAllProjects.setSelection( allProjects );
-//    info.setAllProjects( allProjects );
-  }
+       private static final String DS_KEY = RenameLocalVariablePage.class.getName();
+
+       private static final String DS_RENAME_DQ_STRINGS = "RENAME_DQ_STRINGS"; //$NON-NLS-1$
+
+       private static final String DS_RENAME_PHPDOC = "RENAME_PHPDOC"; //$NON-NLS-1$
+
+       private static final String DS_RENAME_OTHER_COMMENTS = "RENAME_OTHER_COMMENTS"; //$NON-NLS-1$
+
+       private final RenameIdentifierInfo info;
+
+       private IDialogSettings dialogSettings;
+
+       private Text txtNewName;
+
+       private Button cbRenameDQStrings;
+
+       private Button cbRenamePHPdoc;
+
+       private Button cbRenameOtherComments;
+
+       public RenameLocalVariablePage(final RenameIdentifierInfo info) {
+               super(RenameLocalVariablePage.class.getName());
+               this.info = info;
+               initDialogSettings();
+       }
+
+       public void createControl(final Composite parent) {
+               Composite composite = createRootComposite(parent);
+               setControl(composite);
+
+               createLblNewName(composite);
+               createTxtNewName(composite);
+               createCbDQStrings(composite);
+               createCbPHPdoc(composite);
+               createCbOtherComments(composite);
+
+               validate();
+
+               // TODO check if we can leave this step out in the future
+               getRefactoringWizard().setForcePreviewReview(true);
+       }
+
+       private Composite createRootComposite(final Composite parent) {
+               Composite result = new Composite(parent, SWT.NONE);
+               GridLayout gridLayout = new GridLayout(2, false);
+               gridLayout.marginWidth = 10;
+               gridLayout.marginHeight = 10;
+               result.setLayout(gridLayout);
+               initializeDialogUnits(result);
+               Dialog.applyDialogFont(result);
+               return result;
+       }
+
+       private void createLblNewName(final Composite composite) {
+               Label lblNewName = new Label(composite, SWT.NONE);
+               lblNewName.setText(UITexts.renamePropertyInputPage_lblNewName);
+       }
+
+       private void createTxtNewName(Composite composite) {
+               txtNewName = new Text(composite, SWT.BORDER);
+               txtNewName.setText(info.getOldName());
+               txtNewName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
+               txtNewName.selectAll();
+               txtNewName.addKeyListener(new KeyAdapter() {
+                       public void keyReleased(final KeyEvent e) {
+                               info.setNewName(txtNewName.getText());
+                               validate();
+                       }
+               });
+       }
+
+       private void createCbDQStrings(final Composite composite) {
+               String texts = UITexts.renameLocalVariable_cbDQStrings;
+               cbRenameDQStrings = createCheckbox(composite, texts);
+               cbRenameDQStrings.addSelectionListener(new SelectionAdapter() {
+                       public void widgetSelected(final SelectionEvent event) {
+                               boolean selected = cbRenameDQStrings.getSelection();
+                               dialogSettings.put(DS_RENAME_DQ_STRINGS, selected);
+                               info.setRenameDQString(selected);
+                       }
+               });
+               initDQStringsOption();
+       }
+
+       private void createCbPHPdoc(final Composite composite) {
+               String texts = UITexts.renameLocalVariable_cbPHPdoc;
+               cbRenamePHPdoc = createCheckbox(composite, texts);
+               cbRenamePHPdoc.addSelectionListener(new SelectionAdapter() {
+                       public void widgetSelected(final SelectionEvent event) {
+                               boolean selected = cbRenamePHPdoc.getSelection();
+                               dialogSettings.put(DS_RENAME_PHPDOC, selected);
+                               info.setRenamePHPdoc(selected);
+                       }
+               });
+               initPHPdocOption();
+       }
+
+       private void createCbOtherComments(final Composite composite) {
+               String texts = UITexts.renameLocalVariable_cbOtherDoc;
+               cbRenameOtherComments = createCheckbox(composite, texts);
+               cbRenameOtherComments.addSelectionListener(new SelectionAdapter() {
+                       public void widgetSelected(final SelectionEvent event) {
+                               boolean selected = cbRenameOtherComments.getSelection();
+                               dialogSettings.put(DS_RENAME_OTHER_COMMENTS, selected);
+                               info.setRenameOtherComments(selected);
+                       }
+               });
+               initOtherCommentsOption();
+       }
+
+       private Button createCheckbox(final Composite composite, final String text) {
+               Button result = new Button(composite, SWT.CHECK);
+               result.setText(text);
+
+               GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
+               gridData.horizontalSpan = 2;
+               result.setLayoutData(gridData);
+
+               return result;
+       }
+
+       private void initDialogSettings() {
+               IDialogSettings ds = PHPeclipsePlugin.getDefault().getDialogSettings();
+               dialogSettings = ds.getSection(DS_KEY);
+               if (dialogSettings == null) {
+                       dialogSettings = ds.addNewSection(DS_KEY);
+                       // init default values
+                       dialogSettings.put(DS_RENAME_DQ_STRINGS, true);
+                       dialogSettings.put(DS_RENAME_PHPDOC, true);
+                       dialogSettings.put(DS_RENAME_OTHER_COMMENTS, true);
+               }
+       }
+
+       private static boolean isVariable(String txt) {
+               if (txt.length() <= 1) {
+                       return false;
+               }
+               if (txt.charAt(0) != '$') {
+                       return false;
+               }
+               for (int i = 1; i < txt.length(); i++) {
+                       if (!Scanner.isPHPIdentifierPart(txt.charAt(i))) {
+                               return false;
+                       }
+               }
+               return true;
+       }
+
+       private void validate() {
+               String txt = txtNewName.getText();
+               Scanner s;
+               setPageComplete(isVariable(txt) && !txt.equals(info.getOldName()));
+       }
+
+       private void initDQStringsOption() {
+               boolean refs = dialogSettings.getBoolean(DS_RENAME_DQ_STRINGS);
+               cbRenameDQStrings.setSelection(refs);
+               info.setRenameDQString(refs);
+       }
+
+       private void initPHPdocOption() {
+               boolean refs = dialogSettings.getBoolean(DS_RENAME_PHPDOC);
+               cbRenamePHPdoc.setSelection(refs);
+               info.setRenamePHPdoc(refs);
+       }
+
+       private void initOtherCommentsOption() {
+               boolean refs = dialogSettings.getBoolean(DS_RENAME_OTHER_COMMENTS);
+               cbRenameOtherComments.setSelection(refs);
+               info.setRenameOtherComments(refs);
+       }
+
 }
index 6174f21..e3ee6da 100644 (file)
@@ -10,7 +10,7 @@ import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
 
 
 /** <p>The wizard that is shown to the user for entering the necessary
-  * information for property renaming.</p>
+  * information for property renaming of local PHP variables.</p>
   *
   * <p>The wizard class is primarily needed for deciding which pages are
   * shown to the user. The actual user interface creation goes on the
@@ -29,9 +29,6 @@ public class RenameLocalVariableWizard extends RefactoringWizard {
   }
 
 
-  // interface methods of RefactoringWizard
-  /////////////////////////////////////////
-
   protected void addUserInputPages() {
     setDefaultPageTitle( getRefactoring().getName() );
     addPage( new RenameLocalVariablePage( info ) );