KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > actions > FindExceptionOccurrencesAction


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.ui.actions;
12
13 import org.eclipse.swt.widgets.Shell;
14
15 import org.eclipse.jface.viewers.IStructuredSelection;
16
17 import org.eclipse.jface.text.ITextSelection;
18
19 import org.eclipse.ui.IEditorInput;
20 import org.eclipse.ui.IWorkbenchSite;
21 import org.eclipse.ui.PlatformUI;
22 import org.eclipse.ui.texteditor.IEditorStatusLine;
23
24 import org.eclipse.jdt.core.IJavaElement;
25 import org.eclipse.jdt.core.IMember;
26 import org.eclipse.jdt.core.JavaModelException;
27
28 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
29 import org.eclipse.jdt.internal.ui.JavaPlugin;
30 import org.eclipse.jdt.internal.ui.actions.ActionMessages;
31 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
32 import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput;
33 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
34 import org.eclipse.jdt.internal.ui.search.ExceptionOccurrencesFinder;
35 import org.eclipse.jdt.internal.ui.search.FindOccurrencesEngine;
36
37 /**
38  * Action to find all originators of a exception (e.g. method invocations,
39  * class casts, ...) for a given exception.
40  * <p>
41  * This class may be instantiated; it is not intended to be subclassed.
42  * </p>
43  *
44  * @since 3.1
45  */

46 public class FindExceptionOccurrencesAction extends SelectionDispatchAction {
47     
48     private JavaEditor fEditor;
49     
50     /**
51      * Note: This constructor is for internal use only. Clients should not call this constructor.
52      *
53      * @param editor the Java editor
54      */

55     public FindExceptionOccurrencesAction(JavaEditor editor) {
56         this(editor.getEditorSite());
57         fEditor= editor;
58         setEnabled(getEditorInput(editor) != null);
59     }
60     
61     /**
62      * Creates a new <code>FindExceptionOccurrencesAction</code>. The action
63      * requires that the selection provided by the site's selection provider is of type
64      * <code>IStructuredSelection</code>.
65      *
66      * @param site the site providing context information for this action
67      */

68     public FindExceptionOccurrencesAction(IWorkbenchSite site) {
69         super(site);
70         setText(ActionMessages.FindExceptionOccurrences_text);
71         setToolTipText(ActionMessages.FindExceptionOccurrences_toolTip);
72         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.FIND_EXCEPTION_OCCURRENCES);
73     }
74     
75     //---- Text Selection ----------------------------------------------------------------------
76

77     /**
78      * {@inheritDoc}
79      */

80     public void selectionChanged(ITextSelection selection) {
81     }
82     
83     /**
84      * {@inheritDoc}
85      *
86      * @since 3.2
87      */

88     public void selectionChanged(IStructuredSelection selection) {
89         setEnabled(getMember(selection) != null);
90     }
91
92     /* (non-JavaDoc)
93      * Method declared in SelectionDispatchAction.
94      */

95     public final void run(ITextSelection ts) {
96         IJavaElement input= getEditorInput(fEditor);
97         if (!ActionUtil.isProcessable(getShell(), input))
98             return;
99         FindOccurrencesEngine engine= FindOccurrencesEngine.create(input, new ExceptionOccurrencesFinder());
100         try {
101             String JavaDoc result= engine.run(ts.getOffset(), ts.getLength());
102             if (result != null)
103                 showMessage(getShell(), fEditor, result);
104         } catch (JavaModelException e) {
105             JavaPlugin.log(e);
106         }
107     }
108
109     private static IJavaElement getEditorInput(JavaEditor editor) {
110         IEditorInput input= editor.getEditorInput();
111         if (input instanceof IClassFileEditorInput)
112             return ((IClassFileEditorInput)input).getClassFile();
113         return JavaPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(input);
114     }
115         
116     private static void showMessage(Shell shell, JavaEditor editor, String JavaDoc msg) {
117         IEditorStatusLine statusLine= (IEditorStatusLine) editor.getAdapter(IEditorStatusLine.class);
118         if (statusLine != null)
119             statusLine.setMessage(true, msg, null);
120         shell.getDisplay().beep();
121     }
122     
123     private IMember getMember(IStructuredSelection selection) {
124         return null;
125     }
126 }
127
Popular Tags