KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > src > InitializerElement


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.openide.src;
21
22 import java.io.*;
23
24 /** Element which represents an initializer block.
25 * This may be a static class initializer, or (as of Java 1.1)
26 * a nonstatic initializer (usually used in anonymous inner classes).
27 *
28 * @author Petr Hamernik
29 */

30 public final class InitializerElement extends Element implements Cloneable JavaDoc {
31     /** reference to source element */
32     private ClassElement declaringClass;
33
34     static final long serialVersionUID =5768667690932077280L;
35     /** Create an initializer represented in memory. */
36     public InitializerElement() {
37         this(new InitializerElement.Memory(), null);
38     }
39
40     /** Create an initializer.
41     * @param impl the pluggable implementation
42     * @param declaringClass the class containing it, or <code>null</code>
43     */

44     public InitializerElement(InitializerElement.Impl impl, ClassElement declaringClass) {
45         super(impl);
46         this.declaringClass = declaringClass;
47     }
48
49     /** @return the current implementation. */
50     InitializerElement.Impl getInitializerImpl() {
51         return (InitializerElement.Impl)impl;
52     }
53
54     /** Clone this initializer.
55     * @return a new initializer with the same structure, but represented in memory
56     */

57     public Object JavaDoc clone () {
58         return new InitializerElement (new Memory (this), null);
59     }
60
61     /** Set the <code>static</code> flag for this initializer.
62     * @param stat <code>true</code> to make static
63     * @throws SourceException if impossible
64     */

65     public void setStatic(boolean stat) throws SourceException {
66         getInitializerImpl().setStatic(stat);
67     }
68
69     /** Test whether this initializer is static.
70     * @return <code>true</code> if it is
71     */

72     public boolean isStatic() {
73         return getInitializerImpl().isStatic();
74     }
75
76     /** Set the body of this initializer.
77     * @param s the new body
78     * @throws SourceException if impossible
79     */

80     public void setBody (String JavaDoc s) throws SourceException {
81         getInitializerImpl ().setBody (s);
82     }
83
84     /** Get the body of this initializer.
85     * @return the string representing the body
86     */

87     public String JavaDoc getBody () {
88         return getInitializerImpl ().getBody ();
89     }
90
91     /** Get the class documentation.
92     * @return the JavaDoc
93     */

94     public JavaDoc getJavaDoc() {
95         return getInitializerImpl ().getJavaDoc ();
96     }
97
98     // no it's not! --jglick
99
// This field is automatically updated
100
// when a MemberElement is added to the class.
101
/** Get the declaring class.
102     *
103     * @return the class that owns this initializer, or <code>null</code>
104     */

105     public final ClassElement getDeclaringClass () {
106         return declaringClass;
107     }
108
109     /* Prints the element into the element printer.
110     * @param printer The element printer where to print to
111     * @exception ElementPrinterInterruptException if printer cancel the printing
112     */

113     public void print(ElementPrinter printer) throws ElementPrinterInterruptException {
114         printer.markInitializer(this, printer.ELEMENT_BEGIN);
115
116         JavaDoc doc = getJavaDoc();
117         if ((doc != null) && !doc.isEmpty()) {
118             printer.markInitializer(this, printer.JAVADOC_BEGIN); // JAVADOC begin
119
printJavaDoc(doc, printer);
120             printer.markInitializer(this, printer.JAVADOC_END); // JAVADOC end
121
printer.println(""); // NOI18N
122
}
123
124         if (isStatic()) {
125             printer.markInitializer(this, printer.HEADER_BEGIN);
126             printer.print("static "); // NOI18N
127
printer.markInitializer(this, printer.HEADER_END);
128         }
129         printer.print("{"); // NOI18N
130
printer.markInitializer(this, printer.BODY_BEGIN);
131         printer.print(getBody());
132         printer.markInitializer(this, printer.BODY_END);
133         printer.print("}"); // NOI18N
134
printer.markInitializer(this, printer.ELEMENT_END);
135     }
136
137     /** Pluggable implementation of initializers.
138     * @see InitializerElement
139     */

140     public interface Impl extends Element.Impl {
141         /** @deprecated Only public by accident. */
142         /* public static final */ long serialVersionUID = -3742940543185945549L;
143         /** Set the <code>static</code> flag for this initializer.
144          * @param stat <code>true</code> to make static
145          * @throws SourceException if impossible
146          */

147         public void setStatic(boolean stat) throws SourceException;
148
149         /** Test whether this initializer is static.
150          * @return <code>true</code> if it is
151          */

152         public boolean isStatic();
153
154         /** Set the body of this initializer.
155          * @param s the new body
156          * @throws SourceException if impossible
157          */

158         public void setBody (String JavaDoc s) throws SourceException;
159
160         /** Get the body of this initializer.
161          * @return the string representing the body
162          */

163         public String JavaDoc getBody ();
164
165         /** Get the JavaDoc.
166         * @return the JavaDoc
167         */

168         public JavaDoc getJavaDoc ();
169     }
170
171     /** Default implementation of the Impl interface.
172     * It just holds the property values.
173     */

174     static class Memory extends Element.Memory implements InitializerElement.Impl {
175         /** Is this block static ? */
176         private boolean stat;
177         /** body of the element */
178         private String JavaDoc body;
179         /** java doc comment */
180         private JavaDoc javadoc;
181
182         static final long serialVersionUID =1956692952966906280L;
183         Memory() {
184             stat = false;
185             body = ""; // NOI18N
186
javadoc = JavaDocSupport.createInitializerJavaDoc(null);
187         }
188
189         /** Copy constructor.
190         */

191         Memory(InitializerElement el) {
192             stat = el.isStatic ();
193             body = el.getBody ();
194             javadoc = el.getJavaDoc().isEmpty() ? JavaDocSupport.createJavaDoc(null) :
195             JavaDocSupport.createJavaDoc(el.getJavaDoc().getRawText());
196         }
197         /** Sets the 'static' flag for this initializer. */
198         public void setStatic(boolean stat) {
199             boolean old = stat;
200             this.stat = stat;
201             firePropertyChange (PROP_STATIC,
202                                 old ? Boolean.TRUE : Boolean.FALSE,
203                                 stat ? Boolean.TRUE : Boolean.FALSE);
204         }
205
206         /** is this initializer static.
207         * @return true if it is.
208         */

209         public boolean isStatic() {
210             return stat;
211         }
212
213         /** Sets body of the element.
214         * @param s the body
215         */

216         public void setBody (String JavaDoc s) throws SourceException {
217             String JavaDoc old = body;
218             body = s;
219             firePropertyChange (PROP_BODY, old, body);
220         }
221
222         /** Getter for the body of element.
223         * @return the string representing the body
224         */

225         public String JavaDoc getBody () {
226             return body;
227         }
228
229         /** Get the JavaDoc.
230         * @return the JavaDoc
231         */

232         public JavaDoc getJavaDoc () {
233             return javadoc;
234         }
235
236         /** Marks this initializer as the position for inserting new elements.
237          */

238         public void markCurrent(boolean after) {
239             ClassElement decl = ((InitializerElement)element).getDeclaringClass();
240             if (decl == null) {
241                 throw new IllegalStateException JavaDoc();
242             }
243             ((ClassElement.Memory)decl.getCookie(ClassElement.Memory.class)).markCurrent(element, after);
244         }
245
246         public Object JavaDoc readResolve() {
247             return new InitializerElement(this, null);
248         }
249     }
250 }
251
Popular Tags