001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 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:
014: import org.eclipse.core.filebuffers.FileBuffers;
015: import org.eclipse.core.filebuffers.LocationKind;
016: import org.eclipse.core.resources.IContainer;
017: import org.eclipse.core.resources.IFile;
018: import org.eclipse.core.resources.IProject;
019: import org.eclipse.core.resources.IWorkspaceRoot;
020: import org.eclipse.core.runtime.CoreException;
021: import org.eclipse.core.runtime.IPath;
022: import org.eclipse.core.runtime.IProgressMonitor;
023: import org.eclipse.core.runtime.OperationCanceledException;
024: import org.eclipse.core.runtime.SubProgressMonitor;
025: import org.eclipse.jface.text.BadLocationException;
026: import org.eclipse.ltk.core.refactoring.Change;
027: import org.eclipse.ltk.core.refactoring.CompositeChange;
028: import org.eclipse.pde.core.plugin.PluginRegistry;
029: import org.eclipse.pde.internal.core.WorkspaceModelManager;
030: import org.eclipse.pde.internal.core.ibundle.IBundle;
031: import org.eclipse.pde.internal.core.text.bundle.BundleModel;
032: import org.eclipse.pde.internal.core.text.bundle.BundleSymbolicNameHeader;
033: import org.eclipse.pde.internal.core.text.bundle.BundleTextChangeListener;
034: import org.eclipse.pde.internal.core.util.IdUtil;
035: import org.eclipse.pde.internal.ui.PDEUIMessages;
036: import org.eclipse.pde.internal.ui.util.PDEModelUtility;
037: import org.eclipse.text.edits.MalformedTreeException;
038: import org.eclipse.text.edits.MultiTextEdit;
039: import org.osgi.framework.Constants;
040:
041: public class ContainerRenameParticipant extends PDERenameParticipant {
042:
043: public String getName() {
044: return PDEUIMessages.ContainerRenameParticipant_renameFolders;
045: }
046:
047: protected boolean initialize(Object element) {
048: if (element instanceof IContainer) {
049: IProject project = ((IContainer) element).getProject();
050: if (WorkspaceModelManager.isPluginProject(project)) {
051: IPath path = ((IContainer) element)
052: .getProjectRelativePath().removeLastSegments(1);
053: String newName = path.append(
054: getArguments().getNewName()).toString();
055: fProject = project;
056: fElements = new HashMap();
057: fElements.put(element, newName);
058: return true;
059: }
060: }
061: return false;
062: }
063:
064: public Change createChange(IProgressMonitor pm)
065: throws CoreException, OperationCanceledException {
066: // for the special case of a project rename, we need to only check the manifest for changes
067: if (fElements.size() == 1
068: && fElements.keySet().iterator().next() instanceof IProject) {
069: if (!getArguments().getUpdateReferences())
070: return null;
071: return createManifestChange(pm);
072: }
073: return super .createChange(pm);
074: }
075:
076: protected Change createManifestChange(IProgressMonitor monitor)
077: throws CoreException {
078: IFile manifest = fProject.getFile("META-INF/MANIFEST.MF"); //$NON-NLS-1$
079: if (manifest.exists()) {
080: monitor.beginTask("", 4); //$NON-NLS-1$
081: try {
082: String newText = (String) fElements.get(fProject);
083: CompositeChange result = new CompositeChange(
084: PDEUIMessages.ContainerRenameParticipant_renameBundleId);
085: IBundle bundle = BundleManifestChange.getBundle(
086: manifest, new SubProgressMonitor(monitor, 1));
087: if (bundle != null) {
088: BundleTextChangeListener listener = new BundleTextChangeListener(
089: ((BundleModel) bundle.getModel())
090: .getDocument());
091: bundle.getModel().addModelChangedListener(listener);
092:
093: BundleSymbolicNameHeader header = (BundleSymbolicNameHeader) bundle
094: .getManifestHeader(Constants.BUNDLE_SYMBOLICNAME);
095: // can't check the id to the project name. Must run Id calculation code incase project name has invalid OSGi chars
096: String calcProjectId = IdUtil.getValidId(fProject
097: .getName());
098: String oldText = header.getId();
099: // don't update Bundle-SymbolicName if the id and project name don't match
100: if (!oldText.equals(calcProjectId))
101: return null;
102: // remember to create a valid OSGi Bundle-SymbolicName. Project name does not have that garuntee
103: String newId = IdUtil.getValidId(newText);
104: header.setId(newId);
105: // at this point, neither the project or file will exist.
106: // The project/resources get refactored before the TextChange is applied, therefore we need their future locations
107: IProject newProject = ((IWorkspaceRoot) manifest
108: .getProject().getParent())
109: .getProject(newText);
110:
111: MovedTextFileChange change = new MovedTextFileChange(
112: "", newProject.getFile("META-INF/MANIFEST.MF"), manifest); //$NON-NLS-1$ //$NON-NLS-2$
113: MultiTextEdit edit = new MultiTextEdit();
114: edit.addChildren(listener.getTextOperations());
115: change.setEdit(edit);
116: PDEModelUtility.setChangeTextType(change, manifest);
117: result.add(change);
118:
119: // find all the references to the changing Bundle-SymbolicName and update all references to it
120: FindReferenceOperation op = new FindReferenceOperation(
121: PluginRegistry.findModel(fProject)
122: .getBundleDescription(), newId);
123: op.run(new SubProgressMonitor(monitor, 2));
124: result.addAll(op.getChanges());
125: return result;
126: }
127: } catch (CoreException e) {
128: } catch (MalformedTreeException e) {
129: } catch (BadLocationException e) {
130: } finally {
131: FileBuffers.getTextFileBufferManager().disconnect(
132: manifest.getFullPath(), LocationKind.NORMALIZE,
133: new SubProgressMonitor(monitor, 1));
134: monitor.done();
135: }
136: }
137: return null;
138: }
139:
140: }
|