1 11 package org.eclipse.jdt.internal.debug.ui.actions; 12 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 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 47 public void run(IAction action) { 48 IStructuredSelection selection= getCurrentSelection(); 49 if (selection == null) { 50 return; 51 } 52 Iterator itr= selection.iterator(); 53 try { 54 while (itr.hasNext()) { 55 Object element= itr.next(); 56 Object 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 78 protected abstract IJavaType getTypeToOpen(IDebugElement element) throws CoreException; 79 80 87 protected Object resolveSourceElement(Object e) throws CoreException { 88 Object 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 source = findTypeInWorkspace(type.getName()); 102 } 103 } 104 } 105 return source; 106 } 107 108 protected void openInEditor(Object 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 130 protected boolean isHierarchy() { 131 return false; 132 } 133 134 142 public static IType findTypeInWorkspace(String 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 typeName, IProgressMonitor monitor) throws CoreException { 151 152 final List results= new ArrayList (); 153 154 SearchRequestor collector= new SearchRequestor() { 155 public void acceptSearchMatch(SearchMatch match) throws CoreException { 156 Object 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 |