KickJava   Java API By Example, From Geeks To Geeks.

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


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.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.IClassFile;
24 import org.eclipse.jdt.core.ICompilationUnit;
25 import org.eclipse.jdt.core.IMethod;
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 introduces an indirection for a certain method. 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 IntroduceIndirectionAction 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 compilation unit editor
58      */

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

70     public IntroduceIndirectionAction(IWorkbenchSite site) {
71         super(site);
72         setText(RefactoringMessages.IntroduceIndirectionAction_title);
73         setToolTipText(RefactoringMessages.IntroduceIndirectionAction_tooltip);
74         setDescription(RefactoringMessages.IntroduceIndirectionAction_description);
75         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.INTRODUCE_INDIRECTION_ACTION);
76     }
77
78     //---- structured selection --------------------------------------------------
79

80     /*
81      * @see SelectionDispatchAction#selectionChanged(IStructuredSelection)
82      */

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

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

102     public void selectionChanged(JavaTextSelection selection) {
103         try {
104             setEnabled(RefactoringAvailabilityTester.isIntroduceIndirectionAvailable(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.isIntroduceIndirectionAvailable(selection));
116             Object JavaDoc first= selection.getFirstElement();
117             Assert.isTrue(first instanceof IMethod);
118             run((IMethod) first);
119         } catch (CoreException e) {
120             ExceptionHandler.handle(e, RefactoringMessages.IntroduceIndirectionAction_dialog_title, RefactoringMessages.IntroduceIndirectionAction_unknown_exception);
121         }
122     }
123
124     /* (non-Javadoc)
125      * Method declared on SelectionDispatchAction
126      */

127     public void run(ITextSelection selection) {
128         try {
129             Object JavaDoc editorInput= SelectionConverter.getInput(fEditor);
130             if (editorInput instanceof ICompilationUnit)
131                 run(selection.getOffset(), selection.getLength(), (ICompilationUnit) editorInput);
132             else if (editorInput instanceof IClassFile)
133                 run(selection.getOffset(), selection.getLength(), (IClassFile) editorInput);
134         } catch (JavaModelException e) {
135             ExceptionHandler.handle(e, getShell(), RefactoringMessages.IntroduceIndirectionAction_dialog_title, RefactoringMessages.IntroduceIndirectionAction_unknown_exception);
136         }
137     }
138
139     private void run(int offset, int length, ICompilationUnit unit) throws JavaModelException {
140         if (!ActionUtil.isEditable(fEditor, getShell(), unit))
141             return;
142         RefactoringExecutionStarter.startIntroduceIndirectionRefactoring(unit, offset, length, getShell());
143     }
144
145     private void run(int offset, int length, IClassFile file) throws JavaModelException {
146         if (!ActionUtil.isEditable(fEditor, getShell(), file))
147             return;
148         RefactoringExecutionStarter.startIntroduceIndirectionRefactoring(file, offset, length, getShell());
149     }
150
151     private void run(IMethod method) throws JavaModelException {
152         if (!ActionUtil.isEditable(fEditor, getShell(), method))
153             return;
154         RefactoringExecutionStarter.startIntroduceIndirectionRefactoring(method, getShell());
155     }
156 }
157
Popular Tags