KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > refactoring > RenamePluginProcessor


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.refactoring;
12
13 import org.eclipse.core.resources.IProject;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.core.resources.ResourcesPlugin;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IConfigurationElement;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.OperationCanceledException;
20 import org.eclipse.core.runtime.SubProgressMonitor;
21 import org.eclipse.jdt.core.refactoring.descriptors.RenameResourceDescriptor;
22 import org.eclipse.ltk.core.refactoring.Change;
23 import org.eclipse.ltk.core.refactoring.CompositeChange;
24 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
25 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
26 import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
27 import org.eclipse.ltk.core.refactoring.participants.IParticipantDescriptorFilter;
28 import org.eclipse.ltk.core.refactoring.participants.ParticipantManager;
29 import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
30 import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
31 import org.eclipse.ltk.core.refactoring.participants.RenameArguments;
32 import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
33 import org.eclipse.osgi.service.resolver.BundleDescription;
34 import org.eclipse.pde.core.plugin.IPluginModelBase;
35 import org.eclipse.pde.core.plugin.PluginRegistry;
36 import org.eclipse.pde.internal.ui.PDEUIMessages;
37 import org.osgi.framework.Constants;
38
39 import com.ibm.icu.text.MessageFormat;
40
41 public class RenamePluginProcessor extends RefactoringProcessor {
42     
43     RenamePluginInfo fInfo;
44     
45     public RenamePluginProcessor (RenamePluginInfo info) {
46         fInfo = info;
47     }
48
49     public RefactoringStatus checkFinalConditions(IProgressMonitor pm,
50             CheckConditionsContext context) throws CoreException,
51             OperationCanceledException {
52         RefactoringStatus status = new RefactoringStatus();
53         IResource res = fInfo.getBase().getUnderlyingResource();
54         if (res == null)
55             status.addFatalError(PDEUIMessages.RenamePluginProcessor_externalBundleError);
56         else if (!res.getProject().getFile("META-INF/MANIFEST.MF").exists()) //$NON-NLS-1$
57
status.addFatalError(PDEUIMessages.RenamePluginProcessor_noManifestError);
58         if (fInfo.isRenameProject()) {
59             String JavaDoc newName = fInfo.getNewID();
60             IProject newProject = ResourcesPlugin.getWorkspace().getRoot().getProject(newName);
61             // if destination exists and it is not the same project we are currently trying to rename, show error message
62
if (newProject.exists() && !(res.getProject().equals(newProject)))
63                 status.addFatalError(MessageFormat.format(PDEUIMessages.RenameProjectChange_destinationExists, new String JavaDoc[] {newName}));
64         }
65         return status;
66     }
67
68     public RefactoringStatus checkInitialConditions(IProgressMonitor pm)
69             throws CoreException, OperationCanceledException {
70         return null;
71     }
72
73     public Object JavaDoc[] getElements() {
74         return new Object JavaDoc[] { fInfo.getBase() };
75     }
76
77     public String JavaDoc getIdentifier() {
78         return getClass().getName();
79     }
80
81     public String JavaDoc getProcessorName() {
82         return PDEUIMessages.RenamePluginProcessor_processorName;
83     }
84
85     public boolean isApplicable() throws CoreException {
86         return true;
87     }
88
89     public RefactoringParticipant[] loadParticipants(RefactoringStatus status,
90             SharableParticipants sharedParticipants) throws CoreException {
91         if (fInfo.isRenameProject()) {
92             // filter out PDE's container rename refactor participant. We will already update the Manifest, so we don't need to run our participant
93
IParticipantDescriptorFilter filter = new IParticipantDescriptorFilter() {
94                 static final String JavaDoc PDE_CONTAINER_RENAME_PARTICIPANT = "org.eclipse.pde.ui.manifestFolderRenameParticipant"; //$NON-NLS-1$
95

96                 public boolean select(IConfigurationElement element,
97                         RefactoringStatus status) {
98                     if (PDE_CONTAINER_RENAME_PARTICIPANT.equals(element.getAttribute("id"))) //$NON-NLS-1$
99
return false;
100                     return true;
101                 }
102             };
103             
104             IProject project = fInfo.getBase().getUnderlyingResource().getProject();
105             return ParticipantManager.loadRenameParticipants(status,
106                     this,
107                     project,
108                     new RenameArguments(fInfo.getNewID(), true),
109                     filter,
110                     getAffectedNatures(project),
111                     sharedParticipants);
112         }
113         return new RefactoringParticipant[0];
114     }
115     
116     private String JavaDoc[] getAffectedNatures(IProject project) throws CoreException {
117         // NOTE: JDT searches each dependent project for additional natures
118
return project.getDescription().getNatureIds();
119     }
120     
121     public Change createChange(IProgressMonitor pm) throws CoreException,
122     OperationCanceledException {
123         CompositeChange change = new CompositeChange(MessageFormat.format(PDEUIMessages.RenamePluginProcessor_changeTitle,
124                 new String JavaDoc[] {fInfo.getCurrentID(), fInfo.getNewID()}));
125         pm.beginTask("", getTotalWork()); //$NON-NLS-1$
126
// update manifest with new Id
127
CreateHeaderChangeOperation op = new CreateHeaderChangeOperation(fInfo.getBase(),Constants.BUNDLE_SYMBOLICNAME, fInfo.getCurrentID(), fInfo.getNewID());
128         op.run(new SubProgressMonitor(pm, 1));
129         change.add(op.getChange());
130         
131         if (fInfo.isRenameProject()) {
132             change.add(createProjectChange(new SubProgressMonitor(pm, 1)));
133         }
134         if (fInfo.isUpdateReferences())
135             change.addAll(createReferenceChanges(new SubProgressMonitor(pm, 2)));
136         return change;
137     }
138     
139     private int getTotalWork() {
140         int total = 1;
141         if (fInfo.isRenameProject()) total += 1;
142         if (fInfo.isUpdateReferences()) total += 2;
143         return total;
144     }
145     
146     protected Change createProjectChange(IProgressMonitor monitor) {
147         RenameResourceDescriptor descriptor= new RenameResourceDescriptor();
148         IProject project = fInfo.getBase().getUnderlyingResource().getProject();
149         String JavaDoc newName = fInfo.getNewID();
150         // if project's name is already the same as the destination, then we don't have to do anything to rename project
151
if (project.getName().equals(newName))
152             return null;
153         descriptor.setDescription(MessageFormat.format(PDEUIMessages.RenamePluginProcessor_renameProjectDesc, new String JavaDoc[] { project.getName(), newName }));
154         descriptor.setComment(""); //$NON-NLS-1$
155
descriptor.setFlags(RefactoringDescriptor.STRUCTURAL_CHANGE | RefactoringDescriptor.MULTI_CHANGE | RefactoringDescriptor.BREAKING_CHANGE);
156         descriptor.setResource(project);
157         descriptor.setNewName(fInfo.getNewID());
158         monitor.done();
159         return new RenameProjectChange(descriptor, project, fInfo.getNewID(), null);
160     }
161     
162     protected Change[] createReferenceChanges(IProgressMonitor monitor) throws CoreException {
163         IPluginModelBase currentBase = fInfo.getBase();
164         BundleDescription desc = currentBase.getBundleDescription();
165         if (desc == null) {
166             IPluginModelBase savedBase = PluginRegistry.findModel(currentBase.getUnderlyingResource().getProject());
167             desc = (savedBase != null) ? savedBase.getBundleDescription() : null;
168         }
169         if (desc != null) {
170             FindReferenceOperation op = new FindReferenceOperation(desc, fInfo.getNewID());
171             op.run(monitor);
172             return op.getChanges();
173         }
174         return new Change[0];
175     }
176
177 }
178
Popular Tags