KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
36
37 /**
38  * Inlines the value of a local variable at all places where a read reference
39  * is used.
40  *
41  * <p>
42  * This class may be instantiated; it is not intended to be subclassed.
43  * </p>
44  *
45  * @since 2.0
46  */

47 public class InlineTempAction extends SelectionDispatchAction {
48
49     private JavaEditor fEditor;
50
51     /**
52      * Note: This constructor is for internal use only. Clients should not call this constructor.
53      *
54      * @param editor the java editor
55      */

56     public InlineTempAction(JavaEditor editor) {
57         this(editor.getEditorSite());
58         fEditor= editor;
59         setEnabled(SelectionConverter.canOperateOn(fEditor));
60     }
61     
62     /* package */ InlineTempAction(IWorkbenchSite site) {
63         super(site);
64         setText(RefactoringMessages.InlineTempAction_label);
65         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.INLINE_ACTION);
66     }
67     
68     //---- text selection ----------------------------------------------------------
69

70     /* (non-Javadoc)
71      * Method declared on SelectionDispatchAction
72      */

73     public void selectionChanged(ITextSelection selection) {
74         setEnabled(true);
75     }
76     
77     /**
78      * Note: This method is for internal use only. Clients should not call this method.
79      * @param selection
80      */

81     public void selectionChanged(JavaTextSelection selection) {
82         try {
83             setEnabled(RefactoringAvailabilityTester.isInlineTempAvailable(selection));
84         } catch (JavaModelException e) {
85             setEnabled(false);
86         }
87     }
88     
89     /* (non-Javadoc)
90      * Method declared on SelectionDispatchAction
91      */

92     public void run(ITextSelection selection) {
93         try{
94             ICompilationUnit input= SelectionConverter.getInputAsCompilationUnit(fEditor);
95             if (!ActionUtil.isEditable(fEditor))
96                 return;
97             RefactoringExecutionStarter.startInlineTempRefactoring(input, null, selection, getShell());
98         } catch (JavaModelException e){
99             ExceptionHandler.handle(e, RefactoringMessages.InlineTempAction_inline_temp, RefactoringMessages.NewTextRefactoringAction_exception);
100         }
101     }
102
103     /*
104      * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.viewers.IStructuredSelection)
105      */

106     public void run(IStructuredSelection selection) {
107         //do nothing
108
}
109
110     /*
111      * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#selectionChanged(org.eclipse.jface.viewers.IStructuredSelection)
112      */

113     public void selectionChanged(IStructuredSelection selection) {
114         setEnabled(false);
115     }
116
117     /* package */ boolean tryInlineTemp(ICompilationUnit unit, CompilationUnit node, ITextSelection selection, Shell shell) {
118         try {
119             if (RefactoringExecutionStarter.startInlineTempRefactoring(unit, node, selection, shell)) {
120                 return true;
121             }
122         } catch (JavaModelException exception) {
123             ExceptionHandler.handle(exception, RefactoringMessages.InlineTempAction_inline_temp, RefactoringMessages.NewTextRefactoringAction_exception);
124         }
125         return false;
126     }
127 }
128
Popular Tags