KickJava   Java API By Example, From Geeks To Geeks.

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


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.ASTProvider;
26
27 /**
28  *
29  * @author Martin Matula
30  */

31 public abstract class PrefixExpressionImpl extends ExpressionImpl implements PrefixExpression {
32     protected Operator operator = null;
33     protected UnaryExpression expression = null;
34
35     /** Creates a new instance of PrefixExpressionImpl */
36     public PrefixExpressionImpl(StorableObject o) {
37         super(o);
38     }
39     
40     public void setExpression(UnaryExpression expression) {
41         objectChanged(CHANGED_EXPRESSION);
42         changeChild(getExpression(), expression);
43         this.expression = expression;
44     }
45     
46     public UnaryExpression getExpression() {
47         if (!childrenInited) {
48             initChildren();
49         }
50         return expression;
51     }
52     
53     public void setOperator(Operator operator) {
54         objectChanged(CHANGED_OPERATOR);
55         this.operator = operator;
56     }
57     
58     public Operator getOperator() {
59         if (isChanged(CHANGED_OPERATOR)) {
60             return operator;
61         } else {
62             return extractOperator();
63         }
64     }
65
66     protected Operator extractOperator() {
67         return getOperator(getASTree().getSubTrees()[0].getType());
68     }
69
70     public List getChildren() {
71         List list = new ArrayList(1);
72         addIfNotNull(list, getExpression());
73         return list;
74     }
75     
76     protected void initChildren() {
77         childrenInited = false;
78         ASTree tree = getASTree();
79         if (tree != null) {
80             expression = (UnaryExpression) initOrCreate(expression, tree.getSubTrees()[1]);
81         }
82         childrenInited = true;
83     }
84     
85     public String JavaDoc getSourceText() {
86         String JavaDoc origElem;
87         if ((origElem = checkChange()) != null)
88             return origElem;
89         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
90         StatementImpl expr = (StatementImpl) getExpression();
91         Operator operator = getOperator();
92         buf.append(operatorToString(operator));
93         buf.append(expr.getSourceText());
94         return buf.toString();
95     }
96         
97     public void getDiff(List diff) {
98         ASTProvider parser = getParser();
99         ASTree tree = getASTree();
100         ASTree[] children = tree.getSubTrees();
101         
102         if (isChanged(CHANGED_OPERATOR)) {
103             replaceNode(diff, parser, children[0], operatorToString(operator), 0, null);
104         }
105         getChildDiff(diff, parser, children[1], (MetadataElement) getExpression(), CHANGED_EXPRESSION);
106     }
107     
108     protected String JavaDoc operatorToString (Operator op) {
109         if (op == OperatorEnum.INC) return "++"; // NOI18N
110
if (op == OperatorEnum.DEC) return "--"; // NOI18N
111
if (op == OperatorEnum.NOT) return "!"; // NOI18N
112
if (op == OperatorEnum.COMP) return "~"; // NOI18N
113
if (op == null) return "";
114         return "???"; // [PENDING] // NOI18N
115
}
116     
117     void setData(Operator operator, UnaryExpression expression) {
118         this.operator = operator;
119         changeChild(null, expression);
120         this.expression = expression;
121     }
122
123     protected void _delete() {
124         // --- delete components -------------------------------------------
125
if (childrenInited) {
126             deleteChild(expression);
127         }
128         // --- delete links -----------------------------------------------
129
// no links to delete
130
// --- call super ---------------------------------------
131
super._delete();
132     }
133     
134     public void replaceChild(Element oldElement,Element newElement) {
135         if (childrenInited) {
136             if (oldElement.equals(expression)) {
137                 setExpression((UnaryExpression)newElement);
138             }
139         }
140     }
141     
142     public Element duplicate(JavaModelPackage targetExtent) {
143         return targetExtent.getPrefixExpression().createPrefixExpression(
144                 getOperator(), (UnaryExpression) duplicateElement(getExpression(), targetExtent));
145     }
146 }
147
Popular Tags