KickJava   Java API By Example, From Geeks To Geeks.

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


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.internal.ui.refactoring.actions;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.swt.widgets.Shell;
16
17 import org.eclipse.jface.dialogs.MessageDialog;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19
20 import org.eclipse.jface.text.ITextSelection;
21
22 import org.eclipse.ui.IWorkbenchSite;
23 import org.eclipse.ui.PlatformUI;
24
25 import org.eclipse.jdt.core.IMethod;
26 import org.eclipse.jdt.core.ISourceRange;
27 import org.eclipse.jdt.core.ITypeRoot;
28 import org.eclipse.jdt.core.JavaModelException;
29 import org.eclipse.jdt.core.dom.CompilationUnit;
30
31 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
32 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter;
33 import org.eclipse.jdt.internal.corext.refactoring.util.JavaElementUtil;
34 import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser;
35 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
36
37 import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
38
39 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
40 import org.eclipse.jdt.internal.ui.JavaPlugin;
41 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
42 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
43 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
44 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
45 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
46 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
47
48 /**
49  * Inlines a method.
50  *
51  * <p>
52  * This class may be instantiated; it is not intended to be subclassed.
53  * </p>
54  */

55 public class InlineMethodAction extends SelectionDispatchAction {
56
57     private JavaEditor fEditor;
58     
59     /**
60      * Note: This constructor is for internal use only. Clients should not call this constructor.
61      * @param editor the java editor
62      */

63     public InlineMethodAction(JavaEditor editor) {
64         this(editor.getEditorSite());
65         fEditor= editor;
66         setEnabled(SelectionConverter.canOperateOn(fEditor));
67     }
68
69     public InlineMethodAction(IWorkbenchSite site) {
70         super(site);
71         setText(RefactoringMessages.InlineMethodAction_inline_Method);
72         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.INLINE_ACTION);
73     }
74
75     //---- structured selection ----------------------------------------------
76

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

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

92     public void run(IStructuredSelection selection) {
93         try {
94             Assert.isTrue(RefactoringAvailabilityTester.isInlineMethodAvailable(selection));
95             IMethod method= (IMethod) selection.getFirstElement();
96             ISourceRange nameRange= method.getNameRange();
97             run(nameRange.getOffset(), nameRange.getLength(), method.getTypeRoot());
98         } catch (JavaModelException e) {
99             ExceptionHandler.handle(e, getShell(), RefactoringMessages.InlineMethodAction_dialog_title, RefactoringMessages.InlineMethodAction_unexpected_exception);
100         }
101     }
102
103     /*
104      * @see SelectionDispatchAction#selectionChanged(ITextSelection)
105      */

106     public void selectionChanged(ITextSelection selection) {
107         setEnabled(true);
108     }
109     
110     /**
111      * Note: This method is for internal use only. Clients should not call this method.
112      * @param selection
113      */

114     public void selectionChanged(JavaTextSelection selection) {
115         try {
116             setEnabled(RefactoringAvailabilityTester.isInlineMethodAvailable(selection));
117         } catch (JavaModelException e) {
118             setEnabled(false);
119         }
120     }
121     
122     /* (non-Javadoc)
123      * Method declared on SelectionDispatchAction
124      */

125     public void run(ITextSelection selection) {
126         ITypeRoot typeRoot= SelectionConverter.getInputAsTypeRoot(fEditor);
127         if (typeRoot == null)
128             return;
129         if (! JavaElementUtil.isSourceAvailable(typeRoot))
130             return;
131         run(selection.getOffset(), selection.getLength(), typeRoot);
132     }
133
134     private void run(int offset, int length, ITypeRoot typeRoot) {
135         if (!ActionUtil.isEditable(fEditor, getShell(), typeRoot))
136             return;
137         try {
138             CompilationUnit compilationUnit= RefactoringASTParser.parseWithASTProvider(typeRoot, true, null);
139             if (! RefactoringExecutionStarter.startInlineMethodRefactoring(typeRoot, compilationUnit, offset, length, getShell())) {
140                 MessageDialog.openInformation(getShell(), RefactoringMessages.InlineMethodAction_dialog_title, RefactoringMessages.InlineMethodAction_no_method_invocation_or_declaration_selected);
141             }
142         } catch (JavaModelException e) {
143             ExceptionHandler.handle(e, getShell(), RefactoringMessages.InlineMethodAction_dialog_title, RefactoringMessages.InlineMethodAction_unexpected_exception);
144         }
145     }
146
147     public boolean tryInlineMethod(ITypeRoot typeRoot, CompilationUnit node, ITextSelection selection, Shell shell) {
148         try {
149             if (RefactoringExecutionStarter.startInlineMethodRefactoring(typeRoot, node, selection.getOffset(), selection.getLength(), shell)) {
150                 return true;
151             }
152         } catch (JavaModelException exception) {
153             JavaPlugin.log(exception);
154         }
155         return false;
156     }
157 }
158
Popular Tags