add activator refactor support (FELIX-2482)


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@967011 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/sigil/eclipse/ui/plugin.properties b/sigil/eclipse/ui/plugin.properties
index 91bd60c..c87b069 100644
--- a/sigil/eclipse/ui/plugin.properties
+++ b/sigil/eclipse/ui/plugin.properties
@@ -3,5 +3,7 @@
 librariesPrefsPage=OSGi Libraries
 commandConvertProject=Convert Project To Sigil Project
 commandRefreshClasspath=Refresh bundle classpath
+RenameActivatorParticipant.name=Rename Activator Participant
 RenamePackageParticipant.name=Rename Package Participant
+MoveActivatorParticipant.name=Move Activator Participant
 MovePackageParticipant.name=Move Package Participant
\ No newline at end of file
diff --git a/sigil/eclipse/ui/plugin.xml b/sigil/eclipse/ui/plugin.xml
index 04e151d..e35acc1 100644
--- a/sigil/eclipse/ui/plugin.xml
+++ b/sigil/eclipse/ui/plugin.xml
@@ -281,6 +281,21 @@
          </with>
        </enablement>
      </renameParticipant>
+     <renameParticipant
+        id="org.apache.felix.sigil.renameActivatorParticipant"
+        name="%RenameActivatorParticipant.name" 
+        class="org.apache.felix.sigil.ui.eclipse.refactor.RenameActivatorParticipant">
+       <enablement>
+         <with variable="affectedNatures">
+           <iterate operator="or">
+             <equals value="org.apache.felix.sigil.sigilnature"/>
+           </iterate>
+         </with>
+         <with variable="element">
+           <instanceof value="org.eclipse.jdt.core.ICompilationUnit"/>
+         </with>
+       </enablement>
+     </renameParticipant>
    </extension>  
    <extension point="org.eclipse.ltk.core.refactoring.moveParticipants">
      <moveParticipant
@@ -298,5 +313,20 @@
          </with>
        </enablement>
      </moveParticipant>
+     <moveParticipant
+        id="org.apache.felix.sigil.moveActivatorParticipant"
+        name="%MoveActivatorParticipant.name" 
+        class="org.apache.felix.sigil.ui.eclipse.refactor.MoveActivatorParticipant">
+       <enablement>
+         <with variable="affectedNatures">
+           <iterate operator="or">
+             <equals value="org.apache.felix.sigil.sigilnature"/>
+           </iterate>
+         </with>
+         <with variable="element">
+           <instanceof value="org.eclipse.jdt.core.ICompilationUnit"/>
+         </with>
+       </enablement>
+     </moveParticipant>
    </extension>  
 </plugin>
