Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-patch] refactoring update

The patch allows for starting the rename refactoring from the
main-menu with a CElement selection in one of the views.

Markus.
Index: plugin.xml
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.refactoring/plugin.xml,v
retrieving revision 1.3
diff -u -r1.3 plugin.xml
--- plugin.xml	21 May 2005 19:09:19 -0000	1.3
+++ plugin.xml	9 Jun 2005 13:44:05 -0000
@@ -9,7 +9,7 @@
       >
          <menu
                id="org.eclipse.cdt.ui.refactoring.menu"
-               label="Refactor"
+               label="%Refactoring.menu.label"
                path="group.reorganize">
             <separator name="reorgGroup"/>
             <separator name="typeGroup"/>
Index: src/org/eclipse/cdt/internal/refactoring/ui/ActionAdapter.java
===================================================================
RCS file: src/org/eclipse/cdt/internal/refactoring/ui/ActionAdapter.java
diff -N src/org/eclipse/cdt/internal/refactoring/ui/ActionAdapter.java
--- src/org/eclipse/cdt/internal/refactoring/ui/ActionAdapter.java	22 Apr 2005 17:10:09 -0000	1.1
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,39 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2005 Wind River Systems, Inc. 
- * All rights reserved. This program and the accompanying materials 
- * are made available under the terms of the Eclipse Public License v1.0 
- * which accompanies this distribution, and is available at 
- * http://www.eclipse.org/legal/epl-v10.html  
- * 
- * Contributors: 
- * Markus Schorn - initial API and implementation 
- ******************************************************************************/ 
-
-package org.eclipse.cdt.internal.refactoring.ui;
-
-import org.eclipse.jface.action.Action;
-import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.ui.IWorkbenchSite;
-import org.eclipse.ui.IWorkbenchWindowActionDelegate;
-
-public class ActionAdapter extends Action {
-    private IWorkbenchWindowActionDelegate fDelegate;
-
-    public ActionAdapter(IWorkbenchSite site, String name, IWorkbenchWindowActionDelegate delegate) {
-        super(name);
-        fDelegate= delegate;
-        fDelegate.init(site.getWorkbenchWindow());
-    }
-    public void update(ISelection sel) {
-        fDelegate.selectionChanged(ActionAdapter.this, sel);
-    }
-    public void run() {
-        fDelegate.run(ActionAdapter.this);
-    }
-    public void dispose() {
-        if (fDelegate != null) {
-            fDelegate.dispose();
-            fDelegate= null;
-        }
-    }
-}
Index: src/org/eclipse/cdt/refactoring/CRefactory.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.refactoring/src/org/eclipse/cdt/refactoring/CRefactory.java,v
retrieving revision 1.4
diff -u -r1.4 CRefactory.java
--- src/org/eclipse/cdt/refactoring/CRefactory.java	12 May 2005 14:39:16 -0000	1.4
+++ src/org/eclipse/cdt/refactoring/CRefactory.java	9 Jun 2005 13:44:05 -0000
@@ -22,6 +22,7 @@
 import org.eclipse.cdt.internal.core.dom.SavedCodeReaderFactory;
 import org.eclipse.cdt.internal.refactoring.*;
 import org.eclipse.cdt.internal.refactoring.ui.CRenameRefactoringWizard;
+import org.eclipse.cdt.refactoring.actions.CElementPositionAdapter;
 import org.eclipse.core.resources.*;
 import org.eclipse.jface.text.ITextSelection;
 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
@@ -66,6 +67,7 @@
     private ICRefactoringSearch fTextSearch;
     private String[] fAffectedProjectNatures;
     private IParserConfigurationProvider[] fParserConfigurationProviders= new IParserConfigurationProvider[0];
+    private IPositionProvider[] fPositionAdapters= new IPositionProvider[0];
     
     public static CRefactory getInstance() {
         return sInstance;
@@ -89,6 +91,7 @@
                 CProjectNature.C_NATURE_ID, 
                 CCProjectNature.CC_NATURE_ID
         };
+        addPositionProvider(new CElementPositionAdapter());
     }
     
     // runs the rename refactoring
@@ -190,4 +193,22 @@
         fParserConfigurationProviders= 
             (IParserConfigurationProvider[]) now.toArray(new IParserConfigurationProvider[now.size()]);
     }
