KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > actions > FindExceptionOccurrences


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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

44 public class FindExceptionOccurrences extends SelectionDispatchAction {
45     
46     private JavaEditor fEditor;
47     
48     /**
49      * Note: This constructor is for internal use only. Clients should not call this constructor.
50      */

51     public FindExceptionOccurrences(JavaEditor editor) {
52         this(editor.getEditorSite());
53         fEditor= editor;
54         setEnabled(getEditorInput(editor) != null);
55     }
56     
57     private FindExceptionOccurrences(IWorkbenchSite site) {
58         super(site);
59         setText(ActionMessages.getString("FindExceptionOccurrences.text")); //$NON-NLS-1$
60
setToolTipText(ActionMessages.getString("FindExceptionOccurrences.toolTip")); //$NON-NLS-1$
61
WorkbenchHelp.setHelp(this, IJavaHelpContextIds.FIND_EXCEPTION_OCCURRENCES);
62     }
63     
64     //---- Text Selection ----------------------------------------------------------------------
65

66     /* (non-JavaDoc)
67      * Method declared in SelectionDispatchAction.
68      */

69     public void selectionChanged(ITextSelection selection) {
70     }
71
72     /* (non-JavaDoc)
73      * Method declared in SelectionDispatchAction.
74      */

75     public final void run(ITextSelection ts) {
76         IJavaElement input= getEditorInput(fEditor);
77         if (!ActionUtil.isProcessable(getShell(), input))
78             return;
79         FindOccurrencesEngine engine= FindOccurrencesEngine.create(input, new ExceptionOccurrencesFinder());
80         try {
81             String JavaDoc result= engine.run(ts.getOffset(), ts.getLength());
82             if (result != null)
83                 showMessage(getShell(), fEditor, result);
84         } catch (JavaModelException e) {
85             JavaPlugin.log(e);
86         }
87     }
88
89     private static IJavaElement getEditorInput(JavaEditor editor) {
90         IEditorInput input= editor.getEditorInput();
91         if (input instanceof IClassFileEditorInput)
92             return ((IClassFileEditorInput)input).getClassFile();
93         return JavaPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(input);
94     }
95         
96     private static void showMessage(Shell shell, JavaEditor editor, String JavaDoc msg) {
97         IEditorStatusLine statusLine= (IEditorStatusLine) editor.getAdapter(IEditorStatusLine.class);
98         if (statusLine != null)
99             statusLine.setMessage(true, msg, null);
100         shell.getDisplay().beep();
101     }
102 }
103
Popular Tags