1 11 package org.eclipse.pde.internal.ui.refactoring; 12 13 import java.util.HashMap ; 14 15 import org.eclipse.core.filebuffers.FileBuffers; 16 import org.eclipse.core.filebuffers.LocationKind; 17 import org.eclipse.core.resources.IContainer; 18 import org.eclipse.core.resources.IFile; 19 import org.eclipse.core.resources.IProject; 20 import org.eclipse.core.resources.IWorkspaceRoot; 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IPath; 23 import org.eclipse.core.runtime.IProgressMonitor; 24 import org.eclipse.core.runtime.OperationCanceledException; 25 import org.eclipse.core.runtime.SubProgressMonitor; 26 import org.eclipse.jface.text.BadLocationException; 27 import org.eclipse.ltk.core.refactoring.Change; 28 import org.eclipse.ltk.core.refactoring.CompositeChange; 29 import org.eclipse.pde.core.plugin.PluginRegistry; 30 import org.eclipse.pde.internal.core.WorkspaceModelManager; 31 import org.eclipse.pde.internal.core.ibundle.IBundle; 32 import org.eclipse.pde.internal.core.text.bundle.BundleModel; 33 import org.eclipse.pde.internal.core.text.bundle.BundleSymbolicNameHeader; 34 import org.eclipse.pde.internal.core.text.bundle.BundleTextChangeListener; 35 import org.eclipse.pde.internal.core.util.IdUtil; 36 import org.eclipse.pde.internal.ui.PDEUIMessages; 37 import org.eclipse.text.edits.MalformedTreeException; 38 import org.eclipse.text.edits.MultiTextEdit; 39 import org.osgi.framework.Constants; 40 41 public class ContainerRenameParticipant extends PDERenameParticipant { 42 43 public String getName() { 44 return PDEUIMessages.ContainerRenameParticipant_renameFolders; 45 } 46 47 protected boolean initialize(Object element) { 48 if (element instanceof IContainer) { 49 IProject project = ((IContainer)element).getProject(); 50 if (WorkspaceModelManager.isPluginProject(project)) { 51 IPath path = ((IContainer)element).getProjectRelativePath().removeLastSegments(1); 52 String newName = path.append(getArguments().getNewName()).toString(); 53 fProject = project; 54 fElements = new HashMap (); 55 fElements.put(element, newName); 56 return true; 57 } 58 } 59 return false; 60 } 61 62 public Change createChange(IProgressMonitor pm) throws CoreException, 63 OperationCanceledException { 64 if (fElements.size() == 1 && fElements.keySet().iterator().next() instanceof IProject) { 66 if (!getArguments().getUpdateReferences()) 67 return null; 68 return createManifestChange(pm); 69 } 70 return super.createChange(pm); 71 } 72 73 protected Change createManifestChange(IProgressMonitor monitor) throws CoreException { 74 IFile manifest = fProject.getFile("META-INF/MANIFEST.MF"); if (manifest.exists()) { 76 monitor.beginTask("", 4); try { 78 String newText = (String )fElements.get(fProject); 79 CompositeChange result = new CompositeChange(PDEUIMessages.ContainerRenameParticipant_renameBundleId); 80 IBundle bundle = BundleManifestChange.getBundle(manifest, new SubProgressMonitor(monitor, 1)); 81 if (bundle != null) { 82 BundleTextChangeListener listener = new BundleTextChangeListener(((BundleModel)bundle.getModel()).getDocument()); 83 bundle.getModel().addModelChangedListener(listener); 84 85 BundleSymbolicNameHeader header = (BundleSymbolicNameHeader)bundle.getManifestHeader(Constants.BUNDLE_SYMBOLICNAME); 86 String calcProjectId = IdUtil.getValidId(fProject.getName()); 88 String oldText = header.getId(); 89 if (!oldText.equals(calcProjectId)) 91 return null; 92 String newId = IdUtil.getValidId(newText); 94 header.setId(newId); 95 IProject newProject = ((IWorkspaceRoot)manifest.getProject().getParent()).getProject(newText); 98 99 MovedTextFileChange change = new MovedTextFileChange("", newProject.getFile("META-INF/MANIFEST.MF"), manifest); MultiTextEdit edit = new MultiTextEdit(); 101 edit.addChildren(listener.getTextOperations()); 102 change.setEdit(edit); 103 result.add(change); 104 105 FindReferenceOperation op = new FindReferenceOperation(PluginRegistry.findModel(fProject).getBundleDescription(), newId); 107 op.run(new SubProgressMonitor(monitor, 2)); 108 result.addAll(op.getChanges()); 109 return result; 110 } 111 } catch (CoreException e) { 112 } catch (MalformedTreeException e) { 113 } catch (BadLocationException e) { 114 } finally { 115 FileBuffers.getTextFileBufferManager().disconnect(manifest.getFullPath(), LocationKind.NORMALIZE, new SubProgressMonitor(monitor, 1)); 116 monitor.done(); 117 } 118 } 119 return null; 120 } 121 122 } 123 | Popular Tags |