+
+    public void addPositionProvider(IPositionProvider adapter) {
+        HashSet now= new HashSet();
+        now.addAll(Arrays.asList(fPositionAdapters));
+        now.add(adapter);
+        fPositionAdapters= 
+            (IPositionProvider[]) now.toArray(new IPositionProvider[now.size()]);
+    }
+
+    public boolean providePosition(Object o, IPositionConsumer consumer) {
+        for (int i = 0; i < fPositionAdapters.length; i++) {
+            IPositionProvider provider = fPositionAdapters[i];
+            if (provider.providePosition(o, consumer)) {
+                return true;
+            }
+        }
+        return false;
+    }
 }
Index: src/org/eclipse/cdt/refactoring/actions/CRefactoringActionGroup.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.refactoring/src/org/eclipse/cdt/refactoring/actions/CRefactoringActionGroup.java,v
retrieving revision 1.1
diff -u -r1.1 CRefactoringActionGroup.java
--- src/org/eclipse/cdt/refactoring/actions/CRefactoringActionGroup.java	22 Apr 2005 17:10:09 -0000	1.1
+++ src/org/eclipse/cdt/refactoring/actions/CRefactoringActionGroup.java	9 Jun 2005 13:44:05 -0000
@@ -13,6 +13,8 @@
 
 package org.eclipse.cdt.refactoring.actions;
 
+import org.eclipse.cdt.refactoring.IPositionConsumer;
+import org.eclipse.core.resources.IFile;
 import org.eclipse.jface.action.*;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.ui.*;
@@ -30,7 +32,7 @@
  * 
  * @since 2.0
  */
