KickJava   Java API By Example, From Geeks To Geeks.

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


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
15 import org.eclipse.jface.dialogs.MessageDialog;
16 import org.eclipse.jface.viewers.ISelection;
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.ITypeRoot;
26 import org.eclipse.jdt.core.dom.CompilationUnit;
27
28 import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser;
29
30 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
31 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
32 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
33 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
34 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
35 import org.eclipse.jdt.internal.ui.refactoring.actions.InlineConstantAction;
36 import org.eclipse.jdt.internal.ui.refactoring.actions.InlineMethodAction;
37
38 /**
39  * Inlines a method, local variable or a static final field.
40  *
41  * <p>
42  * This class may be instantiated; it is not intended to be subclassed.
43  * </p>
44  *
45  * @since 2.1
46  */

47 public class InlineAction extends SelectionDispatchAction {
48
49     private JavaEditor fEditor;
50     private final InlineTempAction fInlineTemp;
51     private final InlineMethodAction fInlineMethod;
52     private final InlineConstantAction fInlineConstant;
53
54     /**
55      * Creates a new <code>InlineAction</code>. The action requires
56      * that the selection provided by the site's selection provider is of type <code>
57      * org.eclipse.jface.viewers.IStructuredSelection</code>.
58      *
59      * @param site the site providing context information for this action
60      */

61     public InlineAction(IWorkbenchSite site) {
62         super(site);
63         setText(RefactoringMessages.InlineAction_Inline);
64         fInlineTemp = new InlineTempAction(site);
65         fInlineConstant = new InlineConstantAction(site);
66         fInlineMethod = new InlineMethodAction(site);
67         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.INLINE_ACTION);
68     }
69
70     /**
71      * Note: This constructor is for internal use only. Clients should not call this constructor.
72      * @param editor the compilation unit editor
73      */

74     public InlineAction(JavaEditor editor) {
75         //don't want to call 'this' here - it'd create useless action objects
76
super(editor.getEditorSite());
77         setText(RefactoringMessages.InlineAction_Inline);
78         fEditor= editor;
79         fInlineTemp = new InlineTempAction(editor);
80         fInlineConstant = new InlineConstantAction(editor);
81         fInlineMethod = new InlineMethodAction(editor);
82         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.INLINE_ACTION);
83         setEnabled(SelectionConverter.getInputAsCompilationUnit(fEditor) != null);
84     }
85
86     /*
87      * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#selectionChanged(org.eclipse.jface.viewers.ISelection)
88      */

89     public void selectionChanged(ISelection selection) {
90         fInlineConstant.update(selection);
91         fInlineMethod.update(selection);
92         fInlineTemp.update(selection);
93         setEnabled((fInlineTemp.isEnabled() || fInlineConstant.isEnabled() || fInlineMethod.isEnabled()));
94     }
95
96     /*
97      * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.text.ITextSelection)
98      */

99     public void run(ITextSelection selection) {
100         if (!ActionUtil.isEditable(fEditor))
101             return;
102
103         ITypeRoot typeRoot= SelectionConverter.getInputAsTypeRoot(fEditor);
104         if (typeRoot == null)
105             return;
106
107         CompilationUnit node= RefactoringASTParser.parseWithASTProvider(typeRoot, true, null);
108         
109         if (typeRoot instanceof ICompilationUnit) {
110             ICompilationUnit cu= (ICompilationUnit) typeRoot;
111             if (fInlineTemp.isEnabled() && fInlineTemp.tryInlineTemp(cu, node, selection, getShell()))
112                 return;
113
114             if (fInlineConstant.isEnabled() && fInlineConstant.tryInlineConstant(cu, node, selection, getShell()))
115                 return;
116         }
117         //InlineMethod is last (also tries enclosing element):
118
if (fInlineMethod.isEnabled() && fInlineMethod.tryInlineMethod(typeRoot, node, selection, getShell()))
119             return;
120
121         MessageDialog.openInformation(getShell(), RefactoringMessages.InlineAction_dialog_title, RefactoringMessages.InlineAction_select);
122     }
123     
124     /*
125      * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.viewers.IStructuredSelection)
126      */

127     public void run(IStructuredSelection selection) {
128         if (fInlineConstant.isEnabled())
129             fInlineConstant.run(selection);
130         else if (fInlineMethod.isEnabled())
131             fInlineMethod.run(selection);
132         else
133             //inline temp will never be enabled on IStructuredSelection
134
//don't bother running it
135
Assert.isTrue(! fInlineTemp.isEnabled());
136     }
137 }
138
Popular Tags