001: /*******************************************************************************
002: * Copyright (c) 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.ArrayList;
013:
014: import org.eclipse.core.resources.IWorkspaceRunnable;
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.core.runtime.IProgressMonitor;
017: import org.eclipse.core.runtime.Platform;
018: import org.eclipse.core.runtime.SubProgressMonitor;
019: import org.eclipse.ltk.core.refactoring.Change;
020: import org.eclipse.ltk.core.refactoring.TextFileChange;
021: import org.eclipse.osgi.service.resolver.BundleDescription;
022: import org.eclipse.osgi.service.resolver.BundleSpecification;
023: import org.eclipse.osgi.service.resolver.ExportPackageDescription;
024: import org.eclipse.osgi.service.resolver.StateHelper;
025: import org.eclipse.pde.core.plugin.IFragmentModel;
026: import org.eclipse.pde.core.plugin.IPluginModelBase;
027: import org.eclipse.pde.core.plugin.PluginRegistry;
028: import org.eclipse.pde.internal.core.ICoreConstants;
029: import org.osgi.framework.Constants;
030:
031: public class FindReferenceOperation implements IWorkspaceRunnable {
032:
033: private BundleDescription fDesc;
034: private String fNewId;
035: private Change[] fChanges;
036:
037: public FindReferenceOperation(BundleDescription desc, String newId) {
038: fDesc = desc;
039: fNewId = newId;
040: }
041:
042: public void run(IProgressMonitor monitor) throws CoreException {
043: ArrayList list = new ArrayList();
044: if (fDesc != null) {
045: monitor.beginTask("", 3); //$NON-NLS-1$
046: findRequireBundleReferences(list, new SubProgressMonitor(
047: monitor, 1));
048: findFragmentReferences(list, new SubProgressMonitor(
049: monitor, 1));
050: findXFriendReferences(list, new SubProgressMonitor(monitor,
051: 1));
052: }
053: monitor.done();
054: fChanges = (Change[]) list.toArray(new Change[list.size()]);
055: }
056:
057: public Change[] getChanges() {
058: return fChanges;
059: }
060:
061: private void findRequireBundleReferences(ArrayList changes,
062: IProgressMonitor monitor) throws CoreException {
063: String oldId = fDesc.getSymbolicName();
064: BundleDescription[] dependents = fDesc.getDependents();
065: monitor.beginTask("", dependents.length); //$NON-NLS-1$
066: for (int i = 0; i < dependents.length; i++) {
067: BundleSpecification[] requires = dependents[i]
068: .getRequiredBundles();
069: boolean found = false;
070: for (int j = 0; j < requires.length; j++) {
071: if (requires[j].getName().equals(oldId)) {
072: CreateHeaderChangeOperation op = new CreateHeaderChangeOperation(
073: PluginRegistry.findModel(dependents[i]),
074: Constants.REQUIRE_BUNDLE, oldId, fNewId);
075: op.run(new SubProgressMonitor(monitor, 1));
076: TextFileChange change = op.getChange();
077: if (change != null)
078: changes.add(change);
079: found = true;
080: break;
081: }
082: }
083: if (!found)
084: monitor.worked(1);
085: }
086: }
087:
088: private void findFragmentReferences(ArrayList changes,
089: IProgressMonitor monitor) throws CoreException {
090: BundleDescription[] fragments = fDesc.getFragments();
091: monitor.beginTask("", fragments.length); //$NON-NLS-1$
092: String id = fDesc.getSymbolicName();
093: for (int i = 0; i < fragments.length; i++) {
094: IPluginModelBase base = PluginRegistry
095: .findModel(fragments[i]);
096: if (base instanceof IFragmentModel
097: && id.equals(((IFragmentModel) (base))
098: .getFragment().getPluginId())) {
099: CreateHeaderChangeOperation op = new CreateHeaderChangeOperation(
100: base, Constants.FRAGMENT_HOST, id, fNewId);
101: op.run(new SubProgressMonitor(monitor, 1));
102: TextFileChange change = op.getChange();
103: if (change != null)
104: changes.add(change);
105: } else
106: monitor.worked(1);
107: }
108: }
109:
110: private void findXFriendReferences(ArrayList changes,
111: IProgressMonitor monitor) throws CoreException {
112: StateHelper helper = Platform.getPlatformAdmin()
113: .getStateHelper();
114: ExportPackageDescription[] pkgs = helper
115: .getVisiblePackages(fDesc);
116: String id = fDesc.getSymbolicName();
117: monitor.beginTask("", pkgs.length); //$NON-NLS-1$
118: for (int i = 0; i < pkgs.length; i++) {
119: String[] friends = (String[]) pkgs[i]
120: .getDirective(ICoreConstants.FRIENDS_DIRECTIVE);
121: if (friends != null)
122: for (int j = 0; j < friends.length; j++) {
123: if (friends[j].equals(id)) {
124: CreateHeaderChangeOperation op = new CreateHeaderChangeOperation(
125: PluginRegistry.findModel(pkgs[i]
126: .getExporter()),
127: Constants.EXPORT_PACKAGE, id, fNewId);
128: op.run(new SubProgressMonitor(monitor, 1));
129: TextFileChange change = op.getChange();
130: if (change != null)
131: changes.add(change);
132: break;
133: }
134: }
135: }
136: }
137:
138: }
|