KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > codegen > Clazz


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.java.codegen;
21
22 import java.util.*;
23
24 import javax.swing.text.Position JavaDoc;
25
26 import org.openide.src.*;
27 import org.openide.text.CloneableEditorSupport;
28 import org.openide.text.PositionBounds;
29 import org.openide.text.PositionRef;
30
31 import org.netbeans.modules.java.bridge.Binding;
32
33 /**
34  * Specialized document binding for a ClassElement.
35  *
36  * @author Svatopluk Dedic <mailto:sdedic@netbeans.org>
37  * @version 0.1
38  */

39 class Clazz extends Member implements Binding.Class, TextBinding.Container {
40     /**
41      * Support for the containment of member elements.
42      */

43     ContainerSupport container;
44     
45     public Clazz(ClassElement el, SourceText s) {
46         super(el, s);
47     }
48     
49     public TextBinding.Container getContainer() {
50         return this;
51     }
52     
53     public boolean isEmpty() {
54         return container == null || container.isEmpty();
55     }
56     
57     public void updateChildren(Collection c) {
58         if (container == null && c.isEmpty())
59             return;
60         initializeContainer();
61         container.updateChildren(c);
62     }
63     
64     /** Changes class into an interface and vice-versa. Although classes and interfaces
65      * are clearly separated in the language specs, they represent the same concept with
66      * some minor differencies.
67      */

68     public void changeClassType(boolean properClass) throws SourceException {
69         if (!source.isGeneratorEnabled())
70             return;
71         
72         ClassElement el = cloneClass();
73         el.setClassOrInterface(properClass);
74         regenerateHeader(el);
75     }
76     
77     /** Rewrites some interfaces from class' implements property.
78      */

79     public void changeInterfaces(Identifier[] replaceWith) throws SourceException {
80         if (!source.isGeneratorEnabled())
81             return;
82         
83         ClassElement el = cloneClass();
84         el.setInterfaces(replaceWith);
85         regenerateHeader(el);
86     }
87     
88     /**
89      * Changes the superclass' name.
90      */

91     public void changeSuperclass(Identifier id) throws SourceException {
92         if (!source.isGeneratorEnabled())
93             return;
94         
95         ClassElement el = cloneClass();
96         el.setSuperclass(id);
97         regenerateHeader(el);
98     }
99
100     /** The clone implementation will clone, for efficiency reasons only the class itself,
101      * not its children, as ClassElement.clone() normally does.
102      */

103     protected Element cloneElement() {
104         ClassElement x = new ClassElement();
105         ClassElement my = (ClassElement)getElement();
106         try {
107             x.setName(my.getName());
108             x.setModifiers(my.getModifiers());
109             x.setSuperclass(my.getSuperclass());
110             x.setInterfaces(my.getInterfaces());
111             x.setClassOrInterface(my.isClassOrInterface());
112             if (!my.getJavaDoc().isEmpty()) {
113                 x.getJavaDoc().setRawText(my.getJavaDoc().getRawText());
114             }
115         } catch (SourceException ex) {
116             // should NOT happen.
117
}
118         return x;
119     }
120     
121     private ClassElement cloneClass() {
122         return (ClassElement)cloneElement();
123     }
124     
125     protected int classifyProperty(String JavaDoc name) {
126         return CLASS_HEADER;
127     }
128     
129     public ElementBinding findBindingAt(int pos) {
130         ElementBinding b = super.findBindingAt(pos);
131         if (b == this) {
132             ElementBinding b2 = container.findBindingAt(pos);
133             if (b2 != null)
134                 return b2;
135         }
136         return b;
137     }
138
139     /* ============== CONTAINER MANAGEMENT METHODS ================= */
140     
141     public boolean canInsertAfter(Binding b) {
142         if (container == null) {
143             return source.canWriteInside(bodyBounds);
144         }
145         return container.canInsertAfter(b);
146     }
147
148     /** The map contains mapping from target places to their new contents.
149      */

150     public void reorder(Map fromToMap) throws SourceException {
151         container.reorder(fromToMap);
152     }
153     
154     /** Replaces the slot contents with another element (different type permitted ?)
155      */

156     public void replace(Binding oldBinding, Binding newBinding) throws SourceException {
157         container.replace(oldBinding, newBinding);
158     }
159     
160     public void changeMembers(MultiPropertyChangeEvent evt) throws SourceException {
161         if (container == null) {
162             int etype = evt.getEventType();
163             if (etype == evt.TYPE_ADD || etype == evt.TYPE_COMPOUND)
164                 initializeContainer();
165             else
166                 return;
167         }
168         container.changeMembers(evt);
169     }
170     
171     /**
172      * Initializes a new binding for the element so the element is stored after the
173      * `previous' binding, if that matters to the binding implementation.
174      * @param toInitialize the binding that is being initialized & bound to the storage.
175      * @param previous marker spot, the binding that should precede the new one.
176      */

177     public void insert(Binding toInitialize,Binding previous) throws SourceException {
178         initializeContainer();
179         container.insert(toInitialize, previous);
180     }
181     
182     protected void initializeContainer() {
183         if (container != null)
184             return;
185         container = new ContainerSupport(this.source, this);
186     }
187     
188     public void regenerateWhole(Element el) {
189     }
190     
191     PositionBounds findContainerBounds(TextBinding.Container cont) {
192         return bodyBounds;
193     }
194     
195     public void create(PositionBounds bounds) throws SourceException {
196         ClassElement c = cloneClass();
197         ClassElement orig = (ClassElement)getElement();
198
199         // create the class definition.
200
wholeBounds = bounds;
201         super.regenerateWhole(c, true);
202         
203         // initialize the container with the members:
204
PositionRef r = bodyBounds.getBegin();
205         boolean empty = true;
206         ElementBinding prevBinding= null;
207         
208         for (int kind = 0; kind < 5; kind++) {
209             Element[] models;
210             
211             switch (kind) {
212                 case 0:
213                     models = orig.getFields();
214                     break;
215                 case 1:
216                     models = orig.getInitializers();
217                     break;
218                 case 2:
219                     models = orig.getConstructors();
220                     break;
221                 case 3:
222                     models = orig.getMethods();
223                     break;
224                 case 4:
225                     models = orig.getClasses();
226                     break;
227                 default:
228                     models = null;
229             }
230             if (empty && models.length > 0) {
231                 initializeContainer();
232                 empty = false;
233             }
234             
235             for (int i = 0; i < models.length; i++) {
236                 ElementBinding b = source.findBinding(models[i]);
237                 container.insertChild(b, prevBinding, null);
238                 prevBinding = b;
239             }
240         }
241     }
242 }
243
Popular Tags