KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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 java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.OperationCanceledException;
23 import org.eclipse.jdt.core.IJavaElement;
24 import org.eclipse.ltk.core.refactoring.Change;
25 import org.eclipse.ltk.core.refactoring.CompositeChange;
26 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
27 import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
28 import org.eclipse.ltk.core.refactoring.participants.ISharableParticipant;
29 import org.eclipse.ltk.core.refactoring.participants.RefactoringArguments;
30 import org.eclipse.ltk.core.refactoring.participants.RenameArguments;
31 import org.eclipse.ltk.core.refactoring.participants.RenameParticipant;
32
33 public abstract class PDERenameParticipant extends RenameParticipant implements ISharableParticipant {
34     
35     protected IProject fProject;
36     protected HashMap JavaDoc fElements;
37
38     public RefactoringStatus checkConditions(IProgressMonitor pm,
39             CheckConditionsContext context) throws OperationCanceledException {
40         return new RefactoringStatus();
41     }
42     
43     public void addElement(Object JavaDoc element, RefactoringArguments arguments) {
44         String JavaDoc newName = ((RenameArguments)arguments).getNewName();
45         if (element instanceof IResource) {
46             IPath projectPath = ((IResource)element).getProjectRelativePath();
47             newName = projectPath.removeLastSegments(1).append(newName).toString();
48         }
49         fElements.put(element, newName);
50     }
51
52     public Change createChange(IProgressMonitor pm) throws CoreException,
53             OperationCanceledException {
54         if (!getArguments().getUpdateReferences())
55             return null;
56         CompositeChange result = new CompositeChange(getName());
57         if (updateManifest())
58             addBundleManifestChange(result, pm);
59         if (updateBuildProperties())
60             addBuildPropertiesChange(result, pm);
61         addChange(result, "plugin.xml", pm); //$NON-NLS-1$
62
addChange(result, "fragment.xml", pm); //$NON-NLS-1$
63
return (result.getChildren().length == 0) ? null : result;
64     }
65     
66     private void addChange(CompositeChange result, String JavaDoc filename, IProgressMonitor pm)
67             throws CoreException {
68         IFile file = fProject.getFile(filename);
69         if (file.exists()) {
70             Change change = PluginManifestChange.createRenameChange(
71                     file, fElements.keySet().toArray(), getNewNames(), getTextChange(file), pm);
72             if (change != null)
73                 result.add(change);
74         }
75     }
76     
77     protected String JavaDoc[] getNewNames() {
78         String JavaDoc[] result = new String JavaDoc[fElements.size()];
79         Iterator JavaDoc iter = fElements.values().iterator();
80         for (int i = 0; i < fElements.size(); i++)
81             result[i] = iter.next().toString();
82         return result;
83     }
84
85     protected void addBundleManifestChange(CompositeChange result, IProgressMonitor pm)
86             throws CoreException {
87         addBundleManifestChange(fProject.getFile("META-INF/MANIFEST.MF"), result, pm); //$NON-NLS-1$
88
}
89     
90     protected void addBundleManifestChange(IFile file, CompositeChange result,
91             IProgressMonitor pm) throws CoreException {
92         if (file.exists()) {
93             Change change = BundleManifestChange.createRenameChange(
94                             file,
95                             fElements.keySet().toArray(),
96                             getNewNames(),
97                             pm);
98             if (change != null)
99                 result.add(change);
100         }
101     }
102
103     protected void addBuildPropertiesChange(CompositeChange result, IProgressMonitor pm) throws CoreException {
104         IFile file = fProject.getFile("build.properties"); //$NON-NLS-1$
105
if (file.exists()) {
106             Change change = BuildPropertiesChange.createRenameChange(
107                     file,
108                     fElements.keySet().toArray(),
109                     getNewNames(),
110                     pm);
111             if (change != null)
112                 result.add(change);
113         }
114     }
115     
116     protected boolean updateManifest() {
117         return containsElement(true);
118     }
119     
120     protected boolean updateBuildProperties() {
121         return containsElement(false);
122     }
123     
124     protected boolean containsElement(boolean javaElement) {
125         Object JavaDoc[] objs = fElements.keySet().toArray();
126         for (int i = 0; i < objs.length; i++)
127             if (objs[i] instanceof IJavaElement == javaElement)
128                 return true;
129         return false;
130     }
131 }
132
Popular Tags