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