-public class CRefactoringActionGroup extends ActionGroup {
+public class CRefactoringActionGroup extends ActionGroup implements IPositionConsumer {
     /**
      * Pop-up menu: id of the refactor sub menu (value <code>org.eclipse.cdt.ui.refactoring.menu</code>).
      * 
@@ -89,6 +91,7 @@
     }
     
     public void init(IWorkbenchPartSite site) {
+        fRenameAction.setWorkbenchPart(site.getPart());
     }
     
     public void setEditor(ITextEditor textEditor) {
@@ -135,4 +138,10 @@
         fUndoAction.dispose();
         super.dispose();
     }
+
+    public void setPosition(IFile file, int startPos, String text) {
+        fRenameAction.setPosition(file, startPos, text);
+        fUndoAction.selectionChanged(null);
+        fRedoAction.selectionChanged(null);
+    }
 }
Index: src/org/eclipse/cdt/refactoring/actions/CRenameAction.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.refactoring/src/org/eclipse/cdt/refactoring/actions/CRenameAction.java,v
retrieving revision 1.1
diff -u -r1.1 CRenameAction.java
--- src/org/eclipse/cdt/refactoring/actions/CRenameAction.java	22 Apr 2005 17:10:09 -0000	1.1
+++ src/org/eclipse/cdt/refactoring/actions/CRenameAction.java	9 Jun 2005 13:44:05 -0000
@@ -12,37 +12,20 @@
 package org.eclipse.cdt.refactoring.actions;
 
 import org.eclipse.cdt.internal.refactoring.Messages;
-import org.eclipse.cdt.refactoring.CRefactory;
-import org.eclipse.cdt.refactoring.ICRefactoringArgument;
+import org.eclipse.cdt.refactoring.*;
 import org.eclipse.core.resources.IFile;
 import org.eclipse.jface.action.Action;
-import org.eclipse.jface.action.IAction;
 import org.eclipse.jface.text.ITextSelection;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.ISelectionProvider;
-import org.eclipse.ui.*;
-import org.eclipse.ui.IEditorActionDelegate;
 import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IWorkbenchPart;
 import org.eclipse.ui.texteditor.ITextEditor;
 
 /**
  * Launches a rename refactoring.
  */          
-public class CRenameAction extends Action {
-    public static class EditorActionDelegate implements IEditorActionDelegate {
-        CRenameAction fAction= new CRenameAction();
-        public void setActiveEditor(IAction action, IEditorPart targetEditor) {
-            fAction.setEditor(targetEditor);
-            action.setEnabled(fAction.isEnabled());
-        }
-        public void run(IAction action) {
-            fAction.run();
-        }
-        public void selectionChanged(IAction action, ISelection selection) {
-        }
-    }
-    
-    
+public class CRenameAction extends Action implements IPositionConsumer {
     private ITextEditor fEditor;
     private IFile fFile;
     private IWorkbenchPart fWorkbenchPart;
@@ -51,6 +34,7 @@
     
     public CRenameAction() {
         super(Messages.getString("CRenameRefactoringAction.label")); //$NON-NLS-1$
+        setActionDefinitionId("org.eclipse.cdt.ui.edit.text.c.rename.element"); //$NON-NLS-1$
     }
     
     public void setEditor(IEditorPart editor) {
Index: src/org/eclipse/cdt/refactoring/actions/CRenameViewActionDelegate.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.refactoring/src/org/eclipse/cdt/refactoring/actions/CRenameViewActionDelegate.java,v
retrieving revision 1.1
diff -u -r1.1 CRenameViewActionDelegate.java
--- src/org/eclipse/cdt/refactoring/actions/CRenameViewActionDelegate.java	22 Apr 2005 17:10:08 -0000	1.1
+++ src/org/eclipse/cdt/refactoring/actions/CRenameViewActionDelegate.java	9 Jun 2005 13:44:05 -0000
@@ -11,17 +11,11 @@
 
 package org.eclipse.cdt.refactoring.actions;
         
-import org.eclipse.cdt.core.model.*;
-import org.eclipse.cdt.core.model.ICElement;
-import org.eclipse.cdt.core.model.ISourceRange;
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.IResource;
+import org.eclipse.cdt.refactoring.CRefactory;
 import org.eclipse.jface.action.IAction;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.ui.*;
-import org.eclipse.ui.IViewActionDelegate;
-import org.eclipse.ui.IViewPart;
 
 /**
  * Launches a rename refactoring.
@@ -40,43 +34,13 @@
         fAction.run();
     }
     public void selectionChanged(IAction action, ISelection selection) {
-        ISourceRange range= null;
-        IFile file= null;
-        String text= null;
+        boolean handled= false;
         if (selection instanceof IStructuredSelection) {
             IStructuredSelection ss= (IStructuredSelection) selection;
             Object o= ss.getFirstElement();
-            if (o instanceof ISourceReference) {
-                ISourceReference sref= (ISourceReference) o;
-                try {
-                    range= sref.getSourceRange();
-                } catch (CModelException e) {
-                }
-            }
-            if (o instanceof ICElement) {
-                ICElement e= (ICElement) o;
-                IResource r= e.getUnderlyingResource();
-                text= e.getElementName();
-                if (r instanceof IFile) {
-                    file= (IFile) r;
-                }
-            }
+            handled= CRefactory.getInstance().providePosition(o, fAction);
         }
-        if (range != null && file != null && text != null) {
-            int offset= range.getIdStartPos();
-            int useLength= 0;
-            int idx= text.length()-1;
-            while (idx >= 0) {
-                char c= text.charAt(idx);
-                if (!Character.isLetterOrDigit(c) &&  c!='_') {
-                    text= text.substring(idx+1);
-                    break;
-                }
-                useLength++;
-                idx--;
-            }
-            offset= range.getIdStartPos()+range.getIdLength()-useLength;
-            fAction.setPosition(file, offset, text);
+        if (handled) {
             action.setEnabled(fAction.isEnabled());
         }
         else {
Index: src/org/eclipse/cdt/refactoring/actions/CRenameWorkbenchActionDelegate.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.refactoring/src/org/eclipse/cdt/refactoring/actions/CRenameWorkbenchActionDelegate.java,v
retrieving revision 1.1
diff -u -r1.1 CRenameWorkbenchActionDelegate.java
--- src/org/eclipse/cdt/refactoring/actions/CRenameWorkbenchActionDelegate.java	22 Apr 2005 17:10:09 -0000	1.1
+++ src/org/eclipse/cdt/refactoring/actions/CRenameWorkbenchActionDelegate.java	9 Jun 2005 13:44:05 -0000
@@ -11,8 +11,10 @@
 
 package org.eclipse.cdt.refactoring.actions;
         
+import org.eclipse.cdt.refactoring.CRefactory;
 import org.eclipse.jface.action.IAction;
 import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.ui.*;
 
 /**
@@ -26,17 +28,27 @@
         fWorkbenchWindow= window;
     }
     public void selectionChanged(IAction action, ISelection selection) {
-        boolean disable= true;
+        boolean handled= false;
         IWorkbenchPage workbenchPage= fWorkbenchWindow.getActivePage();
+        IWorkbenchPart workbenchPart= null;
         if (workbenchPage != null) {
+            workbenchPart= workbenchPage.getActivePart();
             IEditorPart editor= workbenchPage.getActiveEditor();
-            if (editor == workbenchPage.getActivePart()) {
+            if (editor == workbenchPart) {
                 fAction.setEditor(editor);
-                disable= false;
+                handled= true;
             }
         }
         
-        if (disable) {
+        if (!handled && workbenchPart != null && selection instanceof IStructuredSelection) {
+            Object o= ((IStructuredSelection) selection).getFirstElement();
+            if (o != null) {
+                fAction.setWorkbenchPart(workbenchPart);
+                handled= CRefactory.getInstance().providePosition(o, fAction);
+            }
+        }
+        
+        if (!handled) {
             fAction.setEnabled(false);
             action.setEnabled(false);
         }
Index: src/org/eclipse/cdt/refactoring/IPositionConsumer.java
===================================================================
RCS file: src/org/eclipse/cdt/refactoring/IPositionConsumer.java
diff -N src/org/eclipse/cdt/refactoring/IPositionConsumer.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/cdt/refactoring/IPositionConsumer.java	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,19 @@
+/*******************************************************************************
+ * Copyright (c) 2005 Wind River Systems, Inc.
+ * All rights reserved. This program and the accompanying materials 
+ * are made available under the terms of the Eclipse Public License v1.0 
+ * which accompanies this distribution, and is available at 
+ * http://www.eclipse.org/legal/epl-v10.html  
+ * 
+ * Contributors: 
+ * Markus Schorn - initial API and implementation 
+ ******************************************************************************/ 
+package org.eclipse.cdt.refactoring;
+
+import org.eclipse.core.resources.IFile;
+
+public interface IPositionConsumer {
+
+    void setPosition(IFile file, int offset, String text);
+
+}
Index: src/org/eclipse/cdt/refactoring/IPositionProvider.java
===================================================================
RCS file: src/org/eclipse/cdt/refactoring/IPositionProvider.java
diff -N src/org/eclipse/cdt/refactoring/IPositionProvider.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/cdt/refactoring/IPositionProvider.java	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,17 @@
+/*******************************************************************************
+ * Copyright (c) 2005 Wind River Systems, Inc.
+ * All rights reserved. This program and the accompanying materials 
+ * are made available under the terms of the Eclipse Public License v1.0 
+ * which accompanies this distribution, and is available at 
+ * http://www.eclipse.org/legal/epl-v10.html  
+ * 
+ * Contributors: 
+ * Markus Schorn - initial API and implementation 
+ ******************************************************************************/ 
+package org.eclipse.cdt.refactoring;
+
+public interface IPositionProvider {
+
+    boolean providePosition(Object o, IPositionConsumer consumer);
+
+}
Index: src/org/eclipse/cdt/refactoring/actions/CElementPositionAdapter.java
===================================================================
RCS file: src/org/eclipse/cdt/refactoring/actions/CElementPositionAdapter.java
diff -N src/org/eclipse/cdt/refactoring/actions/CElementPositionAdapter.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/cdt/refactoring/actions/CElementPositionAdapter.java	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,58 @@
+/*
+ * CElementPositionAdapter.java
+ * Created on 09.06.2005
+ *
+ * Copyright 2005 Wind River Systems Inc. All rights reserved.
+ */
+package org.eclipse.cdt.refactoring.actions;
+
+import org.eclipse.cdt.core.model.*;
+import org.eclipse.cdt.refactoring.IPositionProvider;
+import org.eclipse.cdt.refactoring.IPositionConsumer;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IResource;
+
+public class CElementPositionAdapter implements IPositionProvider {
+    public boolean providePosition(Object o, IPositionConsumer consumer) {
+        if (o instanceof ITranslationUnit) {
+            return false;
+        }
+        if (o instanceof ISourceReference) {
+            ISourceReference sref= (ISourceReference) o;
+            ISourceRange range= null;
+            IFile file= null;
+            String text= null;
+
+            try {
+                range= sref.getSourceRange();
+            } catch (CModelException e) {
+            }
+            if (o instanceof ICElement) {
+                ICElement e= (ICElement) o;
+                IResource r= e.getUnderlyingResource();
+                text= e.getElementName();
+                if (r instanceof IFile) {
+                    file= (IFile) r;
+                }
+                if (range != null && file != null && text != null) {
+                    int offset= range.getIdStartPos();
+                    int useLength= 0;
+                    int idx= text.length()-1;
+                    while (idx >= 0) {
+                        char c= text.charAt(idx);
+                        if (!Character.isLetterOrDigit(c) &&  c!='_') {
+                            text= text.substring(idx+1);
+                            break;
+                        }
+                        useLength++;
+                        idx--;
+                    }
+                    offset= range.getIdStartPos()+range.getIdLength()-useLength;
+                    consumer.setPosition(file, offset, text);
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+}

Back to the top