KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006, 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 java.util.HashMap JavaDoc;
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 JavaDoc getName() {
44         return PDEUIMessages.ContainerRenameParticipant_renameFolders;
45     }
46
47     protected boolean initialize(Object JavaDoc 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 JavaDoc newName = path.append(getArguments().getNewName()).toString();
53                 fProject = project;
54                 fElements = new HashMap JavaDoc();
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         // for the special case of a project rename, we need to only check the manifest for changes
65
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"); //$NON-NLS-1$
75
if (manifest.exists()) {
76             monitor.beginTask("", 4); //$NON-NLS-1$
77
try {
78                 String JavaDoc newText = (String JavaDoc)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                     // can't check the id to the project name. Must run Id calculation code incase project name has invalid OSGi chars
87
String JavaDoc calcProjectId = IdUtil.getValidId(fProject.getName());
88                     String JavaDoc oldText = header.getId();
89                     // don't update Bundle-SymbolicName if the id and project name don't match
90
if (!oldText.equals(calcProjectId))
91                         return null;
92                     // remember to create a valid OSGi Bundle-SymbolicName. Project name does not have that garuntee
93
String JavaDoc newId = IdUtil.getValidId(newText);
94                     header.setId(newId);
95                     // at this point, neither the project or file will exist.
96
// The project/resources get refactored before the TextChange is applied, therefore we need their future locations
97
IProject newProject = ((IWorkspaceRoot)manifest.getProject().getParent()).getProject(newText);
98
99                     MovedTextFileChange change = new MovedTextFileChange("", newProject.getFile("META-INF/MANIFEST.MF"), manifest); //$NON-NLS-1$ //$NON-NLS-2$
100
MultiTextEdit edit = new MultiTextEdit();
101                     edit.addChildren(listener.getTextOperations());
102                     change.setEdit(edit);
103                     result.add(change);
104                     
105                     // find all the references to the changing Bundle-SymbolicName and update all references to it
106
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