KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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.ArrayList JavaDoc;
14
15 import org.eclipse.core.resources.IWorkspaceRunnable;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.Platform;
19 import org.eclipse.core.runtime.SubProgressMonitor;
20 import org.eclipse.ltk.core.refactoring.Change;
21 import org.eclipse.ltk.core.refactoring.TextFileChange;
22 import org.eclipse.osgi.service.resolver.BundleDescription;
23 import org.eclipse.osgi.service.resolver.BundleSpecification;
24 import org.eclipse.osgi.service.resolver.ExportPackageDescription;
25 import org.eclipse.osgi.service.resolver.StateHelper;
26 import org.eclipse.pde.core.plugin.IFragmentModel;
27 import org.eclipse.pde.core.plugin.IPluginModelBase;
28 import org.eclipse.pde.core.plugin.PluginRegistry;
29 import org.eclipse.pde.internal.core.ICoreConstants;
30 import org.osgi.framework.Constants;
31
32 public class FindReferenceOperation implements IWorkspaceRunnable{
33     
34     private BundleDescription fDesc;
35     private String JavaDoc fNewId;
36     private Change[] fChanges;
37     
38     public FindReferenceOperation(BundleDescription desc, String JavaDoc newId) {
39         fDesc = desc;
40         fNewId = newId;
41     }
42     
43     public void run(IProgressMonitor monitor) throws CoreException {
44         ArrayList JavaDoc list = new ArrayList JavaDoc();
45         if (fDesc != null) {
46             monitor.beginTask("", 3); //$NON-NLS-1$
47
findRequireBundleReferences(list, new SubProgressMonitor(monitor, 1));
48             findFragmentReferences(list, new SubProgressMonitor(monitor, 1));
49             findXFriendReferences(list, new SubProgressMonitor(monitor, 1));
50         }
51         monitor.done();
52         fChanges = (Change[])list.toArray(new Change[list.size()]);
53     }
54     
55     public Change[] getChanges() {
56         return fChanges;
57     }
58     
59     private void findRequireBundleReferences(ArrayList JavaDoc changes, IProgressMonitor monitor) throws CoreException {
60         String JavaDoc oldId = fDesc.getSymbolicName();
61         BundleDescription[] dependents = fDesc.getDependents();
62         monitor.beginTask("", dependents.length); //$NON-NLS-1$
63
for (int i = 0; i < dependents.length; i++) {
64             BundleSpecification[] requires = dependents[i].getRequiredBundles();
65             boolean found = false;
66             for (int j = 0; j < requires.length; j++) {
67                 if (requires[j].getName().equals(oldId)) {
68                     CreateHeaderChangeOperation op = new CreateHeaderChangeOperation(PluginRegistry.findModel(dependents[i]),
69                             Constants.REQUIRE_BUNDLE, oldId, fNewId);
70                     op.run(new SubProgressMonitor(monitor, 1));
71                     TextFileChange change = op.getChange();
72                     if (change != null)
73                         changes.add(change);
74                     found = true;
75                     break;
76                 }
77             }
78             if (!found)
79                 monitor.worked(1);
80         }
81     }
82     
83     private void findFragmentReferences(ArrayList JavaDoc changes, IProgressMonitor monitor) throws CoreException {
84         BundleDescription[] fragments = fDesc.getFragments();
85         monitor.beginTask("", fragments.length); //$NON-NLS-1$
86
String JavaDoc id = fDesc.getSymbolicName();
87         for (int i = 0; i < fragments.length; i++) {
88             IPluginModelBase base = PluginRegistry.findModel(fragments[i]);
89             if (base instanceof IFragmentModel && id.equals(((IFragmentModel)(base)).getFragment().getPluginId())) {
90                 CreateHeaderChangeOperation op = new CreateHeaderChangeOperation(base, Constants.FRAGMENT_HOST,
91                         id, fNewId);
92                 op.run(new SubProgressMonitor(monitor, 1));
93                 TextFileChange change = op.getChange();
94                 if (change != null)
95                     changes.add(change);
96             } else
97                 monitor.worked(1);
98         }
99     }
100     
101     private void findXFriendReferences(ArrayList JavaDoc changes, IProgressMonitor monitor) throws CoreException {
102         StateHelper helper = Platform.getPlatformAdmin().getStateHelper();
103         ExportPackageDescription[] pkgs = helper.getVisiblePackages(fDesc);
104         String JavaDoc id = fDesc.getSymbolicName();
105         monitor.beginTask("", pkgs.length); //$NON-NLS-1$
106
for (int i = 0; i < pkgs.length; i++) {
107             String JavaDoc[] friends = (String JavaDoc[])pkgs[i].getDirective(ICoreConstants.FRIENDS_DIRECTIVE);
108             if (friends != null)
109                 for (int j = 0; j < friends.length; j++) {
110                     if (friends[j].equals(id)) {
111                         CreateHeaderChangeOperation op = new CreateHeaderChangeOperation(PluginRegistry.findModel(pkgs[i].getExporter()),
112                                 Constants.EXPORT_PACKAGE, id, fNewId);
113                         op.run(new SubProgressMonitor(monitor, 1));
114                         TextFileChange change = op.getChange();
115                         if (change != null)
116                             changes.add(change);
117                         break;
118                     }
119                 }
120         }
121     }
122
123 }
124
Popular Tags