KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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
12 package org.eclipse.jdt.launching.sourcelookup.containers;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IPath;
22 import org.eclipse.debug.core.sourcelookup.AbstractSourceLookupParticipant;
23 import org.eclipse.debug.core.sourcelookup.ISourceContainer;
24 import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector;
25 import org.eclipse.debug.core.sourcelookup.containers.ArchiveSourceContainer;
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.core.JavaModelException;
30 import org.eclipse.jdt.internal.debug.core.JavaDebugUtils;
31
32 /**
33  * A source lookup participant that searches for Java source code.
34  * <p>
35  * This class may be instantiated; this class is not intended to be
36  * subclassed.
37  * </p>
38  * @since 3.0
39  */

40 public class JavaSourceLookupParticipant extends AbstractSourceLookupParticipant {
41     
42     /**
43      * Map of delegate source containers for internal jars.
44      * Internal jars are translated to package fragment roots
45      * if possible.
46      */

47     private Map JavaDoc fDelegateContainers;
48     
49     /**
50      * Returns the source name associated with the given object, or <code>null</code>
51      * if none.
52      *
53      * @param object an object with an <code>IJavaStackFrame</code> adapter, an IJavaValue
54      * or an IJavaType
55      * @return the source name associated with the given object, or <code>null</code>
56      * if none
57      * @exception CoreException if unable to retrieve the source name
58      */

59     public String JavaDoc getSourceName(Object JavaDoc object) throws CoreException {
60         return JavaDebugUtils.getSourceName(object);
61     }
62
63     /* (non-Javadoc)
64      * @see org.eclipse.debug.internal.core.sourcelookup.ISourceLookupParticipant#dispose()
65      */

66     public void dispose() {
67         Iterator JavaDoc iterator = fDelegateContainers.values().iterator();
68         while (iterator.hasNext()) {
69             ISourceContainer container = (ISourceContainer) iterator.next();
70             container.dispose();
71         }
72         fDelegateContainers = null;
73         super.dispose();
74         
75     }
76
77     /* (non-Javadoc)
78      * @see org.eclipse.debug.internal.core.sourcelookup.AbstractSourceLookupParticipant#getDelegateContainer(org.eclipse.debug.internal.core.sourcelookup.ISourceContainer)
79      */

80     protected ISourceContainer getDelegateContainer(ISourceContainer container) {
81         ISourceContainer delegate = (ISourceContainer) fDelegateContainers.get(container);
82         if (delegate == null) {
83             return container;
84         }
85         return delegate;
86     }
87     /* (non-Javadoc)
88      * @see org.eclipse.debug.internal.core.sourcelookup.ISourceLookupParticipant#init(org.eclipse.debug.internal.core.sourcelookup.ISourceLookupDirector)
89      */

90     public void init(ISourceLookupDirector director) {
91         super.init(director);
92         fDelegateContainers = new HashMap JavaDoc();
93     }
94     /* (non-Javadoc)
95      * @see org.eclipse.debug.internal.core.sourcelookup.ISourceLookupParticipant#sourceContainersChanged(org.eclipse.debug.internal.core.sourcelookup.ISourceLookupDirector)
96      */

97     public void sourceContainersChanged(ISourceLookupDirector director) {
98         // use package fragment roots in place of local archives, where they exist
99
fDelegateContainers.clear();
100         ISourceContainer[] containers = director.getSourceContainers();
101         for (int i = 0; i < containers.length; i++) {
102             ISourceContainer container = containers[i];
103             if (container.getType().getId().equals(ArchiveSourceContainer.TYPE_ID)) {
104                 IFile file = ((ArchiveSourceContainer)container).getFile();
105                 IProject project = file.getProject();
106                 IJavaProject javaProject = JavaCore.create(project);
107                 if (javaProject.exists()) {
108                     try {
109                         IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
110                         for (int j = 0; j < roots.length; j++) {
111                             IPackageFragmentRoot root = roots[j];
112                             if (file.equals(root.getUnderlyingResource())) {
113                                 // the root was specified
114
fDelegateContainers.put(container, new PackageFragmentRootSourceContainer(root));
115                             } else {
116                                 IPath path = root.getSourceAttachmentPath();
117                                 if (path != null) {
118                                     if (file.getFullPath().equals(path)) {
119                                         // a source attachment to a root was specified
120
fDelegateContainers.put(container, new PackageFragmentRootSourceContainer(root));
121                                     }
122                                 }
123                             }
124                         }
125                     } catch (JavaModelException e) {
126                     }
127                 }
128             }
129         }
130     }
131 }
132
Popular Tags