KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > snippeteditor > SnippetOpenOnSelectionAction


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.debug.ui.snippeteditor;
12
13  
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
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 /**
34  * This action opens a Java editor on the element represented by text selection of
35  * the code snippet.
36  */

37 public class SnippetOpenOnSelectionAction extends OpenAction {
38     
39     protected JavaSnippetEditor fEditor;
40     private String JavaDoc fDialogTitle;
41     private String JavaDoc 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")); //$NON-NLS-1$
57
setDescription(SnippetMessages.getString("SnippetOpenOnSelectionAction.tooltip")); //$NON-NLS-1$
58
setToolTipText(SnippetMessages.getString("SnippetOpenOnSelectionAction.description")); //$NON-NLS-1$
59
setDialogTitle(SnippetMessages.getString("SnippetOpenOnSelectionDialog.title")); //$NON-NLS-1$
60
setDialogMessage(SnippetMessages.getString("SnippetOpenOnSelectionDialog.message")); //$NON-NLS-1$
61
}
62     
63     protected void setDialogTitle(String JavaDoc title) {
64         fDialogTitle= title;
65     }
66     
67     protected void setDialogMessage(String JavaDoc message) {
68         fDialogMessage= message;
69     }
70     
71     protected void setEditor(JavaSnippetEditor contentEditor) {
72         fEditor= contentEditor;
73     }
74     
75     /**
76      * Shows a dialog for resolving an ambigous java element.
77      * Utility method that can be called by subclassers.
78      */

79     protected IJavaElement selectJavaElement(List JavaDoc elements, Shell shell, String JavaDoc title, String JavaDoc 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 JavaDoc[] selection= dialog.getResult();
100             if (selection != null && selection.length > 0) {
101                 nResults= selection.length;
102                 for (int i= 0; i < nResults; i++) {
103                     Object JavaDoc current= selection[i];
104                     if (current instanceof IJavaElement)
105                         return (IJavaElement) current;
106                 }
107             }
108         }
109         return null;
110     }
111     
112     
113     /**
114      * Filters out source references from the given code resolve results.
115      * A utility method that can be called by subclassers.
116      */

117     protected List JavaDoc filterResolveResults(IJavaElement[] codeResolveResults) {
118         int nResults= codeResolveResults.length;
119         List JavaDoc refs= new ArrayList JavaDoc(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     /**
147      * @see SelectionDispatchAction#selectionChanged(ITextSelection)
148      */

149     public void selectionChanged(ITextSelection selection) {
150         setEnabled(fEditor != null);
151     }
152 }
153
Popular Tags