1 19 20 package org.netbeans.modules.java.bridge; 21 22 import java.lang.reflect.Modifier ; 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 38 class InitializerElementImpl extends MemberElementImpl 39 implements InitializerElement.Impl, Element.Impl2 { 40 41 private transient boolean bodyInited; 42 private transient String 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 s) { 84 } 85 86 public final String 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 content) { 110 122 } 123 124 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 163 public void setBody(String 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 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 oldValue, Integer newValue) { 195 Boolean 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 oldVal, String newVal) { 218 PropertyChangeEvent evt = new PropertyChangeEvent(getElement(), PROP_BODY, oldVal, newVal); 219 fireOwnPropertyChange(evt); 220 } 221 222 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 public Object readResolve() { 240 return null; 241 } 242 243 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")) { ((InitializerElementImpl) impl).fireBodyChange ( 257 (String ) attrEv.getOldElement (), 258 (String ) attrEv.getNewElement () 259 ); 260 } 261 } 262 } 263 264 } 265 266 } 267 | Popular Tags |