1 11 package org.eclipse.jdt.internal.debug.ui.snippeteditor; 12 13 14 import java.util.ArrayList ; 15 import java.util.List ; 16 17 import org.eclipse.jdt.core.IJavaElement; 18 import org.eclipse.jdt.core.ISourceReference; 19 import org.eclipse.jdt.core.JavaModelException; 20 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; 21 import org.eclipse.jdt.ui.JavaElementLabelProvider; 22 import org.eclipse.jdt.ui.actions.IJavaEditorActionDefinitionIds; 23 import org.eclipse.jdt.ui.actions.OpenTypeHierarchyAction; 24 import org.eclipse.jface.commands.ActionHandler; 25 import org.eclipse.jface.viewers.StructuredSelection; 26 import org.eclipse.jface.window.Window; 27 import org.eclipse.swt.widgets.Shell; 28 import org.eclipse.ui.dialogs.ElementListSelectionDialog; 29 import org.eclipse.ui.handlers.IHandlerService; 30 31 36 public class SnippetOpenHierarchyOnSelectionAction extends OpenTypeHierarchyAction { 37 38 private JavaSnippetEditor fEditor; 39 private String fDialogTitle; 40 private String fDialogMessage; 41 42 public SnippetOpenHierarchyOnSelectionAction(JavaSnippetEditor editor) { 43 super(editor.getSite()); 44 fEditor= editor; 45 setResources(); 46 setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_TYPE_HIERARCHY); 47 48 ActionHandler handler = new ActionHandler(this); 49 IHandlerService handlerService = (IHandlerService) editor.getSite().getService(IHandlerService.class); 50 handlerService.activateHandler(IJavaEditorActionDefinitionIds.OPEN_TYPE_HIERARCHY, handler); 51 } 52 53 protected void setResources() { 54 setText(SnippetMessages.getString("SnippetOpenHierarchyOnSelectionAction.label")); setDescription(SnippetMessages.getString("SnippetOpenHierarchyOnSelectionAction.tooltip")); setToolTipText(SnippetMessages.getString("SnippetOpenHierarchyOnSelectionAction.description")); setDialogTitle(SnippetMessages.getString("SnippetOpenHierarchyOnSelectionDialog.title")); setDialogMessage(SnippetMessages.getString("SnippetOpenHierarchyOnSelectionDialog.message")); } 60 61 protected void setDialogTitle(String title) { 62 fDialogTitle= title; 63 } 64 65 protected void setDialogMessage(String message) { 66 fDialogMessage= message; 67 } 68 69 public void run() { 70 if (fEditor == null) { 71 return; 72 } 73 try { 74 IJavaElement[] result= fEditor.codeResolve(); 75 if (result != null && result.length > 0) { 76 IJavaElement chosen= selectJavaElement(filterResolveResults(result), getShell(), fDialogTitle, fDialogMessage); 77 if (chosen != null) { 78 run(new StructuredSelection(chosen)); 79 return; 80 } 81 } 82 } catch (JavaModelException x) { 83 JDIDebugUIPlugin.log(x); 84 } 85 86 getShell().getDisplay().beep(); 87 } 88 89 protected void setEditor(JavaSnippetEditor contentEditor) { 90 fEditor= contentEditor; 91 } 92 93 97 protected List filterResolveResults(IJavaElement[] codeResolveResults) { 98 int nResults= codeResolveResults.length; 99 List refs= new ArrayList (nResults); 100 for (int i= 0; i < nResults; i++) { 101 if (codeResolveResults[i] instanceof ISourceReference) { 102 refs.add(codeResolveResults[i]); 103 } 104 } 105 return refs; 106 } 107 108 109 113 protected IJavaElement selectJavaElement(List elements, Shell shell, String title, String message) { 114 115 int nResults= elements.size(); 116 117 if (nResults == 0) { 118 return null; 119 } 120 121 if (nResults == 1) { 122 return (IJavaElement) elements.get(0); 123 } 124 125 int flags= JavaElementLabelProvider.SHOW_DEFAULT 126 | JavaElementLabelProvider.SHOW_QUALIFIED 127 | JavaElementLabelProvider.SHOW_ROOT; 128 129 ElementListSelectionDialog dialog= new ElementListSelectionDialog(shell, new JavaElementLabelProvider(flags)); 130 dialog.setTitle(title); 131 dialog.setMessage(message); 132 dialog.setElements(elements.toArray()); 133 134 if (dialog.open() == Window.OK) { 135 Object [] selection= dialog.getResult(); 136 if (selection != null && selection.length > 0) { 137 nResults= selection.length; 138 for (int i= 0; i < nResults; i++) { 139 Object current= selection[i]; 140 if (current instanceof IJavaElement) { 141 return (IJavaElement) current; 142 } 143 } 144 } 145 } 146 return null; 147 } 148 } 149 | Popular Tags |