KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > launcher > SWTSourcePathComputer


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.launcher;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.debug.core.ILaunchConfiguration;
23 import org.eclipse.debug.core.sourcelookup.ISourceContainer;
24 import org.eclipse.debug.core.sourcelookup.containers.ExternalArchiveSourceContainer;
25 import org.eclipse.jdt.core.IClasspathEntry;
26 import org.eclipse.jdt.core.IJavaProject;
27 import org.eclipse.jdt.core.IPackageFragmentRoot;
28 import org.eclipse.jdt.core.JavaCore;
29 import org.eclipse.jdt.launching.sourcelookup.containers.JavaProjectSourceContainer;
30 import org.eclipse.jdt.launching.sourcelookup.containers.JavaSourcePathComputer;
31 import org.eclipse.jdt.launching.sourcelookup.containers.PackageFragmentRootSourceContainer;
32 import org.eclipse.osgi.service.resolver.BundleDescription;
33 import org.eclipse.pde.core.plugin.IFragment;
34 import org.eclipse.pde.core.plugin.IFragmentModel;
35 import org.eclipse.pde.core.plugin.IPluginLibrary;
36 import org.eclipse.pde.internal.core.ClasspathUtilCore;
37 import org.eclipse.pde.internal.core.PDECore;
38 import org.eclipse.pde.internal.core.SourceLocationManager;
39
40 public class SWTSourcePathComputer extends JavaSourcePathComputer {
41     
42     private static final String JavaDoc ID = "org.eclipse.pde.ui.swtSourcePathComputer"; //$NON-NLS-1$
43

44     public String JavaDoc getId() {
45         return ID;
46     }
47     
48     public ISourceContainer[] computeSourceContainers(ILaunchConfiguration configuration, IProgressMonitor monitor) throws CoreException {
49         ISourceContainer[] containers = super.computeSourceContainers(configuration, monitor);
50         ArrayList JavaDoc extra = new ArrayList JavaDoc();
51
52         BundleDescription[] fragments = SWTLaunchConfiguration.findFragments();
53         
54         for (int j = 0; j < fragments.length; j++) {
55             if (!fragments[j].isResolved())
56                 continue;
57             
58             IFragmentModel model = PDECore.getDefault().getModelManager().findFragmentModel(fragments[j].getSymbolicName());
59             IFragment fragment = model != null ? model.getFragment() : null;
60             if (fragment == null)
61                 return containers;
62             
63             IPath fragmentPath = new Path(fragment.getModel().getInstallLocation());
64             for (int i = 0; i < containers.length; i++) {
65                 if (containers[i] instanceof PackageFragmentRootSourceContainer) {
66                     PackageFragmentRootSourceContainer container = (PackageFragmentRootSourceContainer)containers[i];
67                     IPackageFragmentRoot root = container.getPackageFragmentRoot();
68                     if (root.getSourceAttachmentPath() == null) {
69                         int matchCount = fragmentPath.matchingFirstSegments(root.getPath());
70                         if (matchCount == fragmentPath.segmentCount()) {
71                             IPath libPath = root.getPath().removeFirstSegments(matchCount);
72                             String JavaDoc libLocation = getLibrarySourceLocation(fragment, libPath);
73                             if (libLocation != null) {
74                                 containers[i] = new ExternalArchiveSourceContainer(libLocation, false);
75                             }
76                         }
77                     }
78                 }
79             }
80             IResource resource = fragment.getModel().getUnderlyingResource();
81             if (resource != null) {
82                 IProject project = resource.getProject();
83                 if (project.hasNature(JavaCore.NATURE_ID)) {
84                     IJavaProject jProject = JavaCore.create(project);
85                     extra.add(new JavaProjectSourceContainer(jProject));
86                     IPackageFragmentRoot[] roots = jProject.getPackageFragmentRoots();
87                     for (int i = 0; i < roots.length; i++) {
88                         if (roots[i].getKind() == IPackageFragmentRoot.K_BINARY
89                                 && roots[i].getRawClasspathEntry().getEntryKind() == IClasspathEntry.CPE_LIBRARY)
90                             extra.add(new PackageFragmentRootSourceContainer(roots[i]));
91                     }
92                 }
93             } else {
94                 IPluginLibrary[] libraries = fragment.getLibraries();
95                 for (int i = 0; i < libraries.length; i++) {
96                     String JavaDoc name = ClasspathUtilCore.expandLibraryName(libraries[i].getName());
97                     String JavaDoc location = getLibrarySourceLocation(fragment, new Path(name));
98                     if (location != null)
99                         extra.add(new ExternalArchiveSourceContainer(location, false));
100                 }
101             }
102         }
103         
104         if (extra.size() > 0) {
105             ISourceContainer[] all = new ISourceContainer[containers.length + extra.size()];
106             System.arraycopy(containers, 0, all, 0, containers.length);
107             for (int i = 0; i < extra.size(); i++) {
108                 all[i+containers.length] = (ISourceContainer) extra.get(i);
109             }
110             return all;
111         }
112         return containers;
113     }
114     
115     private String JavaDoc getLibrarySourceLocation(IFragment fragment, IPath path) {
116         String JavaDoc library = path.segmentCount() == 0 ? "." : path.setDevice(null).toString(); //$NON-NLS-1$
117
SourceLocationManager manager = PDECore.getDefault().getSourceLocationManager();
118         int dot = library.lastIndexOf('.');
119         if (dot != -1) {
120             library = library.substring(0, dot) + "src.zip"; //$NON-NLS-1$
121
}
122         File JavaDoc file = manager.findSourceFile(fragment, new Path(library));
123         return file == null ? null : file.getAbsolutePath();
124     }
125 }
126
Popular Tags