KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > JavaWorkbenchAdapter


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.ui;
12
13
14 import org.eclipse.jface.resource.ImageDescriptor;
15
16 import org.eclipse.ui.model.IWorkbenchAdapter;
17
18 import org.eclipse.jdt.core.IJavaElement;
19 import org.eclipse.jdt.core.IParent;
20 import org.eclipse.jdt.core.JavaModelException;
21
22 import org.eclipse.jdt.ui.JavaElementLabels;
23
24 import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput;
25 import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
26
27 /**
28  * An imlementation of the IWorkbenchAdapter for IJavaElements.
29  */

30 public class JavaWorkbenchAdapter implements IWorkbenchAdapter {
31     
32     protected static final Object JavaDoc[] NO_CHILDREN= new Object JavaDoc[0];
33     
34     private JavaElementImageProvider fImageProvider;
35     
36     public JavaWorkbenchAdapter() {
37         fImageProvider= new JavaElementImageProvider();
38     }
39
40     public Object JavaDoc[] getChildren(Object JavaDoc element) {
41         IJavaElement je= getJavaElement(element);
42         if (je instanceof IParent) {
43             try {
44                 return ((IParent)je).getChildren();
45             } catch(JavaModelException e) {
46                 JavaPlugin.log(e);
47             }
48         }
49         return NO_CHILDREN;
50     }
51
52     public ImageDescriptor getImageDescriptor(Object JavaDoc element) {
53         IJavaElement je= getJavaElement(element);
54         if (je != null)
55             return fImageProvider.getJavaImageDescriptor(je, JavaElementImageProvider.OVERLAY_ICONS | JavaElementImageProvider.SMALL_ICONS);
56         
57         return null;
58         
59     }
60
61     public String JavaDoc getLabel(Object JavaDoc element) {
62         return JavaElementLabels.getTextLabel(getJavaElement(element), JavaElementLabels.ALL_DEFAULT);
63     }
64
65     public Object JavaDoc getParent(Object JavaDoc element) {
66         IJavaElement je= getJavaElement(element);
67         return je != null ? je.getParent() : null;
68     }
69     
70     private IJavaElement getJavaElement(Object JavaDoc element) {
71         if (element instanceof IJavaElement)
72             return (IJavaElement)element;
73         if (element instanceof IClassFileEditorInput)
74             return ((IClassFileEditorInput)element).getClassFile().getPrimaryElement();
75
76         return null;
77     }
78 }
79
Popular Tags