001: /*******************************************************************************
002: * Copyright (c) 2005, 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: import java.util.Iterator;
014:
015: import org.eclipse.core.resources.IFile;
016: import org.eclipse.core.resources.IProject;
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.IProgressMonitor;
019: import org.eclipse.jdt.core.IJavaElement;
020: import org.eclipse.jdt.core.IJavaProject;
021: import org.eclipse.jdt.core.IPackageFragment;
022: import org.eclipse.jdt.core.JavaModelException;
023: import org.eclipse.ltk.core.refactoring.CompositeChange;
024: import org.eclipse.osgi.service.resolver.BaseDescription;
025: import org.eclipse.osgi.service.resolver.BundleDescription;
026: import org.eclipse.osgi.service.resolver.ExportPackageDescription;
027: import org.eclipse.osgi.service.resolver.ImportPackageSpecification;
028: import org.eclipse.pde.core.plugin.IPluginModelBase;
029: import org.eclipse.pde.core.plugin.PluginRegistry;
030: import org.eclipse.pde.internal.core.WorkspaceModelManager;
031: import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
032: import org.eclipse.pde.internal.ui.PDEUIMessages;
033:
034: public class ManifestPackageRenameParticipant extends
035: PDERenameParticipant {
036:
037: protected boolean initialize(Object element) {
038: try {
039: if (element instanceof IPackageFragment) {
040: IPackageFragment fragment = (IPackageFragment) element;
041: if (!fragment.containsJavaResources())
042: return false;
043: IJavaProject javaProject = (IJavaProject) fragment
044: .getAncestor(IJavaElement.JAVA_PROJECT);
045: IProject project = javaProject.getProject();
046: if (WorkspaceModelManager.isPluginProject(project)) {
047: fProject = javaProject.getProject();
048: fElements = new HashMap();
049: fElements
050: .put(fragment, getArguments().getNewName());
051: return true;
052: }
053: }
054: } catch (JavaModelException e) {
055: }
056: return false;
057: }
058:
059: public String getName() {
060: return PDEUIMessages.ManifestPackageRenameParticipant_packageRename;
061: }
062:
063: protected void addBundleManifestChange(CompositeChange result,
064: IProgressMonitor pm) throws CoreException {
065: super .addBundleManifestChange(result, pm);
066: IPluginModelBase model = PluginRegistry.findModel(fProject);
067: if (model != null) {
068: BundleDescription desc = model.getBundleDescription();
069: if (desc != null) {
070: BundleDescription[] dependents = desc.getDependents();
071: for (int i = 0; i < dependents.length; i++) {
072: if (isAffected(desc, dependents[i])) {
073: IPluginModelBase candidate = PluginRegistry
074: .findModel(dependents[i]);
075: if (candidate instanceof IBundlePluginModelBase) {
076: IFile file = (IFile) candidate
077: .getUnderlyingResource();
078: addBundleManifestChange(file, result, pm);
079: }
080: }
081: }
082: }
083: }
084: }
085:
086: private boolean isAffected(BundleDescription desc,
087: BundleDescription dependent) {
088: ImportPackageSpecification[] imports = dependent
089: .getImportPackages();
090: Iterator iter = fElements.keySet().iterator();
091: while (iter.hasNext()) {
092: String name = ((IJavaElement) iter.next()).getElementName();
093: for (int i = 0; i < imports.length; i++) {
094: if (name.equals(imports[i].getName())) {
095: BaseDescription supplier = imports[i].getSupplier();
096: if (supplier instanceof ExportPackageDescription) {
097: if (desc
098: .equals(((ExportPackageDescription) supplier)
099: .getExporter()))
100: return true;
101: }
102: }
103: }
104: }
105: return false;
106: }
107:
108: }
|