Link Here
|
1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2000, 2011 IBM Corporation and others. |
2 |
* Copyright (c) 2000, 2014 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
Link Here
|
7 |
* |
7 |
* |
8 |
* Contributors: |
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
9 |
* IBM Corporation - initial API and implementation |
|
|
10 |
* Timo Kinnunen - Contribution for bug 432147 - [refactoring] Extract Constant displays error message on name of local variable |
10 |
*******************************************************************************/ |
11 |
*******************************************************************************/ |
11 |
package org.eclipse.jdt.ui.actions; |
12 |
package org.eclipse.jdt.ui.actions; |
12 |
|
13 |
|
Link Here
|
14 |
|
15 |
|
15 |
import org.eclipse.ui.PlatformUI; |
16 |
import org.eclipse.ui.PlatformUI; |
16 |
|
17 |
|
|
|
18 |
import org.eclipse.jdt.core.ICompilationUnit; |
19 |
import org.eclipse.jdt.core.JavaModelException; |
20 |
import org.eclipse.jdt.core.SourceRange; |
21 |
import org.eclipse.jdt.core.dom.ASTNode; |
22 |
import org.eclipse.jdt.core.dom.CompilationUnit; |
23 |
import org.eclipse.jdt.core.dom.SimpleName; |
24 |
import org.eclipse.jdt.core.dom.VariableDeclaration; |
25 |
|
26 |
import org.eclipse.jdt.internal.corext.dom.fragments.ASTFragmentFactory; |
27 |
import org.eclipse.jdt.internal.corext.dom.fragments.IASTFragment; |
17 |
import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; |
28 |
import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; |
18 |
import org.eclipse.jdt.internal.corext.refactoring.code.ExtractConstantRefactoring; |
29 |
import org.eclipse.jdt.internal.corext.refactoring.code.ExtractConstantRefactoring; |
|
|
30 |
import org.eclipse.jdt.internal.corext.refactoring.code.PromoteTempToFieldRefactoring; |
31 |
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; |
32 |
import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser; |
19 |
|
33 |
|
20 |
import org.eclipse.jdt.ui.refactoring.RefactoringSaveHelper; |
34 |
import org.eclipse.jdt.ui.refactoring.RefactoringSaveHelper; |
21 |
|
35 |
|
22 |
import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; |
36 |
import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; |
|
|
37 |
import org.eclipse.jdt.internal.ui.JavaPlugin; |
23 |
import org.eclipse.jdt.internal.ui.actions.ActionUtil; |
38 |
import org.eclipse.jdt.internal.ui.actions.ActionUtil; |
24 |
import org.eclipse.jdt.internal.ui.actions.SelectionConverter; |
39 |
import org.eclipse.jdt.internal.ui.actions.SelectionConverter; |
25 |
import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; |
40 |
import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; |
26 |
import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection; |
41 |
import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection; |
27 |
import org.eclipse.jdt.internal.ui.refactoring.ExtractConstantWizard; |
42 |
import org.eclipse.jdt.internal.ui.refactoring.ExtractConstantWizard; |
|
|
43 |
import org.eclipse.jdt.internal.ui.refactoring.PromoteTempWizard; |
28 |
import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; |
44 |
import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; |
29 |
import org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringStarter; |
45 |
import org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringStarter; |
30 |
|
46 |
|
Link Here
|
84 |
public void run(ITextSelection selection) { |
100 |
public void run(ITextSelection selection) { |
85 |
if (!ActionUtil.isEditable(fEditor)) |
101 |
if (!ActionUtil.isEditable(fEditor)) |
86 |
return; |
102 |
return; |
87 |
ExtractConstantRefactoring refactoring= new ExtractConstantRefactoring(SelectionConverter.getInputAsCompilationUnit(fEditor), selection.getOffset(), selection.getLength()); |
103 |
ICompilationUnit unit= SelectionConverter.getInputAsCompilationUnit(fEditor); |
|
|
104 |
CompilationUnit cuNode= RefactoringASTParser.parseWithASTProvider(unit, true, null); |
105 |
int selectionStart= selection.getOffset(); |
106 |
int selectionLength= selection.getLength(); |
107 |
ExtractConstantRefactoring refactoring= new ExtractConstantRefactoring(cuNode, selectionStart, selectionLength); |
108 |
try { |
109 |
CompilationUnitRewrite cuRewrite= new CompilationUnitRewrite(unit, cuNode); |
110 |
SourceRange range= new SourceRange(selectionStart, selectionLength); |
111 |
IASTFragment ast= ASTFragmentFactory.createFragmentForSourceRange(range, cuRewrite.getRoot(), unit); |
112 |
ASTNode node= ast.getAssociatedNode(); |
113 |
if (node instanceof SimpleName && node.getParent() instanceof VariableDeclaration) { |
114 |
ICompilationUnit cunit= SelectionConverter.getInputAsCompilationUnit(fEditor); |
115 |
PromoteTempToFieldRefactoring refactoring2= new PromoteTempToFieldRefactoring(cunit, selection.getOffset(), selection.getLength()); |
116 |
refactoring2.setInitializeIn(PromoteTempToFieldRefactoring.INITIALIZE_IN_FIELD); |
117 |
refactoring2.setDeclareFinal(true); |
118 |
refactoring2.setDeclareStatic(true); |
119 |
refactoring2.setFieldName(refactoring.guessConstantName()); |
120 |
refactoring2.setSelfInitializing(true); |
121 |
new RefactoringStarter().activate(new PromoteTempWizard(refactoring2), getShell(), RefactoringMessages.ConvertLocalToField_title, RefactoringSaveHelper.SAVE_NOTHING); |
122 |
return; |
123 |
} |
124 |
} catch (JavaModelException e) { |
125 |
JavaPlugin.log(e); |
126 |
} |
88 |
new RefactoringStarter().activate(new ExtractConstantWizard(refactoring), getShell(), RefactoringMessages.ExtractConstantAction_extract_constant, RefactoringSaveHelper.SAVE_NOTHING); |
127 |
new RefactoringStarter().activate(new ExtractConstantWizard(refactoring), getShell(), RefactoringMessages.ExtractConstantAction_extract_constant, RefactoringSaveHelper.SAVE_NOTHING); |
89 |
} |
128 |
} |
90 |
} |
129 |
} |