KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 /**
32  * This action opens a Java editor on the element represented by text selection of
33  * the connected Java source editor. In addition, if the element is a type, it also
34  * opens shows the element in the type hierarchy viewer.
35  */

36 public class SnippetOpenHierarchyOnSelectionAction extends OpenTypeHierarchyAction {
37     
38     private JavaSnippetEditor fEditor;
39     private String JavaDoc fDialogTitle;
40     private String JavaDoc 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")); //$NON-NLS-1$
55
setDescription(SnippetMessages.getString("SnippetOpenHierarchyOnSelectionAction.tooltip")); //$NON-NLS-1$
56
setToolTipText(SnippetMessages.getString("SnippetOpenHierarchyOnSelectionAction.description")); //$NON-NLS-1$
57
setDialogTitle(SnippetMessages.getString("SnippetOpenHierarchyOnSelectionDialog.title")); //$NON-NLS-1$
58
setDialogMessage(SnippetMessages.getString("SnippetOpenHierarchyOnSelectionDialog.message")); //$NON-NLS-1$
59
}
60     
61     protected void setDialogTitle(String JavaDoc title) {
62         fDialogTitle= title;
63     }
64     
65     protected void setDialogMessage(String JavaDoc 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     /**
94      * Filters out source references from the given code resolve results.
95      * A utility method that can be called by subclassers.
96      */

97     protected List JavaDoc filterResolveResults(IJavaElement[] codeResolveResults) {
98         int nResults= codeResolveResults.length;
99         List JavaDoc refs= new ArrayList JavaDoc(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     /**
110      * Shows a dialog for resolving an ambigous Java element.
111      * Utility method that can be called by subclassers.
112      */

113     protected IJavaElement selectJavaElement(List JavaDoc elements, Shell shell, String JavaDoc title, String JavaDoc 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 JavaDoc[] selection= dialog.getResult();
136             if (selection != null && selection.length > 0) {
137                 nResults= selection.length;
138                 for (int i= 0; i < nResults; i++) {
139                     Object JavaDoc current= selection[i];
140                     if (current instanceof IJavaElement) {
141                         return (IJavaElement) current;
142                     }
143                 }
144             }
145         }
146         return null;
147     }
148 }
149
Popular Tags