KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > experimental > IntroduceVariableRefactoring


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.refactoring.experimental;
20 import java.util.HashSet JavaDoc;
21 import java.util.Set JavaDoc;
22 import org.netbeans.jmi.javamodel.*;
23 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
24 import org.netbeans.modules.refactoring.classpath.Util;
25 import org.netbeans.modules.refactoring.api.*;
26 import org.openide.filesystems.FileObject;
27
28 /**
29  * @author Daniel Prusa
30  */

31 public final class IntroduceVariableRefactoring extends AbstractRefactoring {
32
33     private Expression expression;
34     private String JavaDoc variableName;
35     private Type variableType;
36     private boolean replaceAll;
37     private boolean isFinal;
38     
39     /**
40      * Creates a new instance of IntroduceVariableRefactoring
41      * @param expression Expression the new local variable should be introduced for
42      */

43     public IntroduceVariableRefactoring(Element elem, int startOffset, int endOffset) {
44         if (startOffset == -1 || endOffset == -1) {
45             if (elem instanceof Expression) {
46                 this.expression = (Expression) elem;
47             }
48         } else {
49             Resource resource = elem.getResource();
50             Element startElem = resource.getElementByOffset(startOffset);
51             Element endElem = resource.getElementByOffset(endOffset-1);
52             this.expression = findSelection(startElem, endElem);
53         }
54     }
55     
56     /**
57      * Getter for property variableName
58      * @return Value of property variableName
59      */

60     public String JavaDoc getVariableName() {
61         return variableName;
62     }
63     
64     /**
65      * Setter for propety variableName
66      * @param variableName New value of property variableName
67      */

68     public void setVariableName(String JavaDoc variableName) {
69         this.variableName = variableName;
70     }
71      
72     /**
73      * Getter for property variableType
74      * @return Value of property variableType
75      */

76     public Type getVariableType() {
77         return variableType;
78     }
79     
80     /**
81      * Setter for propety variableType
82      * @param varType New value of property variableType
83      */

84     public void setVariableType(Type varType) {
85         this.variableType = varType;
86     }
87     
88     /**
89      * Getter for propety replaceAll
90      * @return Value of property replaceAll
91      */

92     public boolean isReplaceAll() {
93         return replaceAll;
94     }
95     
96     /**
97      * Setter for propety replaceAll
98      * @param replaceAll New value of property replaceAll
99      */

100     public void setReplaceAll(boolean replaceAll) {
101         this.replaceAll = replaceAll;
102     }
103     
104     /**
105      * Getter for propety isFinal
106      * @return Value of property isFinal
107      */

108     public boolean isFinal() {
109         return isFinal;
110     }
111     
112     /**
113      * Setter for propety isFinal
114      * @param replaceAll New value of property isFinal
115      */

116     public void setFinal(boolean isFinal) {
117         this.isFinal = isFinal;
118     }
119     
120     /**
121      * Getter for property expression
122      * @return expression
123      */

124     public Expression getExpression() {
125         return expression;
126     }
127     
128     protected void setClassPath() {
129         Util.setClassPath((Element) expression);
130     }
131
132     // helper methods ..........................................................
133

134     private Expression findSelection(Element startElem, Element endElem) {
135         Set JavaDoc parents = new HashSet JavaDoc();
136         Element elem = startElem;
137         while (elem instanceof Expression) {
138             parents.add(elem);
139             elem = (Element) elem.refImmediateComposite();
140         }
141         elem = endElem;
142         while (elem instanceof Expression) {
143             if (parents.contains(elem)) {
144                 return (Expression)elem;
145             }
146             elem = (Element) elem.refImmediateComposite();
147         }
148         return null;
149     }
150     
151 }
152
Popular Tags