KickJava   Java API By Example, From Geeks To Geeks.

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


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.api.java.source.transform.UndoList;
23 import org.netbeans.api.java.source.transform.UndoList;
24 import org.netbeans.api.java.source.query.QueryException;
25 import org.netbeans.api.java.source.query.SearchEntry;
26 import org.netbeans.api.java.source.query.SearchResult;
27 import org.netbeans.api.java.source.query.QueryResult;
28 import org.netbeans.api.java.source.query.SourceSelection;
29 import com.sun.source.tree.CompilationUnitTree;
30 import com.sun.source.tree.Tree;
31 import javax.lang.model.element.Element;
32 import java.io.IOException JavaDoc;
33 import org.netbeans.modules.java.source.engine.DefaultSourceSelection;
34
35 /**
36  * An extension of SearchResult that can return a selection of the original
37  * source code.
38  */

39 public class TransformResult extends SearchResult {
40     ChangeSet changeSet;
41     
42     /** Creates a new instance of TransformResult */
43     public TransformResult(Transformer transformer, String JavaDoc title, ChangeSet changeSet) {
44         super(transformer, title);
45         this.changeSet = changeSet;
46     }
47
48     @Override JavaDoc
49     public QueryResult getResult(int row) {
50         SearchEntry se = getResults()[row];
51         if (changeSet.hasOriginal(se.tree))
52             return new TransformerResultImpl(se);
53         else
54             return super.getResult(row);
55     }
56     
57     public ChangeSet getChangeSet() {
58         return changeSet;
59     }
60     
61     protected SourceSelection makeSelection(Element sym, Tree tree, Tree root, final boolean original) {
62         final CompilationUnitTree topL = getTopLevel(sym, tree, root);
63         int start = model.getStartPos(tree);
64         int end = model.getEndPos(tree, topL);
65         String JavaDoc path = topL.getSourceFile().toUri().getPath();
66         return new DefaultSourceSelection(sym, start, model.getPos(tree), end, path) {
67             public String JavaDoc getSource() {
68                 return original ? fromSource(topL) : toSource(topL);
69             }
70         };
71     }
72     
73     @Override JavaDoc
74     protected CompilationUnitTree getTopLevel(Element sym, Tree tree, Tree root) {
75         CompilationUnitTree tl = super.getTopLevel(sym, tree, root);
76         if (tl == null) {
77             // code has been deleted, use outer class
78
tree = env.getTrees().getTree(env.getElementUtilities().outermostTypeElement(sym));
79             tl = super.getTopLevel(sym, tree, root);
80         }
81     return tl;
82     }
83
84     protected String JavaDoc toSource(CompilationUnitTree tl) {
85         try {
86             return env.toSource(tl);
87         } catch (IOException JavaDoc e) {
88             // commit failed...
89
throw new QueryException(e);
90         }
91     }
92     
93     private class TransformerResultImpl implements TransformerResult {
94         private SearchEntry se;
95         TransformerResultImpl(SearchEntry se) {
96         this.se = se;
97     }
98     public SourceSelection getSourceSelection() {
99         Tree root = env.getRootNode();
100         return makeSelection(se.sym, se.tree, root, false);
101     }
102     public String JavaDoc getNote() {
103         return se.getNote();
104     }
105     public SourceSelection getOriginalSourceSelection() {
106         UndoList undo = env.getUndoList();
107         Tree root = env.getRootNode();
108         Tree oldRoot = (Tree)undo.getOld(root);
109         assert oldRoot != null;
110             Tree searchTree = se.tree;
111         Tree oldTree = changeSet.hasOriginal(searchTree) ?
112         changeSet.getOriginal(searchTree) : searchTree;
113         return makeSelection(se.sym, oldTree, oldRoot, true);
114     }
115     public boolean equals(Object JavaDoc o) {
116         if (this == o)
117         return true;
118         return (o instanceof TransformerResultImpl) ?
119         (se.equals(((TransformerResultImpl)o).se)) : false;
120     }
121         public int hashCode() {
122             return se.hashCode();
123         }
124
125         public Change getChanges() {
126             assert se.tree != null;
127             return changeSet.getResultChanges(se);
128         }
129
130         public String JavaDoc getRefactoringDescription() {
131             return ((Transformer)se.query).getRefactoringDescription();
132         }
133
134         public String JavaDoc getQueryDescription() {
135             return se.query.getQueryDescription();
136         }
137     }
138 }
139
Popular Tags