KickJava   Java API By Example, From Geeks To Geeks.

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


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.FindOccurrencesEngine;
35 import org.eclipse.jdt.internal.ui.search.ImplementOccurrencesFinder;
36
37 /**
38  * Action to find all implement occurrences of an extended class or an implemented interface.
39  * <p>
40  * This class may be instantiated; it is not intended to be subclassed.
41  * </p>
42  *
43  * @since 3.1
44  */

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

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

67     public FindImplementOccurrencesAction(IWorkbenchSite site) {
68         super(site);
69         setText(ActionMessages.FindImplementOccurrencesAction_text);
70         setToolTipText(ActionMessages.FindImplementOccurrencesAction_toolTip);
71         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.FIND_IMPLEMENT_OCCURRENCES);
72     }
73     
74     //---- Text Selection ----------------------------------------------------------------------
75

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

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

87     public void selectionChanged(IStructuredSelection selection) {
88         setEnabled(getMember(selection) != null);
89     }
90
91     /*
92      * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.text.ITextSelection)
93      */

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