1 19 package org.netbeans.modules.refactoring.experimental; 20 import java.util.HashSet ; 21 import java.util.Set ; 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 31 public final class IntroduceVariableRefactoring extends AbstractRefactoring { 32 33 private Expression expression; 34 private String variableName; 35 private Type variableType; 36 private boolean replaceAll; 37 private boolean isFinal; 38 39 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 60 public String getVariableName() { 61 return variableName; 62 } 63 64 68 public void setVariableName(String variableName) { 69 this.variableName = variableName; 70 } 71 72 76 public Type getVariableType() { 77 return variableType; 78 } 79 80 84 public void setVariableType(Type varType) { 85 this.variableType = varType; 86 } 87 88 92 public boolean isReplaceAll() { 93 return replaceAll; 94 } 95 96 100 public void setReplaceAll(boolean replaceAll) { 101 this.replaceAll = replaceAll; 102 } 103 104 108 public boolean isFinal() { 109 return isFinal; 110 } 111 112 116 public void setFinal(boolean isFinal) { 117 this.isFinal = isFinal; 118 } 119 120 124 public Expression getExpression() { 125 return expression; 126 } 127 128 protected void setClassPath() { 129 Util.setClassPath((Element) expression); 130 } 131 132 134 private Expression findSelection(Element startElem, Element endElem) { 135 Set parents = new HashSet (); 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 |