KickJava   Java API By Example, From Geeks To Geeks.

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


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.LabeledStatement;
25 import org.netbeans.jmi.javamodel.Statement;
26 import org.netbeans.lib.java.parser.ASTree;
27 import org.netbeans.mdr.storagemodel.StorableObject;
28 import org.netbeans.modules.javacore.parser.ASTProvider;
29 import org.netbeans.modules.javacore.parser.ASTUtil;
30
31 /**
32  *
33  * @author Martin Matula
34  */

35 public abstract class LabeledStatementImpl extends StatementImpl implements LabeledStatement {
36     private String JavaDoc label = null;
37     private Statement body = null;
38     
39     /** Creates a new instance of LabeledStatementImpl */
40     public LabeledStatementImpl(StorableObject o) {
41         super(o);
42     }
43     
44     public void setLabel(String JavaDoc label) {
45         objectChanged(CHANGED_LABEL);
46         this.label = label;
47     }
48     
49     public String JavaDoc getLabel() {
50         if (isChanged(CHANGED_LABEL)) {
51             return label;
52         } else {
53             return ASTUtil.getIdentifier(getASTree().getSubTrees()[0]);
54         }
55     }
56     
57     public void setBody(Statement body) {
58         objectChanged(CHANGED_BODY);
59         changeChild(getBody(), body);
60         this.body = body;
61     }
62
63     public List getChildren() {
64         List list = new ArrayList(1);
65         addIfNotNull(list, getBody());
66         return list;
67     }
68     
69     protected void initChildren() {
70         childrenInited = false;
71         ASTree tree = getASTree();
72         if (tree != null) {
73             body = (Statement) initOrCreate(body, getASTree().getSubTrees()[1]);
74         }
75         childrenInited = true;
76     }
77     
78     public Statement getBody() {
79         if (!childrenInited) {
80             initChildren();
81         }
82         return body;
83     }
84     
85     String JavaDoc getRawText() {
86         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
87         StatementImpl body = (StatementImpl)getBody();
88         String JavaDoc label = getLabel();
89         buf.append(label);
90         buf.append(": "); // NOI18N
91
buf.append(body.getSourceText());
92         return buf.toString();
93     }
94         
95     public void getDiff(List diff) {
96         ASTProvider parser = getParser();
97         ASTree tree = getASTree();
98         ASTree[] children = tree.getSubTrees();
99         
100         if (isChanged(CHANGED_LABEL)) {
101             replaceNode(diff, parser, children[0], getLabel(), 0, null);
102         }
103         getChildDiff(diff, parser, children[1], (MetadataElement) getBody(), CHANGED_BODY);
104     }
105     
106     void setData(String JavaDoc label, Statement body) {
107         this.label = label;
108         changeChild(null, body);
109         this.body = body;
110     }
111
112     protected void _delete() {
113         // --- delete components -------------------------------------------
114
if (childrenInited) {
115             deleteChild(body);
116         }
117         // --- delete links -----------------------------------------------
118
// no links to delete
119
// --- call super ---------------------------------------
120
super._delete();
121     }
122     
123     public void replaceChild(Element oldElement,Element newElement) {
124         if (childrenInited) {
125             if (oldElement.equals(body)) {
126                 setBody((Statement)newElement);
127             }
128         }
129     }
130     
131     public Element duplicate(JavaModelPackage targetExtent) {
132         return targetExtent.getLabeledStatement().createLabeledStatement(
133                 getLabel(),
134                 (Statement) duplicateElement(getBody(), targetExtent)
135                );
136     }
137 }
138
Popular Tags