add support for updating bundle activator on package rename (FELIX-2502)


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@981398 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/sigil/eclipse/ui/plugin.properties b/sigil/eclipse/ui/plugin.properties
index c87b069..4b96436 100644
--- a/sigil/eclipse/ui/plugin.properties
+++ b/sigil/eclipse/ui/plugin.properties
@@ -3,6 +3,7 @@
 librariesPrefsPage=OSGi Libraries
 commandConvertProject=Convert Project To Sigil Project
 commandRefreshClasspath=Refresh bundle classpath
+RenameActivatorPackageParticipant.name=Rename Activator Package Participant
 RenameActivatorParticipant.name=Rename Activator Participant
 RenamePackageParticipant.name=Rename Package Participant
 MoveActivatorParticipant.name=Move Activator Participant
diff --git a/sigil/eclipse/ui/plugin.xml b/sigil/eclipse/ui/plugin.xml
index 74b51e7..c18582a 100644
--- a/sigil/eclipse/ui/plugin.xml
+++ b/sigil/eclipse/ui/plugin.xml
@@ -289,6 +289,21 @@
          </with>
        </enablement>
      </renameParticipant>
+     <renameParticipant
+        id="org.apache.felix.sigil.movePackageParticipant"
+        name="%RenameActivatorPackageParticipant.name" 
+        class="org.apache.felix.sigil.ui.eclipse.refactor.RenameActivatorPackageParticipant">
+       <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.IPackageFragment"/>
+         </with>
+       </enablement>
+     </renameParticipant>
    </extension>  
    <extension point="org.eclipse.ltk.core.refactoring.moveParticipants">
      <moveParticipant
diff --git a/sigil/eclipse/ui/src/org/apache/felix/sigil/ui/eclipse/refactor/RenameActivatorPackageParticipant.java b/sigil/eclipse/ui/src/org/apache/felix/sigil/ui/eclipse/refactor/RenameActivatorPackageParticipant.java
new file mode 100644
index 0000000..07d33b0
--- /dev/null
+++ b/sigil/eclipse/ui/src/org/apache/felix/sigil/ui/eclipse/refactor/RenameActivatorPackageParticipant.java
@@ -0,0 +1,118 @@
+/*
+ * 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.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 RenameActivatorPackageParticipant extends RenameParticipant
+{
+    private IPackageFragment packageFragment;
+    private List<Change> changes = new LinkedList<Change>();
+
+    @Override
+    public RefactoringStatus checkConditions(IProgressMonitor monitor,
+        CheckConditionsContext ctx) throws OperationCanceledException
+    {
+        RefactoringStatus status = null;
+        if(getArguments().getUpdateReferences()) {
+            try
+            {
+                ISigilProjectModel sigil = SigilCore.create(packageFragment.getJavaProject().getProject());
+                RefactorUtil.touch(ctx, sigil);
+                
+                String oldName = sigil.getBundle().getBundleInfo().getActivator();
+                if ( oldName != null ) {
+                    String[] parts = splitPackageClass(oldName);
+                    if (parts[0].equals(packageFragment.getElementName())) {
+                        String newName = getArguments().getNewName() + "." + parts[1];
+                        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;
+    }
+
+    private static String[] splitPackageClass(String className)
+    {
+        String[] parts = new String[2];
+        int i = className.lastIndexOf('.');
+        if ( i == -1 ) {
+            parts[0] = "";
+            parts[1] = className;
+        }
+        else {
+            parts[0] = className.substring(0, i);
+            parts[1] = className.substring(i + 1);
+        }
+        return parts;
+    }
+
+    @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()
+    {        
+        return "Sigil Rename Activator Package Participant";
+    }
+
+    @Override
+    protected boolean initialize(Object element)
+    {
+        packageFragment = (IPackageFragment) element;
+        return true;
+    }
+
+}