KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.netbeans.jmi.javamodel.Element;
22 import org.netbeans.jmi.javamodel.Expression;
23 import org.netbeans.jmi.javamodel.SwitchStatement;
24 import org.netbeans.lib.java.parser.ASTree;
25 import org.netbeans.lib.java.parser.ASTreeTypes;
26 import org.netbeans.mdr.storagemodel.StorableObject;
27 import org.netbeans.modules.javacore.parser.ASTProvider;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import org.netbeans.jmi.javamodel.JavaModelPackage;
32
33 /**
34  *
35  * @author Martin Matula
36  */

37 public abstract class SwitchStatementImpl extends ConditionImpl implements SwitchStatement {
38     private LightAttrList cases = null;
39     
40     /** Creates a new instance of SwitchStatementImpl */
41     public SwitchStatementImpl(StorableObject o) {
42         super(o);
43     }
44     
45     public List JavaDoc getCases() {
46         if (!childrenInited) {
47             initChildren();
48         }
49         return cases;
50     }
51     
52     public List JavaDoc getChildren() {
53         List JavaDoc list = super.getChildren();
54         list.addAll(getCases());
55         return list;
56     }
57     
58     protected void initChildren() {
59         childrenInited = false;
60         ASTree tree = getASTree();
61         if (tree != null) {
62             ASTree[] parts = tree.getSubTrees();
63             expression = (Expression) initOrCreate(expression, parts[0]);
64             ASTree[] block = parts[1].getSubTrees();
65             ArrayList JavaDoc children = new ArrayList JavaDoc();
66             if (block[0] != null) {
67                 ASTree[] groups = block[0].getSubTrees();
68                 for (int i = 0; i < groups.length; i++) {
69                     ASTree[] cases = groups[i].getSubTrees();
70                     if (cases[0].getType() == ASTreeTypes.SWITCH_LABELS) {
71                         ASTree[] labels = cases[0].getSubTrees();
72                         for (int j = 0; j < labels.length - 1; j++) {
73                             children.add(labels[j]);
74                         }
75                     }
76                     children.add(groups[i]);
77                 }
78             }
79             if (block[1] != null) {
80                 if (block[1].getType() == ASTreeTypes.SWITCH_LABELS) {
81                     ASTree[] labels = block[1].getSubTrees();
82                     for (int j = 0; j < labels.length; j++) {
83                         children.add(labels[j]);
84                     }
85                 }
86             }
87             cases = createChildrenList(cases, "cases", (ASTree[]) children.toArray(new ASTree[children.size()]), CHANGED_CASES, false); // NOI18N
88
}
89         childrenInited = true;
90     }
91     
92     public String JavaDoc getRawText() {
93         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
94         List JavaDoc cases = getCases();
95         TransientElement expr = (TransientElement) getExpression();
96         formatElementPart(SWITCH_KEYWORD, buf, isNew());
97         formatElementPart(STMT_OPEN_BRACKET, buf);
98         buf.append(expr.getSourceText());
99         formatElementPart(STMT_CLOSE_BRACKET, buf);
100         formatElementPart(BLOCK_OPEN_CURLY, buf);
101         for (Iterator JavaDoc iter = cases.iterator(); iter.hasNext();) {
102             CaseImpl casik = (CaseImpl) iter.next();
103             buf.append(casik.getSourceText());
104         }
105         formatElementPart(BLOCK_CLOSE_CURLY, buf);
106         return buf.toString();
107     }
108
109     public void getDiff(List JavaDoc diff) {
110         if (isChanged()) {
111             ASTProvider parser = getParser();
112             ASTree tree = getASTree();
113             
114             if (isChanged(CHANGED_CASES)) {
115                 diff.add(new DiffElement(parser.getToken(tree.getFirstToken()).getStartOffset(), parser.getToken(tree.getLastToken()).getEndOffset(), getSourceText()));
116             } else {
117                 getChildDiff(diff, parser, tree.getSubTrees()[0], (MetadataElement) getExpression(), CHANGED_EXPRESSION);
118                 if (isChanged(CHANGED_CHILDREN)) {
119                     for (Iterator JavaDoc it = getCases().iterator(); it.hasNext();) {
120                         CaseImpl c = (CaseImpl) it.next();
121                         if (c.isChanged()) {
122                             c.getDiff(diff);
123                         }
124                     }
125                 }
126             }
127         }
128     }
129     
130     void setData(Expression expression, List JavaDoc cases) {
131         // expression
132
changeChild(null, expression);
133         this.expression = expression;
134         // cases
135
this.cases = createChildrenList("cases", cases, CHANGED_CASES); // NOI18N
136
}
137
138     protected void _delete() {
139         // --- delete components -------------------------------------------
140
if (childrenInited) {
141             deleteChildren(cases);
142         }
143         // --- delete links -----------------------------------------------
144
// no links to delete
145
// --- call super ---------------------------------------
146
super._delete();
147     }
148     
149     public void replaceChild(Element oldElement,Element newElement) {
150         if (childrenInited) {
151             if (replaceObject(getCases(),oldElement,newElement))
152                 return;
153             super.replaceChild(oldElement,newElement);
154         }
155     }
156     
157     public Element duplicate(JavaModelPackage targetExtent) {
158         return targetExtent.getSwitchStatement().createSwitchStatement(
159                 (Expression) duplicateElement(getExpression(), targetExtent),
160                 duplicateList(getCases(), targetExtent)
161                );
162     }
163 }
164
Popular Tags