1 11 package org.eclipse.pde.internal.ui.refactoring; 12 13 import java.util.ArrayList ; 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 fNewId; 36 private Change[] fChanges; 37 38 public FindReferenceOperation(BundleDescription desc, String newId) { 39 fDesc = desc; 40 fNewId = newId; 41 } 42 43 public void run(IProgressMonitor monitor) throws CoreException { 44 ArrayList list = new ArrayList (); 45 if (fDesc != null) { 46 monitor.beginTask("", 3); 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 changes, IProgressMonitor monitor) throws CoreException { 60 String oldId = fDesc.getSymbolicName(); 61 BundleDescription[] dependents = fDesc.getDependents(); 62 monitor.beginTask("", dependents.length); 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 changes, IProgressMonitor monitor) throws CoreException { 84 BundleDescription[] fragments = fDesc.getFragments(); 85 monitor.beginTask("", fragments.length); String 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 changes, IProgressMonitor monitor) throws CoreException { 102 StateHelper helper = Platform.getPlatformAdmin().getStateHelper(); 103 ExportPackageDescription[] pkgs = helper.getVisiblePackages(fDesc); 104 String id = fDesc.getSymbolicName(); 105 monitor.beginTask("", pkgs.length); for (int i = 0; i < pkgs.length; i++) { 107 String [] friends = (String [])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 |