1 19 20 package org.netbeans.api.java.source.transform; 21 22 import org.netbeans.modules.java.source.engine.RootTree; 23 import org.netbeans.api.java.source.transform.UndoList; 24 import com.sun.source.tree.Tree; 25 import java.util.Map ; 26 import org.netbeans.api.java.source.query.QueryEnvironment; 27 28 import java.util.HashMap ; 29 import org.netbeans.api.java.source.transform.UndoEntry; 30 import org.netbeans.api.java.source.transform.UndoList; 31 import org.netbeans.api.java.source.query.SearchEntry; 32 import org.netbeans.modules.java.source.engine.RootTree; 33 34 39 public final class ChangeSet extends ChangeList { 40 private Translator translator; 41 private UndoList undo; 42 43 public ChangeSet(String refactoring) { 44 super(refactoring); 45 translator = new Translator(); 46 } 47 48 public void attach(QueryEnvironment env) { 49 translator.attach(env); 50 undo = env.getUndoList(); 51 } 52 53 public void release() { 54 translator.release(); 55 undo = null; 56 } 57 58 61 public void rewrite(final Tree oldTree, final Tree newTree) { 62 if(newTree != oldTree) { 63 final Tree lastChange = getChange(oldTree); 64 if (lastChange != null) { 65 final TreeChange lastTreeChange = new TreeChange(lastChange, newTree); 66 undo.addAndApply(new UndoEntry() { 67 @Override 68 public void undo() { 69 remove(lastTreeChange); 70 } 71 @Override 72 public void redo() { 73 addLast(lastTreeChange); 74 } 75 @Override 76 public <T> T getOld(T o) { 77 return (o == newTree) ? (T)lastChange : null; 78 } 79 }); 80 } 81 final TreeChange change = new TreeChange(oldTree, newTree); 82 undo.addAndApply(new UndoEntry() { 83 @Override 84 public void undo() { 85 remove(change); 86 } 87 @Override 88 public void redo() { 89 addLast(change); 90 } 91 @Override 92 public <T> T getOld(T o) { 93 return (o == newTree) ? (T)oldTree : null; 94 } 95 }); 96 addFirst(new TreeChange(oldTree, newTree)); 97 } 98 } 99 100 103 public void rewrite(Change changes) { 104 addFirst(changes); 105 } 106 107 110 public boolean hasChanges() { 111 return size() > 0; 112 } 113 114 119 public RootTree commit(RootTree oldRoot) { 120 if (hasChanges()) { 121 Map <Tree,Tree> changeMap = new HashMap <Tree,Tree>(); 122 addToMap(changeMap); 123 RootTree newRoot = (RootTree)translator.translate(oldRoot, changeMap); 124 return newRoot; 125 } 126 return oldRoot; 127 } 128 129 135 public RootTree rollback(RootTree newRoot) { 136 if (hasChanges()) { 137 Map <Tree,Tree> changeMap = new HashMap <Tree,Tree>(); 138 addToMap(changeMap); 139 RootTree oldRoot = (RootTree)untranslate(newRoot, changeMap); 140 return oldRoot; 141 } 142 return newRoot; 143 } 144 145 private Tree untranslate(Tree tree, Map <Tree,Tree> changeMap) { 146 if(tree==null) return null; 147 Tree repl = getOriginal(tree); 148 return translator.translate(repl != null ? repl : tree, changeMap); 149 } 150 151 Change getResultChanges(SearchEntry se) { 152 for (Change change : this) 153 if (change.hasOriginal(se.tree)) 154 return change; 155 return null; 156 } 157 158 class Translator extends ImmutableTreeTranslator { 159 private Map <Tree,Tree> changeMap; 160 Tree translate(Tree tree, Map <Tree,Tree> changeMap) { 161 this.changeMap = new HashMap <Tree, Tree>(changeMap); 162 return translate(tree); 163 } 164 @Override 165 public Tree translate(Tree tree) { 166 assert changeMap != null; 167 if(tree==null) return null; 168 Tree repl = changeMap.remove(tree); 169 Tree newRepl = super.translate(repl != null ? repl : tree); 170 return newRepl; 171 } 172 }; 173 } 174 | Popular Tags |