KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-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.CoreException;
14 import org.eclipse.core.runtime.NullProgressMonitor;
15
16 import org.eclipse.jdt.core.ICompilationUnit;
17 import org.eclipse.jdt.core.IJavaElement;
18 import org.eclipse.jdt.core.JavaModelException;
19
20 import org.eclipse.jface.text.ITextSelection;
21
22 import org.eclipse.jdt.internal.corext.refactoring.rename.RenameTempRefactoring;
23
24 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
25 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
26 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
27 import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;
28 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
29 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
30 import org.eclipse.jdt.internal.ui.refactoring.reorg.RenameRefactoringWizard;
31 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
32
33 import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
34
35 import org.eclipse.ltk.core.refactoring.Refactoring;
36 import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
37
38 public class RenameTempAction extends SelectionDispatchAction {
39
40     private final CompilationUnitEditor fEditor;
41     private static final String JavaDoc DIALOG_MESSAGE_TITLE= RefactoringMessages.getString("RenameTempAction.rename_Local_Variable"); //$NON-NLS-1$
42

43     public RenameTempAction(CompilationUnitEditor editor) {
44         super(editor.getEditorSite());
45         setText(RefactoringMessages.getString("RenameTempAction.rename_Local_Variable"));//$NON-NLS-1$
46
fEditor= editor;
47         setEnabled(fEditor != null && getCompilationUnit() != null);
48     }
49     
50     private static RenameTempRefactoring createRefactoring(ICompilationUnit cunit, ITextSelection selection) {
51         return RenameTempRefactoring.create(cunit, selection.getOffset(), selection.getLength());
52     }
53     
54     private static RefactoringWizard createWizard(RenameTempRefactoring refactoring) {
55         String JavaDoc message= RefactoringMessages.getString("RenameTempAction.choose_new_name"); //$NON-NLS-1$
56
String JavaDoc wizardPageHelp= IJavaHelpContextIds.RENAME_TEMP_WIZARD_PAGE;
57         String JavaDoc pageTitle= RefactoringMessages.getString("RenameTempAction.rename_Local_Variable");//$NON-NLS-1$
58
RenameRefactoringWizard result= new RenameRefactoringWizard(refactoring, pageTitle, message, null, wizardPageHelp);
59         return result;
60     }
61     
62     public void selectionChanged(ITextSelection selection) {
63         setEnabled(true);
64     }
65     
66     /**
67      * Note: This method is for internal use only. Clients should not call this method.
68      */

69     public void selectionChanged(JavaTextSelection selection) {
70         try {
71             IJavaElement[] elements= selection.resolveElementAtOffset();
72             if (elements.length != 1) {
73                 setEnabled(false);
74             } else {
75                 setEnabled(RenameTempRefactoring.isAvailable(elements[0]));
76             }
77         } catch (CoreException e) {
78             setEnabled(false);
79         }
80     }
81     
82     public boolean canRun(ITextSelection selection) {
83         selectionChanged(selection);
84         if (! isEnabled())
85             return false;
86         
87         if (getCompilationUnit() == null)
88             return false;
89         Refactoring renameTempRefactoring= createRefactoring(getCompilationUnit(), selection);
90         if (renameTempRefactoring == null)
91             return false;
92         try {
93             return (renameTempRefactoring.checkInitialConditions(new NullProgressMonitor()).isOK());
94         } catch(CoreException e) {
95             ExceptionHandler.handle(e, RefactoringMessages.getString("RenameTempAction.rename_Local_Variable"), RefactoringMessages.getString("RenameTempAction.exception"));//$NON-NLS-1$ //$NON-NLS-2$
96
return false;
97         }
98     }
99     
100     public void run(ITextSelection selection) {
101         try{
102             ICompilationUnit input= SelectionConverter.getInputAsCompilationUnit(fEditor);
103             if (!ActionUtil.isProcessable(getShell(), input))
104                 return;
105             RenameTempRefactoring refactoring= createRefactoring(input, selection);
106             if (refactoring == null)
107                 return;
108             new RefactoringStarter().activate(refactoring, createWizard(refactoring), getShell(), DIALOG_MESSAGE_TITLE, false);
109         } catch (JavaModelException e){
110             ExceptionHandler.handle(e, DIALOG_MESSAGE_TITLE, RefactoringMessages.getString("NewTextRefactoringAction.exception")); //$NON-NLS-1$
111
}
112     }
113         
114     private ICompilationUnit getCompilationUnit(){
115         return SelectionConverter.getInputAsCompilationUnit(fEditor);
116     }
117 }
118
Popular Tags