diff --git a/sigil/eclipse/ui/src/org/apache/felix/sigil/ui/eclipse/refactor/BundleActivatorChange.java b/sigil/eclipse/ui/src/org/apache/felix/sigil/ui/eclipse/refactor/BundleActivatorChange.java
new file mode 100644
index 0000000..0aa2695
--- /dev/null
+++ b/sigil/eclipse/ui/src/org/apache/felix/sigil/ui/eclipse/refactor/BundleActivatorChange.java
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.felix.sigil.ui.eclipse.refactor;
+
+import org.apache.felix.sigil.eclipse.model.project.ISigilProjectModel;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.OperationCanceledException;
+import org.eclipse.ltk.core.refactoring.Change;
+import org.eclipse.ltk.core.refactoring.RefactoringStatus;
+
+/**
+ * @author dave
+ *
+ */
+public class BundleActivatorChange extends Change
+{
+
+    private final ISigilProjectModel sigil;
+    private final String oldName;
+    private final String newName;
+
+    /**
+     * @param sigil
+     * @param oldName
+     * @param newName
+     */
+    public BundleActivatorChange(ISigilProjectModel sigil, String oldName, String newName)
+    {
+        this.sigil = sigil;
+        this.oldName = oldName;
+        this.newName = newName;
+    }
+
+    /* (non-Javadoc)
+     * @see org.eclipse.ltk.core.refactoring.Change#getModifiedElement()
+     */
+    @Override
+    public Object getModifiedElement()
+    {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    /* (non-Javadoc)
+     * @see org.eclipse.ltk.core.refactoring.Change#getName()
+     */
+    @Override
+    public String getName()
+    {
+        return "Bundle Activator Rename";
+    }
+
+    /* (non-Javadoc)
+     * @see org.eclipse.ltk.core.refactoring.Change#initializeValidationData(org.eclipse.core.runtime.IProgressMonitor)
+     */
+    @Override
+    public void initializeValidationData(IProgressMonitor monitor)
+    {
+        // TODO Auto-generated method stub
+        
+    }
+
+    /* (non-Javadoc)
+     * @see org.eclipse.ltk.core.refactoring.Change#isValid(org.eclipse.core.runtime.IProgressMonitor)
+     */
+    @Override
+    public RefactoringStatus isValid(IProgressMonitor monitor) throws CoreException,
+        OperationCanceledException
+    {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    /* (non-Javadoc)
+     * @see org.eclipse.ltk.core.refactoring.Change#perform(org.eclipse.core.runtime.IProgressMonitor)
+     */
+    @Override
+    public Change perform(IProgressMonitor monitor) throws CoreException
+    {
+        sigil.getBundle().getBundleInfo().setActivator(newName);
+        sigil.save(monitor);
+        return new BundleActivatorChange(sigil, newName, oldName);
+    }
+
+}
diff --git a/sigil/eclipse/ui/src/org/apache/felix/sigil/ui/eclipse/refactor/MoveActivatorParticipant.java b/sigil/eclipse/ui/src/org/apache/felix/sigil/ui/eclipse/refactor/MoveActivatorParticipant.java
new file mode 100644
index 0000000..170d876
--- /dev/null
+++ b/sigil/eclipse/ui/src/org/apache/felix/sigil/ui/eclipse/refactor/MoveActivatorParticipant.java
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.felix.sigil.ui.eclipse.refactor;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.felix.sigil.eclipse.SigilCore;
+import org.apache.felix.sigil.eclipse.model.project.ISigilProjectModel;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.OperationCanceledException;
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IJavaModel;
+import org.eclipse.jdt.core.IPackageFragment;
+import org.eclipse.ltk.core.refactoring.Change;
+import org.eclipse.ltk.core.refactoring.CompositeChange;
+import org.eclipse.ltk.core.refactoring.NullChange;
+import org.eclipse.ltk.core.refactoring.RefactoringStatus;
+import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
+import org.eclipse.ltk.core.refactoring.participants.MoveParticipant;
+
+public class MoveActivatorParticipant extends MoveParticipant
+{
+    private ICompilationUnit compilationUnit;
+    private List<Change> changes = new LinkedList<Change>();
+
+    @Override
+    public RefactoringStatus checkConditions(IProgressMonitor monitor,
+        CheckConditionsContext ctx) throws OperationCanceledException
+    {
+        RefactoringStatus status = null;
+        if(getArguments().getUpdateReferences()) {
+            IPackageFragment pf = (IPackageFragment) compilationUnit.getAncestor(IJavaModel.PACKAGE_FRAGMENT);
+            String oldName = qualifiedName(pf, compilationUnit.getElementName());
+            try
+            {
+                ISigilProjectModel sigil = SigilCore.create(compilationUnit.getJavaProject().getProject());
+                if (oldName.equals(sigil.getBundle().getBundleInfo().getActivator())) {
+                    IPackageFragment dest = (IPackageFragment) getArguments().getDestination();
+                    String newName = qualifiedName(dest, compilationUnit.getElementName());
+                    
+                    RefactorUtil.touch(ctx, sigil);
+                    changes.add(new BundleActivatorChange(sigil, oldName, newName));
+                    status = RefactoringStatus.createInfoStatus("Updating bundle activator from " + oldName + " to " + newName );
+                }
+            }
+            catch (CoreException e)
+            {
+                SigilCore.warn("Failed to update activator", e);
+            }
+        }
+        
+        return status;
+    }
+
+    /**
+     * @param pf 
+     * @param compilationUnit2
+     * @return
+     */
+    private static String qualifiedName(IPackageFragment pf, String name)
+    {
+        name = name.substring(0, name.lastIndexOf('.'));
+        return pf.getElementName() + '.' + name;
+    }
+
+    @Override
+    public Change createChange(IProgressMonitor monitor) throws CoreException,
+        OperationCanceledException
+    {
+        if (changes.isEmpty()) {
+            return new NullChange();
+        }
+        else 
+        {
+            CompositeChange ret = new CompositeChange("Export-Package update");
+            
+            ret.addAll(changes.toArray(new Change[changes.size()]));
+            
+            return ret;
+        }
+    }
+
+    @Override
+    public String getName()
+    {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    protected boolean initialize(Object element)
+    {
+        compilationUnit = (ICompilationUnit) element;
+        return true;
+    }
+
+}
diff --git a/sigil/eclipse/ui/src/org/apache/felix/sigil/ui/eclipse/refactor/RenameActivatorParticipant.java b/sigil/eclipse/ui/src/org/apache/felix/sigil/ui/eclipse/refactor/RenameActivatorParticipant.java
new file mode 100644
index 0000000..3e9b36e
--- /dev/null
+++ b/sigil/eclipse/ui/src/org/apache/felix/sigil/ui/eclipse/refactor/RenameActivatorParticipant.java
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.felix.sigil.ui.eclipse.refactor;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.felix.sigil.eclipse.SigilCore;
+import org.apache.felix.sigil.eclipse.model.project.ISigilProjectModel;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.OperationCanceledException;
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IJavaModel;
+import org.eclipse.jdt.core.IPackageFragment;
+import org.eclipse.ltk.core.refactoring.Change;
+import org.eclipse.ltk.core.refactoring.CompositeChange;
+import org.eclipse.ltk.core.refactoring.NullChange;
+import org.eclipse.ltk.core.refactoring.RefactoringStatus;
+import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
+import org.eclipse.ltk.core.refactoring.participants.RenameParticipant;
+
+public class RenameActivatorParticipant extends RenameParticipant
+{
+    private ICompilationUnit compilationUnit;
+    private List<Change> changes = new LinkedList<Change>();
+
+    @Override
+    public RefactoringStatus checkConditions(IProgressMonitor monitor,
+        CheckConditionsContext ctx) throws OperationCanceledException
+    {
+        RefactoringStatus status = null;
+        if(getArguments().getUpdateReferences()) {
+            IPackageFragment pf = (IPackageFragment) compilationUnit.getAncestor(IJavaModel.PACKAGE_FRAGMENT);
+            String oldName = qualifiedName(pf, compilationUnit.getElementName());
+            try
+            {
+                ISigilProjectModel sigil = SigilCore.create(compilationUnit.getJavaProject().getProject());
+                if (oldName.equals(sigil.getBundle().getBundleInfo().getActivator())) {
+                    String newName = qualifiedName(pf, getArguments().getNewName());
+                    
+                    RefactorUtil.touch(ctx, sigil);
+                    changes.add(new BundleActivatorChange(sigil, oldName, newName));
+                    status = RefactoringStatus.createInfoStatus("Updating bundle activator from " + oldName + " to " + newName );
+                }
+            }
+            catch (CoreException e)
+            {
+                SigilCore.warn("Failed to update activator", e);
+            }
+        }
+        
+        return status;
+    }
+
+    /**
+     * @param pf 
+     * @param compilationUnit2
+     * @return
+     */
+    private static String qualifiedName(IPackageFragment pf, String name)
+    {
+        name = name.substring(0, name.lastIndexOf('.'));
+        return pf.getElementName() + '.' + name;
+    }
+
+    @Override
+    public Change createChange(IProgressMonitor monitor) throws CoreException,
+        OperationCanceledException
+    {
+        if (changes.isEmpty()) {
+            return new NullChange();
+        }
+        else 
+        {
+            CompositeChange ret = new CompositeChange("Export-Package update");
+            
+            ret.addAll(changes.toArray(new Change[changes.size()]));
+            
+            return ret;
+        }
+    }
+
+    @Override
+    public String getName()
+    {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    protected boolean initialize(Object element)
+    {
+        compilationUnit = (ICompilationUnit) element;
+        return true;
+    }
+
+}