KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 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 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.runtime.CoreException;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.jdt.core.IJavaElement;
21 import org.eclipse.jdt.core.IJavaProject;
22 import org.eclipse.jdt.core.IPackageFragment;
23 import org.eclipse.jdt.core.JavaModelException;
24 import org.eclipse.ltk.core.refactoring.CompositeChange;
25 import org.eclipse.osgi.service.resolver.BaseDescription;
26 import org.eclipse.osgi.service.resolver.BundleDescription;
27 import org.eclipse.osgi.service.resolver.ExportPackageDescription;
28 import org.eclipse.osgi.service.resolver.ImportPackageSpecification;
29 import org.eclipse.pde.core.plugin.IPluginModelBase;
30 import org.eclipse.pde.core.plugin.PluginRegistry;
31 import org.eclipse.pde.internal.core.WorkspaceModelManager;
32 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
33 import org.eclipse.pde.internal.ui.PDEUIMessages;
34
35 public class ManifestPackageRenameParticipant extends PDERenameParticipant {
36     
37     protected boolean initialize(Object JavaDoc element) {
38         try {
39             if (element instanceof IPackageFragment) {
40                 IPackageFragment fragment = (IPackageFragment)element;
41                 if (!fragment.containsJavaResources())
42                     return false;
43                 IJavaProject javaProject = (IJavaProject)fragment.getAncestor(IJavaElement.JAVA_PROJECT);
44                 IProject project = javaProject.getProject();
45                 if (WorkspaceModelManager.isPluginProject(project)) {
46                     fProject = javaProject.getProject();
47                     fElements = new HashMap JavaDoc();
48                     fElements.put(fragment, getArguments().getNewName());
49                     return true;
50                 }
51             }
52         } catch (JavaModelException e) {
53         }
54         return false;
55     }
56
57     public String JavaDoc getName() {
58         return PDEUIMessages.ManifestPackageRenameParticipant_packageRename;
59     }
60     
61     protected void addBundleManifestChange(CompositeChange result, IProgressMonitor pm) throws CoreException {
62         super.addBundleManifestChange(result, pm);
63         IPluginModelBase model = PluginRegistry.findModel(fProject);
64         if (model != null) {
65             BundleDescription desc = model.getBundleDescription();
66             if (desc != null) {
67                 BundleDescription[] dependents = desc.getDependents();
68                 for (int i = 0; i < dependents.length; i++) {
69                     if (isAffected(desc, dependents[i])) {
70                         IPluginModelBase candidate = PluginRegistry.findModel(dependents[i]);
71                         if (candidate instanceof IBundlePluginModelBase) {
72                             IFile file = (IFile)candidate.getUnderlyingResource();
73                             addBundleManifestChange(file, result, pm);
74                         }
75                     }
76                 }
77             }
78         }
79     }
80     
81     private boolean isAffected(BundleDescription desc, BundleDescription dependent) {
82         ImportPackageSpecification[] imports = dependent.getImportPackages();
83         Iterator JavaDoc iter = fElements.keySet().iterator();
84         while (iter.hasNext()) {
85             String JavaDoc name = ((IJavaElement)iter.next()).getElementName();
86             for (int i = 0; i < imports.length; i++) {
87                 if (name.equals(imports[i].getName())) {
88                     BaseDescription supplier = imports[i].getSupplier();
89                     if (supplier instanceof ExportPackageDescription) {
90                         if (desc.equals(((ExportPackageDescription)supplier).getExporter()))
91                             return true;
92                     }
93                 }
94             }
95         }
96         return false;
97     }
98
99 }
100
Popular Tags