KickJava   Java API By Example, From Geeks To Geeks.

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


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.Expression;
24 import org.netbeans.jmi.javamodel.JavaModelPackage;
25 import org.netbeans.jmi.javamodel.StatementBlock;
26 import org.netbeans.jmi.javamodel.SynchronizedStatement;
27 import org.netbeans.lib.java.parser.ASTree;
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 SynchronizedStatementImpl extends StatementImpl implements SynchronizedStatement {
36     private Expression expression = null;
37     private StatementBlock body = null;
38     
39     /** Creates a new instance of SynchronizedStatementImpl */
40     public SynchronizedStatementImpl(StorableObject o) {
41         super(o);
42     }
43     
44     public void setLock(Expression expression) {
45         objectChanged(CHANGED_LOCK);
46         changeChild(getLock(), expression);
47         this.expression = expression;
48     }
49     
50     public Expression getLock() {
51         if (!childrenInited) {
52             initChildren();
53         }
54         return expression;
55     }
56     
57     public void setBody(StatementBlock body) {
58         objectChanged(CHANGED_BODY);
59         changeChild(getBody(), body);
60         this.body = body;
61     }
62     
63     public StatementBlock getBody() {
64         if (!childrenInited) {
65             initChildren();
66         }
67         return body;
68     }
69     
70     public List getChildren() {
71         List list = new ArrayList(2);
72         addIfNotNull(list, getLock());
73         addIfNotNull(list, getBody());
74         return list;
75     }
76     
77     protected void initChildren() {
78         childrenInited = false;
79         ASTree tree = getASTree();
80         if (tree != null) {
81             expression = (Expression) initOrCreate(expression, tree.getSubTrees()[0]);
82             body = (StatementBlock) initOrCreate(body, tree.getSubTrees()[1]);
83         }
84         childrenInited = true;
85     }
86     
87     public String JavaDoc getSourceText() {
88         String JavaDoc origElem;
89         if ((origElem = checkChange()) != null)
90             return origElem;
91         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
92         StatementImpl body = (StatementImpl) getBody();
93         StatementImpl lock = (StatementImpl) getLock();
94         buf.append('\n');
95         formatElementPart(SYNCHRONIZED_KEYWORD, buf);
96         formatElementPart(STMT_OPEN_BRACKET, buf);
97         buf.append(lock.getSourceText());
98         formatElementPart(STMT_CLOSE_BRACKET, buf);
99         buf.append(body.getSourceText());
100         return buf.toString();
101     }
102
103     public void getDiff(List diff) {
104         ASTProvider parser = getParser();
105         ASTree tree = getASTree();
106         ASTree[] children = tree.getSubTrees();
107
108         getChildDiff(diff, parser, children[0], (MetadataElement) getLock(), CHANGED_LOCK);
109         getChildDiff(diff, parser, children[1], (MetadataElement) getBody(), CHANGED_BODY);
110     }
111     
112     void setData(Expression expression, StatementBlock body) {
113         changeChild(null, expression);
114         this.expression = expression;
115         changeChild(null, body);
116         this.body = body;
117     }
118
119     protected void _delete() {
120         // --- delete components -------------------------------------------
121
if (childrenInited) {
122             deleteChild(expression);
123             deleteChild(body);
124         }
125         // --- delete links -----------------------------------------------
126
// no links to delete
127
// --- call super ---------------------------------------
128
super._delete();
129     }
130     
131     public void replaceChild(Element oldElement,Element newElement) {
132         if (childrenInited) {
133             if (oldElement.equals(expression)) {
134                 setLock((Expression)newElement);
135             } else if (oldElement.equals(body)) {
136                 setBody((StatementBlock)newElement);
137             }
138         }
139     }
140     
141     public Element duplicate(JavaModelPackage targetExtent) {
142         return targetExtent.getSynchronizedStatement().createSynchronizedStatement(
143                 (Expression) duplicateElement(getLock(), targetExtent),
144                 (StatementBlock) duplicateElement(getBody(), targetExtent));
145     }
146 }
147
Popular Tags