KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > transform > ChangeSet


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
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 JavaDoc;
26 import org.netbeans.api.java.source.query.QueryEnvironment;
27
28 import java.util.HashMap JavaDoc;
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 /**
35  * A ChangeSet maintains a list of changes, built during a
36  * translate operation. Once all of the changes have been entered,
37  * the ChangeSet is committed to a root node to rewrite it.
38  */

39 public final class ChangeSet extends ChangeList {
40     private Translator translator;
41     private UndoList undo;
42     
43     public ChangeSet(String JavaDoc 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     /**
59      * Adds a change, mapping an original (old) tree to a replacement (new) tree.
60      */

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 JavaDoc
68                     public void undo() {
69                         remove(lastTreeChange);
70                     }
71                     @Override JavaDoc
72                     public void redo() {
73                         addLast(lastTreeChange);
74                     }
75                     @Override JavaDoc
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 JavaDoc
84                 public void undo() {
85                     remove(change);
86                 }
87                 @Override JavaDoc
88                 public void redo() {
89                     addLast(change);
90                 }
91                 @Override JavaDoc
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     /**
101      * Adds a Change instance.
102      */

103     public void rewrite(Change changes) {
104         addFirst(changes);
105     }
106
107     /**
108      * Returns true if there are any changes in this change set.
109      */

110     public boolean hasChanges() {
111         return size() > 0;
112     }
113
114     /**
115      * Commit the queued changes to a specified root node, returning
116      * a rewritten root containing the changes. If this set doesn't
117      * contain any changes, then the original node is returned.
118      */

119     public RootTree commit(RootTree oldRoot) {
120         if (hasChanges()) {
121             Map JavaDoc<Tree,Tree> changeMap = new HashMap JavaDoc<Tree,Tree>();
122             addToMap(changeMap);
123             RootTree newRoot = (RootTree)translator.translate(oldRoot, changeMap);
124             return newRoot;
125         }
126         return oldRoot;
127     }
128
129     /**
130      * Rollback any changes from a specified root node, returning
131      * a rewritten root without these changes. If this set doesn't
132      * contain any changes, or if none of them exist in the original node
133      * then the original node is returned.
134      */

135     public RootTree rollback(RootTree newRoot) {
136         if (hasChanges()) {
137             Map JavaDoc<Tree,Tree> changeMap = new HashMap JavaDoc<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 JavaDoc<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 JavaDoc<Tree,Tree> changeMap;
160         Tree translate(Tree tree, Map JavaDoc<Tree,Tree> changeMap) {
161             this.changeMap = new HashMap JavaDoc<Tree, Tree>(changeMap);
162             return translate(tree);
163         }
164         @Override JavaDoc
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