]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/ui/actions/InlineTempAction.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / ui / actions / InlineTempAction.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 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.swt.widgets.Shell;
14
15 import org.eclipse.jface.viewers.IStructuredSelection;
16
17 import org.eclipse.jface.text.ITextSelection;
18
19 import org.eclipse.ui.IWorkbenchSite;
20 import org.eclipse.ui.PlatformUI;
21
22 import org.eclipse.jdt.core.ICompilationUnit;
23 import org.eclipse.jdt.core.JavaModelException;
24 import org.eclipse.jdt.core.dom.CompilationUnit;
25
26 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
27 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter;
28
29 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
30 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
31 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
32 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
33 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
34 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
35
36 /**
37  * Inlines the value of a local variable at all places where a read reference
38  * is used.
39  *
40  * <p>
41  * This class may be instantiated; it is not intended to be subclassed.
42  * </p>
43  *
44  * @since 2.0
45  *
46  * @noextend This class is not intended to be subclassed by clients.
47  */
48 public class InlineTempAction extends SelectionDispatchAction {
49
50         private JavaEditor fEditor;
51
52         /**
53          * Note: This constructor is for internal use only. Clients should not call this constructor.
54          *
55          * @param editor the java editor
56          *
57          * @noreference This constructor is not intended to be referenced by clients.
58          */
59         public InlineTempAction(JavaEditor editor) {
60                 this(editor.getEditorSite());
61                 fEditor= editor;
62                 setEnabled(SelectionConverter.canOperateOn(fEditor));
63         }
64
65         /* package */ InlineTempAction(IWorkbenchSite site) {
66                 super(site);
67                 setText(RefactoringMessages.InlineTempAction_label);
68                 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.INLINE_ACTION);
69         }
70
71         //---- text selection ----------------------------------------------------------
72
73         /* (non-Javadoc)
74          * Method declared on SelectionDispatchAction
75          */
76         @Override
77         public void selectionChanged(ITextSelection selection) {
78                 setEnabled(true);
79         }
80
81         /**
82          * Note: This method is for internal use only. Clients should not call this method.
83          * 
84          * @param selection the Java text selection
85          * @noreference This method is not intended to be referenced by clients.
86          */
87         @Override
88         public void selectionChanged(JavaTextSelection selection) {
89                 try {
90                         setEnabled(RefactoringAvailabilityTester.isInlineTempAvailable(selection));
91                 } catch (JavaModelException e) {
92                         setEnabled(false);
93                 }
94         }
95
96         /* (non-Javadoc)
97          * Method declared on SelectionDispatchAction
98          */
99         @Override
100         public void run(ITextSelection selection) {
101                 ICompilationUnit input= SelectionConverter.getInputAsCompilationUnit(fEditor);
102                 if (!ActionUtil.isEditable(fEditor))
103                         return;
104                 RefactoringExecutionStarter.startInlineTempRefactoring(input, null, selection, getShell());
105         }
106
107         /*
108          * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.viewers.IStructuredSelection)
109          */
110         @Override
111         public void run(IStructuredSelection selection) {
112                 //do nothing
113         }
114
115         /*
116          * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#selectionChanged(org.eclipse.jface.viewers.IStructuredSelection)
117          */
118         @Override
119         public void selectionChanged(IStructuredSelection selection) {
120                 setEnabled(false);
121         }
122
123         /* package */ boolean tryInlineTemp(ICompilationUnit unit, CompilationUnit node, ITextSelection selection, Shell shell) {
124                 return RefactoringExecutionStarter.startInlineTempRefactoring(unit, node, selection, shell);
125         }
126 }