KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.openide.src.*;
23 import org.openide.text.*;
24
25 import org.netbeans.modules.java.bridge.Binding;
26
27 /**
28  *
29  * @author Svatopluk Dedic <mailto:sdedic@netbeans.org>
30  * @version 0.1
31  */

32 class Initializer extends ElementBinding implements Binding.Initializer {
33     /** This body string will be returned from getBody(), if not null.
34      */

35     private String JavaDoc preexistingBody;
36
37     public Initializer(InitializerElement el, SourceText s) {
38         super(el, s);
39     }
40
41     public void create(PositionBounds b) throws SourceException {
42         super.create(b);
43         preexistingBody = null;
44     }
45
46     private InitializerElement getInitializer() {
47         return (InitializerElement)getElement();
48     }
49     
50     private InitializerElement cloneInitializer() {
51         return (InitializerElement)cloneElement();
52     }
53     
54     protected int classifyProperty(String JavaDoc name) {
55         if (name == PROP_BODY)
56             return CLASS_BODY;
57         else
58             return CLASS_HEADER;
59     }
60     
61     protected Element cloneElement() {
62         InitializerElement initEl = new InitializerElement();
63         try {
64             initEl.setStatic(getInitializer().isStatic());
65         } catch (SourceException ex) {
66         }
67         return initEl;
68     }
69     
70     /**
71      * Changes static <-> nonstatic
72      */

73     public void changeStatic(boolean enable) throws SourceException {
74         if (!source.isGeneratorEnabled())
75             return;
76         
77         InitializerElement iel = cloneInitializer();
78         iel.setStatic(enable);
79         regenerateHeader(iel);
80     }
81     
82     /**
83      * Updates the storage binding object from an external SourceText.
84      */

85     public void updateFrom(Binding other) {
86     }
87     
88     public String JavaDoc getBodyContent() throws SourceException {
89         if (preexistingBody != null)
90             return preexistingBody;
91         return CodeGenerator.readTextBounds(bodyBounds);
92     }
93     
94     public void changeBody(String JavaDoc bodyString) throws SourceException {
95         if (!source.isGeneratorEnabled())
96             return;
97         
98         InitializerElement ie = cloneInitializer();
99         ie.setBody(bodyString);
100         source.runAtomic(getElement(), new PartialGenerator(source.getDocument(),
101             ie, bodyBounds, ElementPrinter.BODY_BEGIN, ElementPrinter.BODY_END));
102     }
103     
104     public void copyBody(String JavaDoc bodyString) {
105         this.preexistingBody = bodyString;
106     }
107 }
108
Popular Tags