KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > util > OpenTypeHierarchyUtil


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.jdt.internal.ui.util;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.CoreException;
15
16
17 import org.eclipse.ui.IWorkbench;
18 import org.eclipse.ui.IWorkbenchPage;
19 import org.eclipse.ui.IWorkbenchWindow;
20 import org.eclipse.ui.WorkbenchException;
21
22 import org.eclipse.jdt.core.IClassFile;
23 import org.eclipse.jdt.core.ICompilationUnit;
24 import org.eclipse.jdt.core.IImportDeclaration;
25 import org.eclipse.jdt.core.IJavaElement;
26 import org.eclipse.jdt.core.IMember;
27 import org.eclipse.jdt.core.IPackageFragment;
28 import org.eclipse.jdt.core.IType;
29 import org.eclipse.jdt.core.JavaModelException;
30 import org.eclipse.jdt.core.Signature;
31
32 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
33
34 import org.eclipse.jdt.ui.JavaUI;
35 import org.eclipse.jdt.ui.PreferenceConstants;
36
37 import org.eclipse.jdt.internal.ui.JavaPlugin;
38 import org.eclipse.jdt.internal.ui.JavaUIMessages;
39 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
40 import org.eclipse.jdt.internal.ui.typehierarchy.TypeHierarchyViewPart;
41
42 public class OpenTypeHierarchyUtil {
43     
44     private OpenTypeHierarchyUtil() {
45     }
46
47     public static TypeHierarchyViewPart open(IJavaElement element, IWorkbenchWindow window) {
48         IJavaElement[] candidates= getCandidates(element);
49         if (candidates != null) {
50             return open(candidates, window);
51         }
52         return null;
53     }
54     
55     public static TypeHierarchyViewPart open(IJavaElement[] candidates, IWorkbenchWindow window) {
56         Assert.isTrue(candidates != null && candidates.length != 0);
57             
58         IJavaElement input= null;
59         if (candidates.length > 1) {
60             String JavaDoc title= JavaUIMessages.OpenTypeHierarchyUtil_selectionDialog_title;
61             String JavaDoc message= JavaUIMessages.OpenTypeHierarchyUtil_selectionDialog_message;
62             input= SelectionConverter.selectJavaElement(candidates, window.getShell(), title, message);
63         } else {
64             input= candidates[0];
65         }
66         if (input == null)
67             return null;
68             
69         try {
70             if (PreferenceConstants.OPEN_TYPE_HIERARCHY_IN_PERSPECTIVE.equals(PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.OPEN_TYPE_HIERARCHY))) {
71                 return openInPerspective(window, input);
72             } else {
73                 return openInViewPart(window, input);
74             }
75                 
76         } catch (WorkbenchException e) {
77             ExceptionHandler.handle(e, window.getShell(),
78                 JavaUIMessages.OpenTypeHierarchyUtil_error_open_perspective,
79                 e.getMessage());
80         } catch (JavaModelException e) {
81             ExceptionHandler.handle(e, window.getShell(),
82                 JavaUIMessages.OpenTypeHierarchyUtil_error_open_editor,
83                 e.getMessage());
84         }
85         return null;
86     }
87
88     private static TypeHierarchyViewPart openInViewPart(IWorkbenchWindow window, IJavaElement input) {
89         IWorkbenchPage page= window.getActivePage();
90         try {
91             TypeHierarchyViewPart result= (TypeHierarchyViewPart) page.findView(JavaUI.ID_TYPE_HIERARCHY);
92             if (result != null) {
93                 result.clearNeededRefresh(); // avoid refresh of old hierarchy on 'becomes visible'
94
}
95             result= (TypeHierarchyViewPart) page.showView(JavaUI.ID_TYPE_HIERARCHY);
96             result.setInputElement(input);
97             return result;
98         } catch (CoreException e) {
99             ExceptionHandler.handle(e, window.getShell(),
100                 JavaUIMessages.OpenTypeHierarchyUtil_error_open_view, e.getMessage());
101         }
102         return null;
103     }
104     
105     private static TypeHierarchyViewPart openInPerspective(IWorkbenchWindow window, IJavaElement input) throws WorkbenchException, JavaModelException {
106         IWorkbench workbench= JavaPlugin.getDefault().getWorkbench();
107         // The problem is that the input element can be a working copy. So we first convert it to the original element if
108
// it exists.
109
IJavaElement perspectiveInput= input;
110         
111         if (input instanceof IMember) {
112             if (input.getElementType() != IJavaElement.TYPE) {
113                 perspectiveInput= ((IMember)input).getDeclaringType();
114             } else {
115                 perspectiveInput= input;
116             }
117         }
118         IWorkbenchPage page= workbench.showPerspective(JavaUI.ID_HIERARCHYPERSPECTIVE, window, perspectiveInput);
119         
120         TypeHierarchyViewPart part= (TypeHierarchyViewPart) page.findView(JavaUI.ID_TYPE_HIERARCHY);
121         if (part != null) {
122             part.clearNeededRefresh(); // avoid refresh of old hierarchy on 'becomes visible'
123
}
124         part= (TypeHierarchyViewPart) page.showView(JavaUI.ID_TYPE_HIERARCHY);
125         part.setInputElement(input);
126         if (input instanceof IMember) {
127             if (page.getEditorReferences().length == 0) {
128                 JavaUI.openInEditor(input, false, false); // only open when the perspecive has been created
129
}
130         }
131         return part;
132     }
133
134
135     /**
136      * Converts the input to a possible input candidates
137      */

138     public static IJavaElement[] getCandidates(Object JavaDoc input) {
139         if (!(input instanceof IJavaElement)) {
140             return null;
141         }
142         try {
143             IJavaElement elem= (IJavaElement) input;
144             switch (elem.getElementType()) {
145                 case IJavaElement.INITIALIZER:
146                 case IJavaElement.METHOD:
147                 case IJavaElement.FIELD:
148                 case IJavaElement.TYPE:
149                 case IJavaElement.PACKAGE_FRAGMENT_ROOT:
150                 case IJavaElement.JAVA_PROJECT:
151                     return new IJavaElement[] { elem };
152                 case IJavaElement.PACKAGE_FRAGMENT:
153                     if (((IPackageFragment)elem).containsJavaResources())
154                         return new IJavaElement[] {elem};
155                     break;
156                 case IJavaElement.PACKAGE_DECLARATION:
157                     return new IJavaElement[] { elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT) };
158                 case IJavaElement.IMPORT_DECLARATION:
159                     IImportDeclaration decl= (IImportDeclaration) elem;
160                     if (decl.isOnDemand()) {
161                         elem= JavaModelUtil.findTypeContainer(elem.getJavaProject(), Signature.getQualifier(elem.getElementName()));
162                     } else {
163                         elem= elem.getJavaProject().findType(elem.getElementName());
164                     }
165                     if (elem == null)
166                         return null;
167                     return new IJavaElement[] {elem};
168                     
169                 case IJavaElement.CLASS_FILE:
170                     return new IJavaElement[] { ((IClassFile)input).getType() };
171                 case IJavaElement.COMPILATION_UNIT: {
172                     ICompilationUnit cu= (ICompilationUnit) elem.getAncestor(IJavaElement.COMPILATION_UNIT);
173                     if (cu != null) {
174                         IType[] types= cu.getTypes();
175                         if (types.length > 0) {
176                             return types;
177                         }
178                     }
179                     break;
180                 }
181                 default:
182             }
183         } catch (JavaModelException e) {
184             JavaPlugin.log(e);
185         }
186         return null;
187     }
188 }
189
Popular Tags