KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > sourcelookup > WorkbenchAdapter


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 package org.eclipse.jdt.internal.debug.ui.sourcelookup;
12
13 import java.io.File JavaDoc;
14
15 import org.eclipse.core.runtime.IPath;
16 import org.eclipse.debug.ui.DebugUITools;
17 import org.eclipse.debug.ui.IDebugUIConstants;
18 import org.eclipse.jdt.core.IJavaElement;
19 import org.eclipse.jdt.core.IJavaProject;
20 import org.eclipse.jdt.core.IPackageFragmentRoot;
21 import org.eclipse.jdt.launching.sourcelookup.containers.ClasspathContainerSourceContainer;
22 import org.eclipse.jdt.launching.sourcelookup.containers.ClasspathVariableSourceContainer;
23 import org.eclipse.jdt.launching.sourcelookup.containers.JavaProjectSourceContainer;
24 import org.eclipse.jdt.launching.sourcelookup.containers.PackageFragmentRootSourceContainer;
25 import org.eclipse.jdt.ui.ISharedImages;
26 import org.eclipse.jdt.ui.JavaUI;
27 import org.eclipse.jface.resource.ImageDescriptor;
28 import org.eclipse.ui.model.IWorkbenchAdapter;
29
30 /**
31  * Workbench adapter for Java source containers and source container
32  * types.
33  *
34  * @since 3.0
35  */

36 public class WorkbenchAdapter implements IWorkbenchAdapter {
37     /* (non-Javadoc)
38      * @see org.eclipse.ui.model.IWorkbenchAdapter#getChildren(java.lang.Object)
39      */

40     public Object JavaDoc[] getChildren(Object JavaDoc o) {
41         return null;
42     }
43     /* (non-Javadoc)
44      * @see org.eclipse.ui.model.IWorkbenchAdapter#getImageDescriptor(java.lang.Object)
45      */

46     public ImageDescriptor getImageDescriptor(Object JavaDoc object) {
47         if (object instanceof PackageFragmentRootSourceContainer) {
48             PackageFragmentRootSourceContainer container = (PackageFragmentRootSourceContainer) object;
49             IPackageFragmentRoot fragmentRoot = container.getPackageFragmentRoot();
50             return getImageDescriptor(fragmentRoot);
51         }
52         if (object instanceof JavaProjectSourceContainer) {
53             JavaProjectSourceContainer container = (JavaProjectSourceContainer) object;
54             IJavaProject javaProject = container.getJavaProject();
55             return getImageDescriptor(javaProject);
56         }
57         if (object instanceof ClasspathVariableSourceContainer) {
58             return DebugUITools.getImageDescriptor(IDebugUIConstants.IMG_OBJS_ENV_VAR);
59         }
60         if (object instanceof ClasspathContainerSourceContainer) {
61             return JavaUI.getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJS_LIBRARY);
62         }
63         return null;
64     }
65     
66     /**
67      * Returns an image descriptor for a java element, or <code>null</code>
68      * if none.
69      *
70      * @param element java element
71      * @return an image descriptor for a java element, or <code>null</code>
72      * if none
73      */

74     protected ImageDescriptor getImageDescriptor(IJavaElement element) {
75         IWorkbenchAdapter adapter = (IWorkbenchAdapter)element.getAdapter(IWorkbenchAdapter.class);
76         if (adapter != null) {
77             return adapter.getImageDescriptor(element);
78         }
79         return null;
80     }
81     
82     /* (non-Javadoc)
83      * @see org.eclipse.ui.model.IWorkbenchAdapter#getLabel(java.lang.Object)
84      */

85     public String JavaDoc getLabel(Object JavaDoc o) {
86         if (o instanceof PackageFragmentRootSourceContainer) {
87             PackageFragmentRootSourceContainer container = (PackageFragmentRootSourceContainer) o;
88             IPackageFragmentRoot fragmentRoot = container.getPackageFragmentRoot();
89             IPath path = fragmentRoot.getPath();
90             if (path.segmentCount() > 0) {
91                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
92                 buffer.append(path.lastSegment());
93                 if (path.segmentCount() > 1) {
94                     buffer.append(" - "); //$NON-NLS-1$
95
if (path.getDevice() != null) {
96                         buffer.append(path.getDevice());
97                     }
98                     String JavaDoc[] segments = path.segments();
99                     for (int i = 0; i < segments.length - 1; i++) {
100                         buffer.append(File.separatorChar);
101                         buffer.append(segments[i]);
102                     }
103                 }
104                 return buffer.toString();
105             }
106         }
107         return ""; //$NON-NLS-1$
108
}
109     /* (non-Javadoc)
110      * @see org.eclipse.ui.model.IWorkbenchAdapter#getParent(java.lang.Object)
111      */

112     public Object JavaDoc getParent(Object JavaDoc o) {
113         return null;
114     }
115 }
116
Popular Tags