KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > actions > OpenTypeAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.actions;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IAdaptable;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.debug.core.model.IDebugElement;
24 import org.eclipse.jdt.core.IJavaElement;
25 import org.eclipse.jdt.core.IType;
26 import org.eclipse.jdt.core.search.IJavaSearchConstants;
27 import org.eclipse.jdt.core.search.SearchEngine;
28 import org.eclipse.jdt.core.search.SearchMatch;
29 import org.eclipse.jdt.core.search.SearchParticipant;
30 import org.eclipse.jdt.core.search.SearchPattern;
31 import org.eclipse.jdt.core.search.SearchRequestor;
32 import org.eclipse.jdt.debug.core.IJavaArrayType;
33 import org.eclipse.jdt.debug.core.IJavaType;
34 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants;
35 import org.eclipse.jdt.internal.debug.core.JavaDebugUtils;
36 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
37 import org.eclipse.jdt.internal.ui.util.OpenTypeHierarchyUtil;
38 import org.eclipse.jdt.ui.JavaUI;
39 import org.eclipse.jface.action.IAction;
40 import org.eclipse.jface.viewers.IStructuredSelection;
41
42 public abstract class OpenTypeAction extends ObjectActionDelegate {
43     
44     /* (non-Javadoc)
45      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
46      */

47     public void run(IAction action) {
48         IStructuredSelection selection= getCurrentSelection();
49         if (selection == null) {
50             return;
51         }
52         Iterator JavaDoc itr= selection.iterator();
53         try {
54             while (itr.hasNext()) {
55                 Object JavaDoc element= itr.next();
56                 Object JavaDoc sourceElement = resolveSourceElement(element);
57                 if (sourceElement != null) {
58                     openInEditor(sourceElement);
59                 } else {
60                     IStatus status = new Status(IStatus.INFO, IJavaDebugUIConstants.PLUGIN_ID, IJavaDebugUIConstants.INTERNAL_ERROR, ActionMessages.OpenTypeAction_0, null);
61                     throw new CoreException(status);
62                 }
63             }
64         } catch(CoreException e) {
65             JDIDebugUIPlugin.statusDialog(e.getStatus());
66         }
67     }
68     
69     protected abstract IDebugElement getDebugElement(IAdaptable element);
70     
71     /**
72      * Returns the type to open based on the given selected debug element, or <code>null</code>.
73      *
74      * @param element selected debug element
75      * @return the type to open or <code>null</code> if none
76      * @throws DebugException
77      */

78     protected abstract IJavaType getTypeToOpen(IDebugElement element) throws CoreException;
79     
80     /**
81      * Resolves and returns the source element to open or <code>null</code> if none.
82      *
83      * @param e selected element to resolve a source element for
84      * @return the source element to open or <code>null</code> if none
85      * @throws CoreException
86      */

87     protected Object JavaDoc resolveSourceElement(Object JavaDoc e) throws CoreException {
88         Object JavaDoc source = null;
89         IAdaptable element= (IAdaptable) e;
90         IDebugElement dbgElement= getDebugElement(element);
91         if (dbgElement != null) {
92             IJavaType type = getTypeToOpen(dbgElement);
93             while (type instanceof IJavaArrayType) {
94                 type = ((IJavaArrayType)type).getComponentType();
95             }
96             if (type != null) {
97                 source = JavaDebugUtils.resolveType(type);
98                 if (source == null) {
99                     //resort to looking through the workspace projects for the
100
//type as the source locators failed.
101
source = findTypeInWorkspace(type.getName());
102                 }
103             }
104         }
105         return source;
106     }
107
108     protected void openInEditor(Object JavaDoc sourceElement) throws CoreException {
109         if (isHierarchy()) {
110             if (sourceElement instanceof IJavaElement) {
111                 OpenTypeHierarchyUtil.open((IJavaElement)sourceElement, getWorkbenchWindow());
112             } else {
113                 typeHierarchyError();
114             }
115         } else {
116             if(sourceElement instanceof IJavaElement) {
117                 JavaUI.openInEditor((IJavaElement) sourceElement);
118             }
119             else {
120                 showErrorMessage(ActionMessages.OpenTypeAction_2);
121             }
122         }
123     }
124     
125     /**
126      * Returns whether a type hierarchy should be opened.
127      *
128      * @return whether a type hierarchy should be opened
129      */

130     protected boolean isHierarchy() {
131         return false;
132     }
133     
134     /**
135      * Searches for and returns a type with the given name in the workspace,
136      * or <code>null</code> if none.
137      *
138      * @param typeName fully qualified type name
139      * @return type or <code>null</code>
140      * @throws JavaModelException
141      */

142     public static IType findTypeInWorkspace(String JavaDoc typeName) throws CoreException {
143         IType[] types= findTypes(typeName, null);
144         if (types.length > 0) {
145             return types[0];
146         }
147         return null;
148     }
149     
150     private static IType[] findTypes(String JavaDoc typeName, IProgressMonitor monitor) throws CoreException {
151         
152         final List JavaDoc results= new ArrayList JavaDoc();
153         
154         SearchRequestor collector= new SearchRequestor() {
155             public void acceptSearchMatch(SearchMatch match) throws CoreException {
156                 Object JavaDoc element= match.getElement();
157                 if (element instanceof IType)
158                     results.add(element);
159             }
160         };
161         
162         SearchEngine engine= new SearchEngine();
163         SearchPattern pattern= SearchPattern.createPattern(typeName, IJavaSearchConstants.TYPE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH);
164         engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, SearchEngine.createWorkspaceScope(), collector, monitor);
165         
166         return (IType[]) results.toArray(new IType[results.size()]);
167     }
168     
169     protected void typeHierarchyError() {
170         showErrorMessage(ActionMessages.ObjectActionDelegate_Unable_to_display_type_hierarchy__The_selected_source_element_is_not_contained_in_the_workspace__1);
171     }
172 }
173
Popular Tags