KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 package org.netbeans.modules.javacore.jmiimpl.javamodel;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import org.netbeans.jmi.javamodel.*;
25 import org.netbeans.lib.java.parser.ASTree;
26 import org.netbeans.lib.java.parser.ParserTokens;
27 import org.netbeans.mdr.storagemodel.StorableObject;
28 import org.netbeans.modules.javacore.parser.ASTProvider;
29
30 /**
31  * @author Martin Matula
32  */

33 public abstract class WildCardImpl extends TransientElement implements WildCard {
34     private boolean isLower;
35     private TypeReference boundName;
36     private Type bound;
37
38     public WildCardImpl(StorableObject o) {
39         super(o);
40     }
41
42     public boolean isLower() {
43         if (isChanged(CHANGED_IS_LOWER)) {
44             return isLower;
45         } else {
46             ASTree lower = getASTree().getSubTrees()[0];
47             return lower == null || lower.getType() == ParserTokens.EXTENDS;
48         }
49     }
50
51     public void setLower(boolean newValue) {
52         objectChanged(CHANGED_IS_LOWER);
53         isLower = newValue;
54     }
55
56     public TypeReference getBoundName() {
57         if (!childrenInited) {
58             initChildren();
59         }
60         return boundName;
61     }
62
63     public void setBoundName(TypeReference newValue) {
64         objectChanged(CHANGED_BOUND);
65         changeChild(getBoundName(), newValue);
66         this.boundName = newValue;
67         this.bound = null;
68     }
69
70     public Type getBound() {
71         if (bound == null || !isChanged(CHANGED_BOUND)) {
72             return (Type) getBoundName().getElement();
73         } else {
74             return bound;
75         }
76     }
77
78     public void setBound(Type newValue) {
79         bound = newValue;
80         MultipartIdClass proxy = ((JavaModelPackage) refImmediatePackage()).getMultipartId();
81         setBoundName(proxy.createMultipartId(newValue.getName(), null, null));
82     }
83
84     public List JavaDoc getChildren() {
85         List JavaDoc list = new ArrayList JavaDoc(1);
86         addIfNotNull(list, getBoundName());
87         return list;
88     }
89
90     protected void initChildren() {
91         childrenInited = false;
92         ASTree tree = getASTree();
93         ASTree[] children = getASTree().getSubTrees();
94         if (tree != null) {
95         boundName = (TypeReference) initOrCreate(boundName, children[1]);
96         }
97         childrenInited = true;
98     }
99
100     public String JavaDoc getSourceText() {
101         String JavaDoc origElem;
102         if ((origElem = checkChange()) != null)
103             return origElem;
104         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
105         buf.append('?');
106         TypeReferenceImpl boundName = (TypeReferenceImpl) getBoundName();
107         if (boundName != null) {
108             buf.append(isLower() ? " extends " : " super "); // NOI18N
109
buf.append(boundName.getSourceText());
110         }
111         return buf.toString();
112     }
113
114     public void getDiff(List JavaDoc diff) {
115         ASTProvider parser = getParser();
116         ASTree tree = getASTree();
117
118         if (isChanged(CHANGED_IS_LOWER) || isChanged(CHANGED_BOUND)) {
119             replaceNode(diff, parser, tree, getSourceText(), 0, null);
120         } else if (isChanged(CHANGED_CHILDREN)) {
121             ((MetadataElement) getBoundName()).getDiff(diff);
122         }
123     }
124
125     void setData(boolean isLower, TypeReference boundName) {
126         this.isLower = isLower;
127         changeChild(null, boundName);
128         this.boundName = boundName;
129     }
130
131     protected void _delete() {
132         // --- delete components -------------------------------------------
133
if (childrenInited) {
134             deleteChild(boundName);
135         }
136         // --- delete links -----------------------------------------------
137
// no links to delete
138
// --- call super ---------------------------------------
139
super._delete();
140     }
141
142     public void replaceChild(Element oldElement,Element newElement){
143         if (childrenInited) {
144             if (oldElement.equals(boundName)) {
145                 setBoundName((TypeReference) newElement);
146             }
147         }
148     }
149     
150     public Element duplicate(JavaModelPackage targetExtent) {
151         return targetExtent.getWildCard().createWildCard(
152                 isLower(), (TypeReference) duplicateElement(getBoundName(), targetExtent));
153     }
154 }
155
Popular Tags