1 19 20 package org.netbeans.modules.java.bridge; 21 22 import java.beans.*; 23 import java.util.*; 24 import java.lang.ref.WeakReference ; 25 26 import org.openide.nodes.Node; 27 import org.openide.src.*; 28 29 import javax.jmi.reflect.InvalidObjectException; 30 import javax.jmi.reflect.RefBaseObject; 31 32 import org.netbeans.api.mdr.events.*; 33 import org.netbeans.jmi.javamodel.Field; 34 import org.netbeans.jmi.javamodel.IsOfType; 35 import org.netbeans.jmi.javamodel.TypeReference; 36 import org.netbeans.jmi.javamodel.ArrayReference; 37 import org.netbeans.jmi.javamodel.EnumConstant; 38 import org.netbeans.jmi.javamodel.PrimitiveType; 39 40 final class FieldElementImpl extends MemberElementImpl implements FieldElement.Impl { 41 42 private static final long serialVersionUID = 6799964214013985830L; 43 44 private ElementImpl.ElementListener fieldListener; 45 46 FieldElementImpl(DefaultLangModel model, Field fieldElement) { 47 super(model, fieldElement); 48 javadoc = new JavaDocImpl.Field(null, this); 49 } 50 51 public void connectListener () { 52 FieldListener fieldListener = new FieldListener (this); 53 fieldListener.connect (); 54 } 55 56 protected void createFromModel(Element el) throws SourceException { 57 super.createFromModel(el); 58 FieldElement f = (FieldElement)el; 59 setType(f.getType()); 60 setInitValue(f.getInitValue()); 61 setJavaDocText(f.getJavaDoc().getRawText(), true); 62 } 63 64 67 public Type getType() { 68 repository.beginTrans(false); 69 try { 70 if (javaElement.isValid()) { 71 setClassPath(); 72 if (javaElement instanceof EnumConstant) { 73 return descrToType(((Field) javaElement).getType()); 74 } 75 javaElement.refImmediateComposite(); org.netbeans.jmi.javamodel.Type t = ((Field)javaElement).getType(); 77 if (t instanceof PrimitiveType) { 78 return descrToType(t); 79 } 80 return typeReferenceToType (((Field) javaElement).getTypeName ()); 81 } else { 82 if (fieldListener != null) 83 return ((FieldListener)fieldListener).type; 84 else 85 return Type.VOID; 86 } 87 } finally { 88 repository.endTrans(false); 89 } 90 } 91 92 public JavaDoc.Field getJavaDoc() { 93 updateJavadoc(); 94 return (JavaDoc.Field)javadoc; 95 } 96 97 public Object readResolve() { 98 return null; 99 } 100 101 104 public void setType(Type type) throws SourceException { 105 checkWritable(true); 106 checkDocument(); 107 boolean failed = true; 108 repository.beginTrans (true); 109 try { 110 if (javaElement.isValid()) { 111 setClassPath(); 112 Type oldType = getType (); 113 PropertyChangeEvent evt; 114 type = resolveType(type); 115 if (compareSourceTypes(oldType, type)) { 116 failed = false; 117 return; 118 } 119 evt = new PropertyChangeEvent(getElement(), PROP_TYPE, oldType, type); 120 checkVetoablePropertyChange(evt); 121 if (type.isPrimitive()) { 122 ((Field) javaElement).setType(typeToDescr(type)); 123 } else { 124 ((Field) javaElement).setTypeName(typeToTypeReference(type)); 125 } 126 failed = false; 127 } else { 128 failed = false; 129 throwIsInvalid (); 130 } 131 } finally { 132 repository.endTrans (failed); 133 } 134 } 135 136 139 public String getInitValue() { 140 repository.beginTrans(false); 141 try { 142 if (javaElement.isValid()) { 143 setClassPath(); 144 return ((Field)javaElement).getInitialValueText(); 145 } else { 146 return null; 147 } 148 } finally { 149 repository.endTrans(false); 150 } 151 } 152 153 156 public void setInitValue(String newInitValue) throws SourceException { 157 checkWritable(false); 158 checkDocument(); 159 String oldInitValue=getInitValue(); 160 boolean failed = true; 161 repository.beginTrans (true); 162 try { 163 if (javaElement.isValid()) { 164 PropertyChangeEvent evt; 165 setClassPath(); 166 if ((newInitValue == oldInitValue) || 167 ((newInitValue != null) && newInitValue.equals(oldInitValue))) { 168 return; 169 } 170 evt = new PropertyChangeEvent(getElement(), PROP_INIT_VALUE, oldInitValue, newInitValue); 171 checkVetoablePropertyChange(evt); 173 174 ((Field)javaElement).setInitialValueText(newInitValue); 175 failed = false; 176 } else { 177 failed = false; 178 throwIsInvalid (); 179 } 180 } finally { 181 repository.endTrans (failed); 182 } 183 } 184 185 public void fireTypeChange (Type oldType, Type newType) { 186 oldType = resolveType (oldType); 187 newType = resolveType (newType); 188 if (compareSourceTypes(oldType, newType)) 189 return; 190 191 PropertyChangeEvent evt = new PropertyChangeEvent(getElement(), PROP_TYPE, oldType, newType); 192 fireOwnPropertyChange(evt); 193 194 FieldElement oldField = (FieldElement) cloneSelf (); 195 try { 196 FieldElement.Impl impl = (FieldElement.Impl) oldField.getCookie (Element.Impl.class); 197 impl.setType (oldType); 198 } catch (SourceException e) { 199 e.printStackTrace (); 200 } 201 notifyConnectionChange (oldField); 202 } 203 204 public void fireInitValueChange (String oldVal, String newVal) { 205 PropertyChangeEvent evt = new PropertyChangeEvent(getElement(), PROP_INIT_VALUE, oldVal, newVal); 206 fireOwnPropertyChange(evt); 207 } 208 209 212 protected Element createWrapperElement() { 213 return null; 214 } 215 216 public String toString() { 217 return "FieldElementImpl[" + getName().getSourceName() + "]"; } 219 220 protected Element cloneSelf() { 221 return (Element)((FieldElement)getElement()).clone(); 222 } 223 224 226 static class FieldListener extends MemberElementImpl.MemberElementListener implements MDRChangeListener { 227 228 230 private Type type; 231 232 FieldListener (FieldElementImpl impl) { 233 super (impl); 234 } 235 236 public void connect () { 237 if (REGISTER_LISTENER) { 238 super.connect (); 239 TypeReference typeRef = ((Field) javaElement).getTypeName(); 240 if (typeRef != null) { 241 type = ((FieldElementImpl) getImpl ()).typeReferenceToType (typeRef); 242 } else { 243 type = null; 244 } 245 } 246 } 247 248 public void doChange(MDRChangeEvent event) { 249 super.doChange (event); 250 251 if (event instanceof AttributeEvent) { 252 AttributeEvent attrEvent = (AttributeEvent) event; 253 String attrName = attrEvent.getAttributeName(); 254 Object source = event.getSource(); 255 if (attrName.equals("typeName")) { Type oldType = type; 257 TypeReference typeRef = (TypeReference) attrEvent.getNewElement(); 258 if (typeRef != null) { 259 type = ((FieldElementImpl) getImpl()).typeReferenceToType(typeRef); 260 } else { 261 type = null; 262 } 263 ((FieldElementImpl) impl).fireTypeChange (oldType, type); 264 } else if (attrName.equals("initialValueText")) { ((FieldElementImpl) impl).fireInitValueChange ((String )attrEvent.getOldElement(), (String )attrEvent.getNewElement()); 266 } 267 } 268 } 269 270 } 271 272 } 273 | Popular Tags |