KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
23 import org.netbeans.lib.java.parser.ASTree;
24 import org.netbeans.mdr.storagemodel.StorableObject;
25 import org.netbeans.modules.javacore.parser.*;
26
27 /**
28  *
29  * @author Martin Matula
30  */

31 public abstract class CatchImpl extends TransientElement implements Catch {
32     private Parameter parameter;
33     private StatementBlock body;
34
35     /** Creates a new instance of CatchImpl */
36     public CatchImpl(StorableObject o) {
37         super(o);
38     }
39     
40     public Parameter getParameter() {
41         if (!childrenInited) {
42             initChildren();
43         }
44         return parameter;
45     }
46     
47     public void setParameter(Parameter parameter) {
48         objectChanged(CHANGED_PARAMETER);
49         changeChild(getParameter(), parameter);
50         this.parameter = parameter;
51     }
52     
53     public StatementBlock getBody() {
54         if (!childrenInited) {
55             initChildren();
56         }
57         return body;
58     }
59     
60     public void setBody(StatementBlock body) {
61         objectChanged(CHANGED_BODY);
62         changeChild(getBody(), body);
63         this.body = body;
64     }
65     
66     public List getChildren() {
67         List list = new ArrayList(2);
68         addIfNotNull(list, getParameter());
69         addIfNotNull(list, getBody());
70         return list;
71     }
72     
73     protected void initChildren() {
74         childrenInited = false;
75         ASTree tree = getASTree();
76         if (tree != null) {
77             ASTree[] parts = tree.getSubTrees();
78             
79             // parameter
80
ASTree parameterAST = parts[0];
81             ParameterInfo paramInfo = (ParameterInfo)getParser().getSemanticInfo(parameterAST, this);
82             if (parameter == null) {
83                 parameter = (Parameter)createElement(paramInfo);
84                 changeChild(null, parameter);
85             } else {
86                 ((SemiPersistentElement)parameter).updatePersistent(paramInfo);
87                 ((SemiPersistentElement)parameter).setElementInfo(paramInfo);
88             }
89             // body
90
body = (StatementBlock) initOrCreate(body, parts[1]);
91         }
92         childrenInited = true;
93     }
94     
95     String JavaDoc getRawText() {
96         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
97         StatementImpl body = (StatementImpl)getBody();
98         ParameterImpl param = (ParameterImpl)getParameter();
99         formatElementPart(CATCH_KEYWORD, buf);
100         formatElementPart(STMT_OPEN_BRACKET, buf);
101         buf.append(param.getSourceText());
102         formatElementPart(STMT_CLOSE_BRACKET, buf);
103         buf.append(body.getSourceText());
104         return buf.toString();
105     }
106     
107     protected String JavaDoc getIndentation() {
108         return ((MetadataElement) refImmediateComposite()).getIndentation();
109     }
110     
111     public void getDiff(List diff) {
112         ASTProvider parser = getParser();
113         ASTree[] children = getASTree().getSubTrees();
114         
115         getChildDiff(diff, parser, children[0], (MetadataElement) getParameter(), CHANGED_PARAMETER);
116         getChildDiff(diff, parser, children[1], (MetadataElement) getBody(), CHANGED_BODY);
117     }
118     
119     void setData(Parameter parameter, StatementBlock body) {
120         changeChild(null, parameter);
121         this.parameter = parameter;
122         changeChild(null, body);
123         this.body = body;
124     }
125     
126     protected void _delete() {
127         // --- delete components -------------------------------------------
128
if (childrenInited) {
129             deleteChild(parameter);
130             deleteChild(body);
131         }
132         // --- delete links -----------------------------------------------
133
// no links to delete
134
// --- call super ---------------------------------------
135
super._delete();
136     }
137     
138     public void replaceChild(Element oldElement,Element newElement) {
139         if (childrenInited) {
140             if (oldElement.equals(parameter)) {
141                 setParameter((Parameter)newElement);
142             }
143             if (oldElement.equals(body)) {
144                 setBody((StatementBlock)newElement);
145             }
146         }
147     }
148     
149     public Element duplicate(JavaModelPackage targetExtent) {
150         return targetExtent.getCatch().createCatch(
151                 (Parameter) duplicateElement(getParameter(), targetExtent), (StatementBlock) duplicateElement(getBody(), targetExtent));
152     }
153 }
154
Popular Tags