KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
22 import org.netbeans.jmi.javamodel.Element;
23 import org.netbeans.jmi.javamodel.JavaModelPackage;
24 import org.netbeans.jmi.javamodel.StatementBlock;
25 import org.netbeans.lib.java.parser.ASTree;
26 import org.netbeans.lib.java.parser.ParserTokens;
27 import org.netbeans.lib.java.parser.Token;
28 import org.netbeans.mdr.storagemodel.StorableObject;
29 import org.netbeans.modules.javacore.parser.ASTProvider;
30
31 /**
32  *
33  * @author Martin Matula
34  */

35 public abstract class StatementBlockImpl extends StatementImpl implements StatementBlock {
36     private LightAttrList statements = null;
37     
38     /** Creates a new instance of StatementBlockImpl */
39     public StatementBlockImpl(StorableObject o) {
40         super(o);
41     }
42     
43     /**
44      * Returns the value of attribute statements.
45      * @return Value of attribute statements.
46      */

47     public List getStatements() {
48         if (!childrenInited) {
49             initChildren();
50         }
51         return statements;
52     }
53     
54     void setStatements(List statements) {
55         this.statements = createChildrenList("statements", statements, CHANGED_STATEMENTS); // NOI18N
56
}
57     
58     protected void _delete() {
59         // --- delete components -------------------------------------------
60
if (childrenInited) {
61             deleteChildren(statements);
62         }
63         // --- delete links -----------------------------------------------
64
// no links to delete
65
// --- call super ---------------------------------------
66
super._delete();
67     }
68     
69     public void replaceChild(Element oldElement,Element newElement) {
70         if (childrenInited) {
71             replaceObject(getStatements(),oldElement,newElement);
72         }
73     }
74
75     public List getChildren() {
76         return new ArrayList(getStatements());
77     }
78     
79     protected void initChildren() {
80         childrenInited = false;
81         statements = createChildrenList(statements, "statements", getASTree().getSubTrees(), CHANGED_STATEMENTS, false); // NOI18N
82
childrenInited = true;
83     }
84     
85     String JavaDoc getRawText() {
86         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
87         List statements = getStatements();
88         if (isNew()) {
89             buf.append('{');
90         } else {
91             buf.append(IndentUtil.reformatTokenWithPads(this, getASTree().getFirstToken()));
92         }
93         for (Iterator iter = statements.iterator(); iter.hasNext();) {
94             MetadataElement stat = (MetadataElement) iter.next();
95             buf.append(stat.getSourceText());
96         }
97         if (isNew()) {
98             formatElementPart(BLOCK_CLOSE_CURLY, buf);
99         } else {
100             buf.append(IndentUtil.reformatTokenWithPads(this, getASTree().getLastToken()));
101         }
102         return buf.toString();
103     }
104
105     protected String JavaDoc getIndentation() {
106         Object JavaDoc comp = refImmediateComposite();
107         // for method or for statement block which is not part of another
108
// statement block do not increment the indentation
109
if (comp instanceof SemiPersistentElement) {
110             return ((SemiPersistentElement) comp).getIndentation();
111         } else if (!(comp instanceof StatementBlock)) {
112             return ((TransientElement) comp).getIndentation();
113         } else {
114             return ((TransientElement) comp).getIndentation().concat(INDENTATION);
115         }
116     }
117
118     public void getDiff(List diff) {
119         ASTProvider parser = getParser();
120         ASTree tree = getASTree();
121         ASTree[] children = tree.getSubTrees();
122         if (children == null)
123             children = new ASTree[0];
124         
125         Token startToken = parser.getToken(tree.getLastToken());
126         if (isChanged(CHANGED_STATEMENTS)) {
127             getStatementsDiff(diff, parser, children, getStatements(), getTokenStart(parser, startToken), "", null, 0, true);
128         } else if (isChanged(CHANGED_CHILDREN)) {
129             getElementsDiff(diff, getStatements());
130         }
131     }
132     
133     private static int getTokenStart(ASTProvider parser, Token startToken) {
134         int startOffset = parser.getToken(startToken.getFirstToken()-1).getEndOffset();
135         Token[] pad = startToken.getPadding();
136         if (pad.length > 0) {
137             for (int i = 0; i < pad.length; i++) {
138                 if (pad[i].getType() == ParserTokens.EOL) {
139                     break;
140                 }
141             }
142         }
143         return startOffset;
144         /*
145         Token[] pad = startToken.getPadding();
146         if (pad.length > 0) {
147             startToken = pad[0];
148             for (int i = 0; i < pad.length; i++) {
149                 if (pad[i].getType() == ParserTokens.EOL) {
150                     return pad[i].getStartOffset();
151                 }
152             }
153         }
154         return startToken.getStartOffset();
155          */

156     }
157     
158     protected int getStartOffset(ASTProvider parser, ASTree tree, boolean includePaddings) {
159         Token startToken = parser.getToken(tree.getFirstToken());
160         if (includePaddings) {
161             return getTokenStart(parser, startToken);
162         }
163         return startToken.getStartOffset();
164     }
165     
166     public Element duplicate(JavaModelPackage targetExtent) {
167         return targetExtent.getStatementBlock().createStatementBlock(duplicateList(getStatements(), targetExtent));
168     }
169 }
170
Popular Tags