KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > jmiimpl > javamodel > TransientElement


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.javacore.jmiimpl.javamodel;
20
21 import java.lang.ref.Reference JavaDoc;
22 import java.util.*;
23 import org.netbeans.lib.java.parser.ASTree;
24 import org.netbeans.lib.java.parser.Token;
25 import org.netbeans.mdr.storagemodel.StorableObject;
26 import org.netbeans.modules.javacore.JMManager;
27 import org.netbeans.modules.javacore.parser.ASTProvider;
28 import org.netbeans.modules.javacore.parser.ElementInfo;
29 import org.netbeans.modules.javacore.parser.MDRParser;
30 import org.openide.ErrorManager;
31
32 /**
33  *
34  * @author Martin Matula
35  */

36 public abstract class TransientElement extends MetadataElement {
37     private Reference JavaDoc tree;
38     private int firstToken, lastToken;
39     private int astType;
40     private ResourceImpl resource;
41
42     private boolean isNew = false;
43
44     /** Creates a new instance of StatementBlockImpl */
45     public TransientElement(StorableObject o) {
46         super(o);
47     }
48
49     protected final void init(ASTree tree) {
50         MDRParser parser = (MDRParser) tree.getASTContext();
51         if (parser == null) {
52             // issue #46267
53
JMManager.getLog().notify(ErrorManager.INFORMATIONAL, new Exception JavaDoc("getASTContext() returned null for: " + tree)); // NOI18N
54
JMManager.getLog().log("Using Element.getResource() instead of parser.getResource() to retrieve the parent resource...");
55             this.resource = (ResourceImpl) getResource();
56         } else {
57             this.resource = (ResourceImpl) parser.getResource();
58         }
59         this.tree = ElementInfo.createASTReference(tree);
60         this.firstToken = tree.getFirstToken();
61         this.lastToken = tree.getLastToken();
62         this.astType = tree.getType();
63         // go down via containment hierarchy and force ASTInfo renewal
64
if (childrenInited) {
65             initChildren();
66         }
67         resetChange();
68     }
69
70     public final ASTree getASTree() {
71         initCheck();
72         if (tree == null) return null;
73         ASTree result = (ASTree) tree.get();
74         if (result == null) {
75             ASTree parentTree = resource.getASTree();
76             MDRParser parser = (MDRParser) parentTree.getASTContext();
77             result = parser.findTree(parentTree, firstToken, lastToken, astType);
78             this.tree = ElementInfo.createASTReference(result);
79         }
80         return result;
81
82     }
83
84     public final MDRParser getParser() {
85         ASTree tree = getASTree(); // make sure that parser is initialized
86
if (tree == null) return null;
87         return (MDRParser) tree.getASTContext();
88     }
89
90     protected final Object JavaDoc getInternalForm() {
91         return getASTree();
92     }
93
94     protected final boolean isInitialized() {
95         return (tree != null) || isNew;
96     }
97
98     protected final void uninitialize() {
99         tree = null;
100         resource = null;
101         isNew = false;
102     }
103
104     /** Overriding methods must call super.setNew() */
105     protected void setNew() {
106         setChanged();
107         isNew = true;
108         childrenInited = true;
109     }
110
111     protected final boolean isNew() {
112         return isNew;
113     }
114
115     public void getCollectionDiff(List diff, ASTProvider parser, int changeMask, ASTree parentNode, int groupNodeType, Collection newElements, int endOffset, String JavaDoc separator) {
116         ASTree[] oldElements = getChildNodes(parentNode, groupNodeType);
117         getCollectionDiff(diff, parser, changeMask, oldElements, newElements, endOffset, separator);
118     }
119
120     protected String JavaDoc getIndentation() {
121         super.getIndentation();
122         Object JavaDoc comp = refImmediateComposite();
123         if (comp instanceof SemiPersistentElement) {
124             return ((SemiPersistentElement) refImmediateComposite()).getIndentation().concat(INDENTATION);
125         } else {
126             return ((TransientElement) refImmediateComposite()).getIndentation().concat(INDENTATION);
127         }
128     }
129
130     protected void printListOfItems (String JavaDoc origSrc, StringBuffer JavaDoc buf, ASTree[] children, Map oldItems, List items) {
131         ASTProvider parser = getParser();
132         if (oldItems == null) {
133             oldItems = new HashMap();
134             for (int x = 0; x < children.length; x++) {
135                 Token tok = (x < children.length - 1) ?
136                     parser.getToken(children[x].getLastToken() + 1) : null;
137                 oldItems.put(children[x], tok);
138             }
139         }
140         Iterator iter = items.iterator();
141         TransientElement item = null;
142         int x;
143         for (x = 0; iter.hasNext(); x++) {
144             item = (TransientElement) iter.next();
145             ASTree itemTree = item.getASTree();
146             if (!((x < children.length) && (itemTree == children[x]) && !item.isChanged())) {
147                 x--;
148                 break;
149             }
150         }
151         if ((x > -1) && (item != null)) {
152             Token token = null;
153             if (x == (children.length - 1)) {
154                 item = null;
155                 token = parser.getToken(children[x].getLastToken());
156             } else {
157                 token = parser.getToken(children[x].getLastToken() + 1);
158             }
159             int start = parser.getToken(children[0].getFirstToken()).getStartOffset();
160             buf.append(origSrc.substring(start, token.getEndOffset()));
161         }
162
163         while (item != null) {
164             ASTree itemTree = item.getASTree();
165             Token tok = (Token) oldItems.get (itemTree);
166             buf.append(item.getSourceText());
167             if (iter.hasNext()) {
168                 if (tok != null) {
169                     buf.append(origSrc.substring(tok.getStartOffset(), tok.getEndOffset()));
170                 } else {
171                     buf.append(", "); // NOI18N
172
}
173             }
174             item = iter.hasNext() ? (StatementImpl) iter.next() : null;
175         } // while
176
}
177
178     protected void invalidate() {
179         super._delete();
180     }
181
182     /** Part ot the rollback mechanism.
183      *
184      * In the case of any rollback, all classes should be rebuilt from ASTrees.
185      * The layout for method is the next one:
186      * protected void reset() {
187      * .. do your own job...
188      * super.reset()
189      * }
190      */

191     protected final void rollback() {
192         // body elements are not preserved after rollback
193
if (isValid()) {
194             _delete();
195         }
196     }
197 }
198
Popular Tags