001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.refactoring;
011:
012: import java.util.HashMap;
013: import java.util.Iterator;
014:
015: import org.eclipse.core.resources.IFile;
016: import org.eclipse.core.resources.IProject;
017: import org.eclipse.core.resources.IResource;
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.IPath;
020: import org.eclipse.core.runtime.IProgressMonitor;
021: import org.eclipse.core.runtime.OperationCanceledException;
022: import org.eclipse.jdt.core.IJavaElement;
023: import org.eclipse.ltk.core.refactoring.Change;
024: import org.eclipse.ltk.core.refactoring.CompositeChange;
025: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
026: import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
027: import org.eclipse.ltk.core.refactoring.participants.ISharableParticipant;
028: import org.eclipse.ltk.core.refactoring.participants.RefactoringArguments;
029: import org.eclipse.ltk.core.refactoring.participants.RenameArguments;
030: import org.eclipse.ltk.core.refactoring.participants.RenameParticipant;
031:
032: public abstract class PDERenameParticipant extends RenameParticipant
033: implements ISharableParticipant {
034:
035: protected IProject fProject;
036: protected HashMap fElements;
037:
038: public RefactoringStatus checkConditions(IProgressMonitor pm,
039: CheckConditionsContext context)
040: throws OperationCanceledException {
041: return new RefactoringStatus();
042: }
043:
044: public void addElement(Object element,
045: RefactoringArguments arguments) {
046: String newName = ((RenameArguments) arguments).getNewName();
047: if (element instanceof IResource) {
048: IPath projectPath = ((IResource) element)
049: .getProjectRelativePath();
050: newName = projectPath.removeLastSegments(1).append(newName)
051: .toString();
052: }
053: fElements.put(element, newName);
054: }
055:
056: public Change createChange(IProgressMonitor pm)
057: throws CoreException, OperationCanceledException {
058: if (!getArguments().getUpdateReferences())
059: return null;
060: CompositeChange result = new CompositeChange(getName());
061: if (updateManifest())
062: addBundleManifestChange(result, pm);
063: if (updateBuildProperties())
064: addBuildPropertiesChange(result, pm);
065: addChange(result, "plugin.xml", pm); //$NON-NLS-1$
066: addChange(result, "fragment.xml", pm); //$NON-NLS-1$
067: return (result.getChildren().length == 0) ? null : result;
068: }
069:
070: private void addChange(CompositeChange result, String filename,
071: IProgressMonitor pm) throws CoreException {
072: IFile file = fProject.getFile(filename);
073: if (file.exists()) {
074: Change change = PluginManifestChange.createRenameChange(
075: file, fElements.keySet().toArray(), getNewNames(),
076: getTextChange(file), pm);
077: if (change != null)
078: result.add(change);
079: }
080: }
081:
082: protected String[] getNewNames() {
083: String[] result = new String[fElements.size()];
084: Iterator iter = fElements.values().iterator();
085: for (int i = 0; i < fElements.size(); i++)
086: result[i] = iter.next().toString();
087: return result;
088: }
089:
090: protected void addBundleManifestChange(CompositeChange result,
091: IProgressMonitor pm) throws CoreException {
092: addBundleManifestChange(fProject
093: .getFile("META-INF/MANIFEST.MF"), result, pm); //$NON-NLS-1$
094: }
095:
096: protected void addBundleManifestChange(IFile file,
097: CompositeChange result, IProgressMonitor pm)
098: throws CoreException {
099: if (file.exists()) {
100: Change change = BundleManifestChange.createRenameChange(
101: file, fElements.keySet().toArray(), getNewNames(),
102: pm);
103: if (change != null)
104: result.add(change);
105: }
106: }
107:
108: protected void addBuildPropertiesChange(CompositeChange result,
109: IProgressMonitor pm) throws CoreException {
110: IFile file = fProject.getFile("build.properties"); //$NON-NLS-1$
111: if (file.exists()) {
112: Change change = BuildPropertiesChange.createRenameChange(
113: file, fElements.keySet().toArray(), getNewNames(),
114: pm);
115: if (change != null)
116: result.add(change);
117: }
118: }
119:
120: protected boolean updateManifest() {
121: return containsElement(true);
122: }
123:
124: protected boolean updateBuildProperties() {
125: return containsElement(false);
126: }
127:
128: protected boolean containsElement(boolean javaElement) {
129: Object[] objs = fElements.keySet().toArray();
130: for (int i = 0; i < objs.length; i++)
131: if (objs[i] instanceof IJavaElement == javaElement)
132: return true;
133: return false;
134: }
135: }
|