KickJava   Java API By Example, From Geeks To Geeks.

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


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.Case;
23 import org.netbeans.jmi.javamodel.Element;
24 import org.netbeans.jmi.javamodel.Expression;
25 import org.netbeans.jmi.javamodel.JavaModelPackage;
26 import org.netbeans.lib.java.parser.ASTree;
27 import org.netbeans.lib.java.parser.ASTreeTypes;
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 CaseImpl extends TransientElement implements Case {
36     private Expression value = null;
37     private LightAttrList statements = null;
38     
39     /** Creates a new instance of CaseImpl */
40     public CaseImpl(StorableObject o) {
41         super(o);
42     }
43     
44     public Expression getValue() {
45         if (!childrenInited) {
46             initChildren();
47         }
48         return value;
49     }
50     
51     public void setValue(Expression value) {
52         objectChanged(CHANGED_VALUE);
53         changeChild(getValue(), value);
54         this.value = value;
55     }
56     
57     public List getStatements() {
58         if (!childrenInited) {
59             initChildren();
60         }
61         return statements;
62     }
63     
64     public List getChildren() {
65         List list = new ArrayList(1 + getStatements().size());
66         addIfNotNull(list, getValue());
67         list.addAll(getStatements());
68         return list;
69     }
70     
71     protected void initChildren() {
72         childrenInited = false;
73         ASTree tree = getASTree();
74         if (tree != null) {
75             value = (Expression) initOrCreate(value, getLabelAST(tree));
76             statements = createChildrenList(statements, "statements", getStatementsAST(tree), CHANGED_STATEMENTS, false); // NOI18N
77
}
78         childrenInited = true;
79     }
80     
81     private static ASTree getLabelAST(ASTree tree) {
82         if (tree.getType() == ASTreeTypes.SWITCH_BLOCK_STATEMENT_GROUP) {
83             ASTree[] cases = tree.getSubTrees();
84             ASTree[] labels = cases[0].getSubTrees();
85             return cases[0].getType() == ASTreeTypes.SWITCH_LABELS ?
86                 labels[labels.length - 1].getSubTrees()[0] : labels[0];
87         } else {
88             return tree.getSubTrees()[0];
89         }
90     }
91     
92     private static ASTree[] getStatementsAST(ASTree tree) {
93         if (tree.getType() == ASTreeTypes.SWITCH_BLOCK_STATEMENT_GROUP) {
94             return tree.getSubTrees()[1].getSubTrees();
95         } else {
96             return null;
97         }
98     }
99     
100     public String JavaDoc getSourceText() {
101 // String origElem;
102
// if ((origElem = checkChange()) != null)
103
// return origElem;
104
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
105         List statements = getStatements();
106         TransientElement value = (TransientElement)getValue();
107         buf.append('\n');
108         if (value != null) {
109             formatElementPart(CASE_KEYWORD, buf);
110             buf.append(value.getSourceText());
111         } else {
112             formatElementPart(DEFAULT_KEYWORD, buf);
113         }
114         buf.append(':');
115         for (Iterator iter = statements.iterator(); iter.hasNext();) {
116             buf.append('\n');
117             StatementImpl stat = (StatementImpl) iter.next();
118             buf.append(stat.getSourceText());
119         }
120         return buf.toString();
121     }
122      
123     public void getDiff(List diff) {
124         ASTProvider parser = getParser();
125         ASTree tree = getASTree();
126
127         getChildDiff(diff, parser, getLabelAST(tree), (MetadataElement) getValue(), CHANGED_VALUE);
128         getCollectionDiff(diff, parser, CHANGED_STATEMENTS, getStatementsAST(tree), getStatements(), getEndOffset(parser, tree), "\n"); // NOI18N
129
}
130     
131     void setData(Expression value, List statements) {
132         // value
133
changeChild(null, value);
134         this.value = value;
135         // statements
136
this.statements = createChildrenList("statements", statements, CHANGED_STATEMENTS); // NOI18N
137
}
138     
139     protected void _delete() {
140         // --- delete components -------------------------------------------
141
if (childrenInited) {
142             deleteChild(value);
143             deleteChildren(statements);
144         }
145         // --- delete links -----------------------------------------------
146
// no links to delete
147
// --- call super ---------------------------------------
148
super._delete();
149     }
150     
151     public void replaceChild(Element oldElement,Element newElement){
152         if (childrenInited) {
153             if (oldElement.equals(value)) {
154                 setValue((Expression)newElement);
155             } else
156                 replaceObject(getStatements(),oldElement,newElement);
157         }
158     }
159     
160     public Element duplicate(JavaModelPackage targetExtent) {
161         return targetExtent.getCase().createCase(
162                 (Expression) duplicateElement(getValue(), targetExtent), duplicateList(getStatements(), targetExtent));
163     }
164 }
165
Popular Tags