KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 import java.util.ArrayList JavaDoc;
13 import java.util.List JavaDoc;
14
15 import org.eclipse.core.resources.IProject;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.Path;
20 import org.eclipse.debug.core.ILaunchConfiguration;
21 import org.eclipse.jdt.core.IJavaProject;
22 import org.eclipse.jdt.core.IPackageFragmentRoot;
23 import org.eclipse.jdt.core.JavaCore;
24 import org.eclipse.jdt.core.JavaModelException;
25 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
26 import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
27 import org.eclipse.jdt.launching.IVMInstall;
28 import org.eclipse.jdt.launching.JavaRuntime;
29 import org.eclipse.jdt.launching.StandardSourcePathProvider;
30 import org.eclipse.pde.internal.ui.PDEPlugin;
31 import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
32 /**
33  * Generates a source lookup path for Runtime Workbench launch configurations.
34  */

35 public class WorkbenchSourcePathProvider extends StandardSourcePathProvider {
36     /**
37      * @see org.eclipse.jdt.launching.IRuntimeClasspathProvider#computeUnresolvedClasspath(org.eclipse.debug.core.ILaunchConfiguration)
38      */

39     public IRuntimeClasspathEntry[] computeUnresolvedClasspath(
40             ILaunchConfiguration configuration) throws CoreException {
41         boolean defaultPath = configuration.getAttribute(
42                 IJavaLaunchConfigurationConstants.ATTR_DEFAULT_SOURCE_PATH,
43                 true);
44         if (!defaultPath) {
45             return recoverRuntimePath(configuration,
46                     IJavaLaunchConfigurationConstants.ATTR_SOURCE_PATH);
47         }
48         List JavaDoc sourcePath = new ArrayList JavaDoc();
49         // first on the source lookup path, goes the class libraries for the
50
// JRE
51
String JavaDoc vmInstallName = configuration.getAttribute(
52                 IPDELauncherConstants.VMINSTALL, VMHelper
53                         .getDefaultVMInstallName());
54         IVMInstall[] vmInstallations = VMHelper.getAllVMInstances();
55         IVMInstall jre = null;
56         for (int i = 0; i < vmInstallations.length; i++) {
57             if (vmInstallName.equals(vmInstallations[i].getName())) {
58                 jre = vmInstallations[i];
59                 break;
60             }
61         }
62         if (jre != null) {
63             // add container that corresponds to JRE
64
IPath containerPath = new Path(JavaRuntime.JRE_CONTAINER);
65             containerPath = containerPath
66                     .append(jre.getVMInstallType().getId());
67             containerPath = containerPath.append(jre.getName());
68             IRuntimeClasspathEntry entry = JavaRuntime
69                     .newRuntimeContainerClasspathEntry(containerPath,
70                             IRuntimeClasspathEntry.BOOTSTRAP_CLASSES);
71             sourcePath.add(entry);
72         }
73         IProject[] projects = getJavaProjects(configuration);
74         for (int i = 0; i < projects.length; i++) {
75             sourcePath.add(JavaRuntime
76                     .newProjectRuntimeClasspathEntry(JavaCore.create(projects[i])));
77         }
78         return (IRuntimeClasspathEntry[]) sourcePath
79                 .toArray(new IRuntimeClasspathEntry[sourcePath.size()]);
80     }
81     /**
82      * Converts plugin models to java projects
83      */

84     private IProject[] getJavaProjects(ILaunchConfiguration configuration)
85             throws CoreException {
86         IProject[] projects = LaunchPluginValidator.getAffectedProjects(configuration);
87         return PDEPlugin.getWorkspace().computeProjectOrder(projects).projects;
88     }
89     /**
90      * @see IRuntimeClasspathProvider#resolveClasspath(IRuntimeClasspathEntry[],
91      * ILaunchConfiguration)
92      */

93     public IRuntimeClasspathEntry[] resolveClasspath(
94             IRuntimeClasspathEntry[] entries, ILaunchConfiguration configuration)
95             throws CoreException {
96         List JavaDoc all = new ArrayList JavaDoc(entries.length);
97         for (int i = 0; i < entries.length; i++) {
98             if (entries[i].getType() == IRuntimeClasspathEntry.PROJECT) {
99                 // a project resolves to itself for source lookup (rather than
100
// the class file output locations)
101
all.add(entries[i]);
102                 // also add non-JRE libraries
103
IResource resource = entries[i].getResource();
104                 if (resource instanceof IProject) {
105                     IJavaProject project = JavaCore.create((IProject) resource);
106                     IPackageFragmentRoot[] roots = project
107                             .getPackageFragmentRoots();
108                     for (int j = 0; j < roots.length; j++) {
109                         if (roots[j].getKind() == IPackageFragmentRoot.K_BINARY && !isJRELibrary(roots[j])) {
110                             IRuntimeClasspathEntry rte = JavaRuntime
111                                     .newArchiveRuntimeClasspathEntry(roots[j]
112                                             .getPath());
113                             IPath path = roots[j].getSourceAttachmentPath();
114                             if (path != null) {
115                                 rte.setSourceAttachmentPath(path);
116                                 rte.setSourceAttachmentRootPath(roots[j]
117                                         .getSourceAttachmentRootPath());
118                             }
119                             if (!all.contains(rte))
120                                 all.add(rte);
121                         }
122                     }
123                 }
124             } else {
125                 IRuntimeClasspathEntry[] resolved = JavaRuntime
126                         .resolveRuntimeClasspathEntry(entries[i], configuration);
127                 for (int j = 0; j < resolved.length; j++) {
128                     all.add(resolved[j]);
129                 }
130             }
131         }
132         return (IRuntimeClasspathEntry[]) all
133                 .toArray(new IRuntimeClasspathEntry[all.size()]);
134     }
135     private boolean isJRELibrary(IPackageFragmentRoot root) {
136         try {
137             IPath path = root.getRawClasspathEntry().getPath();
138             if (path.equals(new Path(JavaRuntime.JRE_CONTAINER))
139                     || path.equals(new Path(JavaRuntime.JRELIB_VARIABLE))) {
140                 return true;
141             }
142         } catch (JavaModelException e) {
143         }
144         return false;
145     }
146 }
147
Popular Tags