KickJava   Java API By Example, From Geeks To Geeks.

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


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.jmi.javamodel.TryStatement;
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 TryStatementImpl extends StatementImpl implements TryStatement {
36     private StatementBlock body = null;
37     private LightAttrList catches = null;
38     private StatementBlock finalizer = null;
39     
40     /** Creates a new instance of TryStatementImpl */
41     public TryStatementImpl(StorableObject o) {
42         super(o);
43     }
44     
45     public void setBody(StatementBlock body) {
46         objectChanged(CHANGED_BODY);
47         changeChild(getBody(), body);
48         this.body = body;
49     }
50     
51     public StatementBlock getBody() {
52         if (!childrenInited) {
53             initChildren();
54         }
55         return body;
56     }
57     
58     public void setFinalizer(StatementBlock finalizer) {
59         objectChanged(CHANGED_FINALIZER);
60         changeChild(getFinalizer(), finalizer);
61         this.finalizer = finalizer;
62     }
63     
64     public StatementBlock getFinalizer() {
65         if (!childrenInited) {
66             initChildren();
67         }
68         return finalizer;
69     }
70     
71     public List getCatches() {
72         if (!childrenInited) {
73             initChildren();
74         }
75         return catches;
76     }
77     
78     public List getChildren() {
79         List list = new ArrayList(2 + getCatches().size());
80         addIfNotNull(list, getBody());
81         list.addAll(getCatches());
82         addIfNotNull(list, getFinalizer());
83         return list;
84     }
85     
86     protected void initChildren() {
87         childrenInited = false;
88         ASTree tree = getASTree();
89         if (tree != null) {
90             ASTree[] parts = tree.getSubTrees();
91             body = (StatementBlock) initOrCreate(body, parts[0]);
92             catches = createChildrenList(catches, "catches", parts[1], ASTreeTypes.CATCHES, CHANGED_CATCHES, false); // NOI18N
93
finalizer = (StatementBlock) initOrCreate(finalizer, parts[2]);
94         }
95         childrenInited = true;
96     }
97     
98     String JavaDoc getRawText() {
99         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
100         StatementImpl body = (StatementImpl) getBody();
101         List catches = getCatches();
102         TransientElement finalizer = (TransientElement)getFinalizer();
103         formatElementPart(TRY_KEYWORD, buf);
104         buf.append(body.getSourceText());
105         for (Iterator iter = catches.iterator(); iter.hasNext();) {
106             TransientElement cas = (TransientElement) iter.next();
107             buf.append(cas.getSourceText());
108         }
109         if (finalizer != null) {
110             formatElementPart(FINALLY_KEYWORD, buf);
111             buf.append(finalizer.getSourceText());
112         }
113         return buf.toString();
114     }
115
116     public void getDiff(List diff) {
117         ASTProvider parser = getParser();
118         ASTree tree = getASTree();
119         ASTree[] children = tree.getSubTrees();
120
121         getChildDiff(diff, parser, children[0], (MetadataElement) getBody(), CHANGED_BODY);
122         int pos;
123         if (children[2] == null) {
124             pos = parser.getToken(tree.getLastToken()).getEndOffset();
125         } else {
126             pos = parser.getToken(children[2].getFirstToken()).getStartOffset();
127         }
128         getCollectionDiff(diff, parser, CHANGED_CATCHES, children[1], ASTreeTypes.CATCHES, getCatches(), pos, "\n"); // NOI18N
129
getChildDiff(diff, parser, children[2], (MetadataElement) getFinalizer(), CHANGED_FINALIZER, pos, "\n"); // NOI18N
130
}
131     
132     void setData(StatementBlock body, List catches, StatementBlock finalizer) {
133         changeChild(null, body);
134         this.body = body;
135         this.catches = createChildrenList("catches", catches, CHANGED_CATCHES); // NOI18N
136
changeChild(null, finalizer);
137         this.finalizer = finalizer;
138     }
139
140     protected void _delete() {
141         // --- delete components -------------------------------------------
142
if (childrenInited) {
143             deleteChild(body);
144             deleteChild(finalizer);
145             deleteChildren(catches);
146         }
147         // --- delete links -----------------------------------------------
148
// no links to delete
149
// --- call super ---------------------------------------
150
super._delete();
151     }
152     
153     public void replaceChild(Element oldElement,Element newElement) {
154         if (childrenInited) {
155             if (oldElement.equals(body)) {
156                 setBody((StatementBlock)newElement);
157             } else if (oldElement.equals(finalizer)) {
158                 setFinalizer((StatementBlock)newElement);
159             } else
160                 replaceObject(getCatches(),oldElement,newElement);
161         }
162     }
163     
164     public Element duplicate(JavaModelPackage targetExtent) {
165         return targetExtent.getTryStatement().createTryStatement(
166                 (StatementBlock) duplicateElement(getBody(), targetExtent),
167                 duplicateList(getCatches(), targetExtent),
168                 (StatementBlock) duplicateElement(getFinalizer(), targetExtent)
169                );
170     }
171 }
172
Popular Tags