001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.ui.actions;
011:
012: import org.eclipse.core.runtime.Assert;
013: import org.eclipse.core.runtime.CoreException;
014:
015: import org.eclipse.jface.viewers.IStructuredSelection;
016:
017: import org.eclipse.jface.text.ITextSelection;
018:
019: import org.eclipse.ui.IWorkbenchSite;
020: import org.eclipse.ui.PlatformUI;
021:
022: import org.eclipse.jdt.core.IJavaElement;
023: import org.eclipse.jdt.core.IMethod;
024: import org.eclipse.jdt.core.ITypeRoot;
025: import org.eclipse.jdt.core.JavaModelException;
026:
027: import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
028: import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter;
029: import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
030:
031: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
032: import org.eclipse.jdt.internal.ui.JavaPlugin;
033: import org.eclipse.jdt.internal.ui.actions.ActionUtil;
034: import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
035: import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
036: import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
037: import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
038: import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
039:
040: /**
041: * Action that replaces method invocations. This action may be invoked
042: * on source or binary methods or method invocations with or without attached source.
043: *
044: * <p>
045: * This class may be instantiated; it is not intended to be subclassed.
046: * </p>
047: *
048: * @since 3.2
049: */
050: public class ReplaceInvocationsAction extends SelectionDispatchAction {
051:
052: private JavaEditor fEditor;
053:
054: /**
055: * Note: This constructor is for internal use only. Clients should not call this constructor.
056: * @param editor the java editor
057: */
058: public ReplaceInvocationsAction(JavaEditor editor) {
059: this (editor.getEditorSite());
060: fEditor = editor;
061: setEnabled(true);
062: }
063:
064: /**
065: * Creates a new <code>ReplaceInvocationsAction</code>.
066: *
067: * @param site the site providing context information for this action
068: */
069: public ReplaceInvocationsAction(IWorkbenchSite site) {
070: super (site);
071: setText(RefactoringMessages.ReplaceInvocationsAction_label);
072: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
073: IJavaHelpContextIds.REPLACE_INVOCATIONS_ACTION);
074: }
075:
076: //---- structured selection --------------------------------------------------
077:
078: /*
079: * @see SelectionDispatchAction#selectionChanged(IStructuredSelection)
080: */
081: public void selectionChanged(IStructuredSelection selection) {
082: try {
083: setEnabled(RefactoringAvailabilityTester
084: .isReplaceInvocationsAvailable(selection));
085: } catch (JavaModelException e) {
086: if (JavaModelUtil.isExceptionToBeLogged(e))
087: JavaPlugin.log(e);
088: }
089: }
090:
091: /*
092: * @see SelectionDispatchAction#selectionChanged(ITextSelection)
093: */
094: public void selectionChanged(ITextSelection selection) {
095: setEnabled(true);
096: }
097:
098: /**
099: * Note: This method is for internal use only. Clients should not call this method.
100: */
101: public void selectionChanged(JavaTextSelection selection) {
102: try {
103: setEnabled(RefactoringAvailabilityTester
104: .isReplaceInvocationsAvailable(selection));
105: } catch (JavaModelException e) {
106: setEnabled(false);
107: }
108: }
109:
110: /*
111: * @see SelectionDispatchAction#run(IStructuredSelection)
112: */
113: public void run(IStructuredSelection selection) {
114: try {
115: Assert.isTrue(RefactoringAvailabilityTester
116: .isReplaceInvocationsAvailable(selection));
117: Object first = selection.getFirstElement();
118: Assert.isTrue(first instanceof IMethod);
119: IMethod method = (IMethod) first;
120: if (ActionUtil.isProcessable(getShell(), method))
121: RefactoringExecutionStarter
122: .startReplaceInvocationsRefactoring(method,
123: getShell());
124: } catch (CoreException e) {
125: handleException(e);
126: }
127: }
128:
129: private void handleException(CoreException e) {
130: ExceptionHandler
131: .handle(
132: e,
133: RefactoringMessages.ReplaceInvocationsAction_dialog_title,
134: RefactoringMessages.ReplaceInvocationsAction_unavailable);
135: }
136:
137: /* (non-Javadoc)
138: * Method declared on SelectionDispatchAction
139: */
140: public void run(ITextSelection selection) {
141: try {
142: IJavaElement editorInput = SelectionConverter
143: .getInput(fEditor);
144: if ((editorInput instanceof ITypeRoot)
145: && ActionUtil
146: .isProcessable(getShell(), editorInput)) {
147: ITypeRoot typeRoot = (ITypeRoot) editorInput;
148: RefactoringExecutionStarter
149: .startReplaceInvocationsRefactoring(typeRoot,
150: selection.getOffset(), selection
151: .getLength(), getShell());
152: }
153: } catch (JavaModelException e) {
154: handleException(e);
155: }
156: }
157: }
|