KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > bridge > InitializerElementImpl


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.bridge;
21
22 import java.lang.reflect.Modifier JavaDoc;
23 import java.beans.*;
24
25 import org.openide.src.*;
26 import org.netbeans.api.mdr.events.*;
27
28 import org.netbeans.jmi.javamodel.Initializer;
29
30 /**
31  * The class implements property handling for a class' initializer. In fact, the initializer
32  * has only two properties: the body and the optional static modifier.
33  *
34  * @author Svatopluk Dedic, Petr Hamernik
35  * @version 0.1
36  * @since 24/11/2000
37  */

38 class InitializerElementImpl extends MemberElementImpl
39     implements InitializerElement.Impl, Element.Impl2 {
40
41     private transient boolean bodyInited;
42     private transient String JavaDoc cachedBody;
43     
44     private transient long bodyHash = -1;
45     
46     private ElementImpl.ElementListener initializerListener;
47     
48     private static final long serialVersionUID = 3490993892523739282L;
49     
50     InitializerElementImpl(DefaultLangModel model, Initializer initializer) {
51         super(model, initializer);
52     }
53     
54     public void connectListener () {
55         initializerListener = new InitializerListener (this);
56         initializerListener.connect ();
57     }
58     
59     protected void createFromModel(Element el) throws SourceException {
60         InitializerElement i = (InitializerElement)el;
61         setBody(i.getBody());
62         setStatic(i.isStatic());
63     }
64     
65     public void attachedToElement(Element el) {
66         super.attachedToElement(el);
67     }
68
69     public boolean isStatic() {
70         repository.beginTrans(false);
71         try {
72             if (javaElement.isValid()) {
73                 setClassPath();
74                 return (getModifiers() & Modifier.STATIC) > 0;
75             } else {
76                 return false;
77             }
78         } finally {
79             repository.endTrans(false);
80         }
81     }
82     
83     void copyBody(String JavaDoc s) {
84     }
85         
86     public final String JavaDoc getBody() {
87         repository.beginTrans(false);
88         try {
89             if (javaElement.isValid()) {
90                 setClassPath();
91                 return ((Initializer)javaElement).getBodyText();
92             } else {
93                 return null;
94             }
95         } finally {
96             repository.endTrans(false);
97         }
98     }
99     
100     public JavaDoc getJavaDoc() {
101         return null;
102     }
103
104     public boolean isValid() {
105         return super.isValid() && (getDeclaringImpl() == null ||
106             getDeclaringImpl().isValid());
107     }
108     
109     void updateBody(String JavaDoc content) {
110         /*
111         if (content == null) {
112             bodyHash = -1;
113             return;
114         }
115         long newHash = computeHash(content);
116         if (newHash == bodyHash)
117             return;
118         bodyHash = newHash;
119         // we don't have info about the previous body's contents.
120         addPropertyChange(new PropertyChangeEvent(getElement(), PROP_BODY, null, null));
121          */

122     }
123     
124     // Change operations.
125
///////////////////////////////////////////////////////////////////////////////
126
public void setStatic(boolean enable) throws SourceException {
127         checkWritable(false);
128         checkDocument();
129         boolean failed = true;
130         repository.beginTrans (true);
131         try {
132             if (javaElement.isValid()) {
133                 PropertyChangeEvent evt;
134
135                 setClassPath();
136                 int mod = getModifiers ();
137                 if (((mod & Modifier.STATIC) > 0) == enable) {
138                     failed = false;
139                     return;
140                 }
141                 evt = new PropertyChangeEvent(getElement(), PROP_STATIC, enable ? Boolean.FALSE : Boolean.TRUE,
142                     enable ? Boolean.TRUE : Boolean.FALSE);
143
144                 checkVetoablePropertyChange(evt);
145
146                 ((Initializer) javaElement).setModifiers (enable ? Modifier.STATIC : 0);
147                 failed = false;
148             } else {
149                 failed = false;
150                 throwIsInvalid ();
151             }
152         } finally {
153             repository.endTrans (failed);
154         }
155     }
156     
157     /** Sets the body. The entire functionality is delegated to the storage binding object.
158      * Note that the text sent out in the PropertyChangeEvent may NOT match the text
159      * passed in the vetoable change event since the text may be transformed by an
160      * indentation engine.
161      * @param body text of the initializer's body.
162      */

163     public void setBody(String JavaDoc body) throws SourceException {
164         checkWritable(false);
165         checkDocument();
166         repository.beginTrans (true);
167         boolean rollback = true;
168         try {
169             if (javaElement.isValid()) {
170                 PropertyChangeEvent evt;
171
172                 setClassPath();
173                 String JavaDoc cachedBody = getBody ();
174                 if (cachedBody == body ||
175                     (cachedBody != null && body != null && cachedBody.equals(body))) {
176                     checkIsValid ();
177                     return;
178                 }
179                 evt = new PropertyChangeEvent(getElement(), PROP_BODY, cachedBody, body);
180                 checkVetoablePropertyChange(evt);
181
182                 InitializerElement elem = (InitializerElement) cloneSelf ();
183                 ((Initializer)javaElement).setBodyText(body);
184                 rollback = false;
185             } else {
186                 rollback = false;
187                 throwIsInvalid ();
188             }
189         } finally {
190             repository.endTrans (rollback);
191         }
192     }
193     
194     public void fireModifiersChange (Integer JavaDoc oldValue, Integer JavaDoc newValue) {
195         Boolean JavaDoc oldStatic, newStatic;
196         boolean flag = (oldValue.intValue () & Modifier.STATIC) > 0;
197         if (flag) {
198             oldStatic = Boolean.TRUE;
199             newStatic = Boolean.FALSE;
200         } else {
201             oldStatic = Boolean.FALSE;
202             newStatic = Boolean.TRUE;
203         }
204         
205         PropertyChangeEvent evt = new PropertyChangeEvent(getElement(), PROP_STATIC, oldStatic, newStatic);
206         fireOwnPropertyChange(evt);
207         
208         InitializerElement old = (InitializerElement) cloneSelf ();
209         try {
210             old.setStatic (flag);
211         } catch (SourceException e) {
212             e.printStackTrace ();
213         }
214         notifyConnectionChange (old);
215     }
216     
217     public void fireBodyChange (String JavaDoc oldVal, String JavaDoc newVal) {
218         PropertyChangeEvent evt = new PropertyChangeEvent(getElement(), PROP_BODY, oldVal, newVal);
219         fireOwnPropertyChange(evt);
220     }
221     
222     // Utility methods.
223
//////////////////////////////////////////////////////////////////////////////////
224
protected final SourceElementImpl findSource() {
225         return getDeclaringImpl().findSource();
226     }
227     
228     protected Element cloneSelf() {
229         InitializerElement el = new InitializerElement();
230         try {
231             el.setStatic(isStatic());
232         } catch (SourceException ex) {
233         }
234         return el;
235     }
236     
237     // Serialization support
238
//////////////////////////////////////////////////////////////////////////////////
239
public Object JavaDoc readResolve() {
240         return null;
241     }
242     
243     // ...........................................................................
244

245     static class InitializerListener extends MemberElementImpl.MemberElementListener {
246                 
247         InitializerListener (InitializerElementImpl impl) {
248             super (impl);
249         }
250
251         public void doChange(MDRChangeEvent event) {
252             super.doChange (event);
253             if (event instanceof AttributeEvent) {
254                 AttributeEvent attrEv = (AttributeEvent) event;
255                 if (attrEv.getAttributeName ().equals ("bodyText")) { // NOI18N
256
((InitializerElementImpl) impl).fireBodyChange (
257                         (String JavaDoc) attrEv.getOldElement (),
258                         (String JavaDoc) attrEv.getNewElement ()
259                      );
260                 }
261             }
262         }
263         
264     }
265     
266 }
267
Popular Tags