KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > launching > sourcelookup > containers > ClasspathContainerSourceContainer


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 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.jdt.launching.sourcelookup.containers;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IPath;
15 import org.eclipse.debug.core.ILaunchConfiguration;
16 import org.eclipse.debug.core.sourcelookup.ISourceContainer;
17 import org.eclipse.debug.core.sourcelookup.ISourceContainerType;
18 import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector;
19 import org.eclipse.debug.core.sourcelookup.containers.CompositeSourceContainer;
20 import org.eclipse.jdt.core.IClasspathContainer;
21 import org.eclipse.jdt.core.IJavaProject;
22 import org.eclipse.jdt.core.JavaCore;
23 import org.eclipse.jdt.internal.launching.LaunchingPlugin;
24 import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
25 import org.eclipse.jdt.launching.JavaRuntime;
26
27 /**
28  * A source container for a classpath container.
29  * <p>
30  * This class may be instantiated; this class is not intended to be
31  * subclassed.
32  * </p>
33  *
34  * @since 3.0
35  */

36
37 public class ClasspathContainerSourceContainer extends CompositeSourceContainer {
38     
39     /**
40      * Associated classpath container path.
41      */

42     private IPath fContainerPath;
43     /**
44      * Unique identifier for Java project source container type
45      * (value <code>org.eclipse.jdt.launching.sourceContainer.classpathContainer</code>).
46      */

47     public static final String JavaDoc TYPE_ID = LaunchingPlugin.getUniqueIdentifier() + ".sourceContainer.classpathContainer"; //$NON-NLS-1$
48

49     /**
50      * Constructs a new source container for the given classpath container.
51      *
52      * @param containerPath classpath container path
53      */

54     public ClasspathContainerSourceContainer(IPath containerPath) {
55         fContainerPath = containerPath;
56     }
57     
58     /* (non-Javadoc)
59      * @see org.eclipse.debug.internal.core.sourcelookup.ISourceContainer#getName()
60      */

61     public String JavaDoc getName() {
62         IClasspathContainer container = null;
63         try {
64             container = getClasspathContainer();
65         } catch (CoreException e) {
66         }
67         if (container == null) {
68             return getPath().lastSegment();
69         }
70         return container.getDescription();
71     }
72     /* (non-Javadoc)
73      * @see org.eclipse.debug.internal.core.sourcelookup.ISourceContainer#getType()
74      */

75     public ISourceContainerType getType() {
76         return getSourceContainerType(TYPE_ID);
77     }
78
79     /* (non-Javadoc)
80      * @see org.eclipse.debug.internal.core.sourcelookup.containers.CompositeSourceContainer#createSourceContainers()
81      */

82     protected ISourceContainer[] createSourceContainers() throws CoreException {
83         IRuntimeClasspathEntry entry = JavaRuntime.newRuntimeContainerClasspathEntry(getPath(), IRuntimeClasspathEntry.USER_CLASSES);
84         IRuntimeClasspathEntry[] entries = JavaRuntime.resolveSourceLookupPath(new IRuntimeClasspathEntry[]{entry}, getDirector().getLaunchConfiguration());
85         return JavaRuntime.getSourceContainers(entries);
86     }
87     
88     /**
89      * Returns the classpath container's path
90      *
91      * @return classpath container's path
92      */

93     public IPath getPath() {
94         return fContainerPath;
95     }
96
97     /* (non-Javadoc)
98      * @see java.lang.Object#equals(java.lang.Object)
99      */

100     public boolean equals(Object JavaDoc obj) {
101         if (obj instanceof ClasspathContainerSourceContainer) {
102             return getPath().equals(((ClasspathContainerSourceContainer)obj).getPath());
103         }
104         return false;
105     }
106     /* (non-Javadoc)
107      * @see java.lang.Object#hashCode()
108      */

109     public int hashCode() {
110         return getPath().hashCode();
111     }
112     
113     /**
114      * Returns the associated container or <code>null</code> if unavailable.
115      *
116      * @return classpath container or <code>null</code>
117      * @throws CoreException if unable to retrieve container
118      */

119     public IClasspathContainer getClasspathContainer() throws CoreException {
120         ISourceLookupDirector director = getDirector();
121         if (director != null) {
122             ILaunchConfiguration configuration = director.getLaunchConfiguration();
123             if (configuration != null) {
124                 IJavaProject project = JavaRuntime.getJavaProject(configuration);
125                 if (project != null) {
126                     return JavaCore.getClasspathContainer(getPath(), project);
127                 }
128             }
129         }
130         return null;
131     }
132     
133 }
134
Popular Tags