KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.core.runtime.Assert;
14 import org.eclipse.core.runtime.CoreException;
15
16 import org.eclipse.jface.viewers.IStructuredSelection;
17
18 import org.eclipse.jface.text.ITextSelection;
19
20 import org.eclipse.ui.IWorkbenchSite;
21 import org.eclipse.ui.PlatformUI;
22
23 import org.eclipse.jdt.core.IJavaElement;
24 import org.eclipse.jdt.core.IMethod;
25 import org.eclipse.jdt.core.ITypeRoot;
26 import org.eclipse.jdt.core.JavaModelException;
27
28 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
29 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter;
30 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
31
32 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
33 import org.eclipse.jdt.internal.ui.JavaPlugin;
34 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
35 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
36 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
37 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
38 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
39 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
40
41 /**
42  * Action that replaces method invocations. This action may be invoked
43  * on source or binary methods or method invocations with or without attached source.
44  *
45  * <p>
46  * This class may be instantiated; it is not intended to be subclassed.
47  * </p>
48  *
49  * @since 3.2
50  */

51 public class ReplaceInvocationsAction extends SelectionDispatchAction {
52
53     private JavaEditor fEditor;
54
55     /**
56      * Note: This constructor is for internal use only. Clients should not call this constructor.
57      * @param editor the java editor
58      */

59     public ReplaceInvocationsAction(JavaEditor editor) {
60         this(editor.getEditorSite());
61         fEditor= editor;
62         setEnabled(true);
63     }
64
65     /**
66      * Creates a new <code>ReplaceInvocationsAction</code>.
67      *
68      * @param site the site providing context information for this action
69      */

70     public ReplaceInvocationsAction(IWorkbenchSite site) {
71         super(site);
72         setText(RefactoringMessages.ReplaceInvocationsAction_label);
73         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.REPLACE_INVOCATIONS_ACTION);
74     }
75
76     //---- structured selection --------------------------------------------------
77

78     /*
79      * @see SelectionDispatchAction#selectionChanged(IStructuredSelection)
80      */

81     public void selectionChanged(IStructuredSelection selection) {
82         try {
83             setEnabled(RefactoringAvailabilityTester.isReplaceInvocationsAvailable(selection));
84         } catch (JavaModelException e) {
85             if (JavaModelUtil.isExceptionToBeLogged(e))
86                 JavaPlugin.log(e);
87         }
88     }
89
90     /*
91      * @see SelectionDispatchAction#selectionChanged(ITextSelection)
92      */

93     public void selectionChanged(ITextSelection selection) {
94         setEnabled(true);
95     }
96
97     /**
98      * Note: This method is for internal use only. Clients should not call this method.
99      */

100     public void selectionChanged(JavaTextSelection selection) {
101         try {
102             setEnabled(RefactoringAvailabilityTester.isReplaceInvocationsAvailable(selection));
103         } catch (JavaModelException e) {
104             setEnabled(false);
105         }
106     }
107
108     /*
109      * @see SelectionDispatchAction#run(IStructuredSelection)
110      */

111     public void run(IStructuredSelection selection) {
112         try {
113             Assert.isTrue(RefactoringAvailabilityTester.isReplaceInvocationsAvailable(selection));
114             Object JavaDoc first= selection.getFirstElement();
115             Assert.isTrue(first instanceof IMethod);
116             IMethod method= (IMethod) first;
117             if (ActionUtil.isProcessable(getShell(), method))
118                 RefactoringExecutionStarter.startReplaceInvocationsRefactoring(method, getShell());
119         } catch (CoreException e) {
120             handleException(e);
121         }
122     }
123
124     private void handleException(CoreException e) {
125         ExceptionHandler.handle(e, RefactoringMessages.ReplaceInvocationsAction_dialog_title, RefactoringMessages.ReplaceInvocationsAction_unavailable);
126     }
127
128     /* (non-Javadoc)
129      * Method declared on SelectionDispatchAction
130      */

131     public void run(ITextSelection selection) {
132         try {
133             IJavaElement editorInput= SelectionConverter.getInput(fEditor);
134             if ((editorInput instanceof ITypeRoot)
135                     && ActionUtil.isProcessable(getShell(), editorInput)) {
136                 ITypeRoot typeRoot= (ITypeRoot) editorInput;
137                 RefactoringExecutionStarter.startReplaceInvocationsRefactoring(typeRoot, selection.getOffset(), selection.getLength(), getShell());
138             }
139         } catch (JavaModelException e) {
140             handleException(e);
141         }
142     }
143 }
144
Popular Tags