KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > actions > MoveInstanceMethodAction


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.internal.ui.refactoring.actions;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.CoreException;
15
16 import org.eclipse.jface.dialogs.MessageDialog;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18
19 import org.eclipse.jface.text.ITextSelection;
20
21 import org.eclipse.ui.IWorkbenchSite;
22 import org.eclipse.ui.PlatformUI;
23
24 import org.eclipse.jdt.core.ICompilationUnit;
25 import org.eclipse.jdt.core.IJavaElement;
26 import org.eclipse.jdt.core.IMethod;
27 import org.eclipse.jdt.core.JavaModelException;
28
29 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
30 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter;
31 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
32
33 import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
34
35 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
36 import org.eclipse.jdt.internal.ui.JavaPlugin;
37 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
38 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
39 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
40 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
41 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
42 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
43
44 public final class MoveInstanceMethodAction 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      * @param editor the java editor
51      */

52     public MoveInstanceMethodAction(JavaEditor editor) {
53         this(editor.getEditorSite());
54         fEditor= editor;
55         setEnabled(SelectionConverter.canOperateOn(fEditor));
56     }
57
58     public MoveInstanceMethodAction(IWorkbenchSite site) {
59         super(site);
60         setText(RefactoringMessages.MoveInstanceMethodAction_Move_Method);
61         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.MOVE_ACTION);
62     }
63
64     /*
65      * @see SelectionDispatchAction#selectionChanged(IStructuredSelection)
66      */

67     public void selectionChanged(IStructuredSelection selection) {
68         try {
69             setEnabled(RefactoringAvailabilityTester.isMoveMethodAvailable(selection));
70         } catch (JavaModelException e) {
71             // http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253
72
if (JavaModelUtil.isExceptionToBeLogged(e))
73                 JavaPlugin.log(e);
74             setEnabled(false);//no ui
75
}
76     }
77
78     public void selectionChanged(ITextSelection selection) {
79         setEnabled(true);
80     }
81
82     /**
83      * Note: This method is for internal use only. Clients should not call this method.
84      */

85     public void selectionChanged(JavaTextSelection selection) {
86         try {
87             setEnabled(RefactoringAvailabilityTester.isMoveMethodAvailable(selection));
88         } catch (CoreException e) {
89             setEnabled(false);
90         }
91     }
92     
93     private static IMethod getSingleSelectedMethod(IStructuredSelection selection) {
94         if (selection.isEmpty() || selection.size() != 1)
95             return null;
96         
97         Object JavaDoc first= selection.getFirstElement();
98         if (! (first instanceof IMethod))
99             return null;
100         return (IMethod) first;
101     }
102     /*
103      * @see SelectionDispatchAction#run(IStructuredSelection)
104      */

105     public void run(IStructuredSelection selection) {
106         try {
107             Assert.isTrue(RefactoringAvailabilityTester.isMoveMethodAvailable(selection));
108             IMethod method= getSingleSelectedMethod(selection);
109             Assert.isNotNull(method);
110             if (!ActionUtil.isEditable(fEditor, getShell(), method))
111                 return;
112             RefactoringExecutionStarter.startMoveMethodRefactoring(method, getShell());
113         } catch (JavaModelException e) {
114             ExceptionHandler.handle(e, getShell(), RefactoringMessages.MoveInstanceMethodAction_dialog_title, RefactoringMessages.MoveInstanceMethodAction_unexpected_exception);
115         }
116     }
117     
118     /* (non-Javadoc)
119      * Method declared on SelectionDispatchAction
120      */

121     public void run(ITextSelection selection) {
122         try {
123             run(selection, SelectionConverter.getInputAsCompilationUnit(fEditor));
124         } catch (JavaModelException e) {
125             ExceptionHandler.handle(e, getShell(), RefactoringMessages.MoveInstanceMethodAction_dialog_title, RefactoringMessages.MoveInstanceMethodAction_unexpected_exception);
126         }
127     }
128
129     private void run(ITextSelection selection, ICompilationUnit cu) throws JavaModelException {
130         Assert.isNotNull(cu);
131         Assert.isTrue(selection.getOffset() >= 0);
132         Assert.isTrue(selection.getLength() >= 0);
133
134         if (!ActionUtil.isEditable(fEditor, getShell(), cu))
135             return;
136
137         IMethod method= getMethod(cu, selection);
138         if (method != null) {
139             RefactoringExecutionStarter.startMoveMethodRefactoring(method, getShell());
140         } else {
141             MessageDialog.openInformation(getShell(), RefactoringMessages.MoveInstanceMethodAction_dialog_title, RefactoringMessages.MoveInstanceMethodAction_No_reference_or_declaration);
142         }
143     }
144
145     private static IMethod getMethod(ICompilationUnit cu, ITextSelection selection) throws JavaModelException {
146         IJavaElement element= SelectionConverter.getElementAtOffset(cu, selection);
147         if (element instanceof IMethod)
148             return (IMethod) element;
149         return null;
150     }
151 }
152
Popular Tags