1 19 20 package org.netbeans.modules.java.bridge; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.lang.ref.WeakReference ; 25 import java.util.Iterator ; 26 import java.util.LinkedList ; 27 import java.util.List ; 28 import org.netbeans.api.mdr.MDRepository; 29 import org.netbeans.jmi.javamodel.Resource; 30 import org.netbeans.modules.java.JavaDataObject; 31 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 32 import org.netbeans.modules.javacore.jmiimpl.javamodel.ResourceImpl; 33 import org.openide.filesystems.FileObject; 34 import org.openide.loaders.DataObject; 35 import org.openide.nodes.Node; 36 import org.openide.src.*; 37 import org.openide.util.Task; 38 39 public class SrcElementImpl implements SourceElement.Impl, Node.Cookie, PropertyChangeListener { 40 41 private static final ClassElement[] NO_CLASSES = new ClassElement[0]; 42 43 46 LinkedList listeners; 47 48 52 SourceElement srcElement = null; 53 54 private transient int status = SourceElement.STATUS_OK; 55 56 private transient DefaultLangModel model; 57 58 private transient WeakReference delegate; 59 60 private JavaDataObject javaDataObject; 61 62 private static final long serialVersionUID = 3120888788645227532L; 63 64 65 67 public SrcElementImpl(JavaDataObject javaObject) { 68 javaDataObject = javaObject; 69 model = javaDataObject.getModel (); 70 } 71 72 private SourceElementImpl getDelegate() { 73 SourceElementImpl impl = (delegate != null) ? (SourceElementImpl) delegate.get() : null; 74 if (impl == null || !impl.isResourceValid()) { 75 MDRepository repo = JavaMetamodel.getDefaultRepository(); 76 repo.beginTrans(false); 77 try { 78 FileObject fileObj = javaDataObject.getPrimaryFile(); 79 Resource resource = JavaMetamodel.getManager().getResource (fileObj); 80 if (resource != null) { 81 status = SourceElement.STATUS_OK; 82 impl = new SourceElementImpl (model, resource, javaDataObject); 83 impl.attachedToElement(srcElement); 84 delegate = new WeakReference (impl); 85 } 86 } finally { 87 repo.endTrans(false); 88 } 89 } 90 return impl; 91 } 92 93 public void invalidateDelegate() { 94 delegate = null; 95 } 96 97 106 107 110 protected SourceElement getElement() { 111 return this.srcElement; 112 } 113 114 118 public void attachedToElement(Element el) { 119 this.srcElement = (SourceElement)el; 120 } 121 122 125 public int getStatus() { 126 MDRepository repo = JavaMetamodel.getDefaultRepository(); 127 repo.beginTrans(false); 128 try { 129 FileObject fileObj = javaDataObject.getPrimaryFile(); 130 Resource resource = JavaMetamodel.getManager().getResource(fileObj); 131 if (resource != null) { 132 if (resource.getStatus() == ResourceImpl.HAS_SYNTAX_ERROR) { 133 return SourceElement.STATUS_ERROR; 134 } 135 } 136 } finally { 137 repo.endTrans(false); 138 } 139 return status; 140 } 141 142 145 public Task prepare() { 146 return Task.EMPTY; 147 } 148 149 153 public void setPackage(Identifier id) throws SourceException { 154 SourceElementImpl del = getDelegate(); 155 if (del != null) 156 del.setPackage(id); 157 } 158 159 public Identifier getPackage() { 160 SourceElementImpl del = getDelegate(); 161 if (del != null) 162 return del.getPackage(); 163 else 164 return Identifier.create (""); 165 } 166 167 public Import[] getImports() { 168 SourceElementImpl del = getDelegate(); 169 if (del != null) 170 return del.getImports(); 171 else 172 return new Import[0]; 173 } 174 175 public void changeImports(Import[] elems, int action) throws SourceException { 176 SourceElementImpl del = getDelegate(); 177 if (del != null) 178 del.changeImports(elems, action); 179 } 180 181 public void changeClasses(ClassElement[] elems, int action) throws SourceException { 182 SourceElementImpl del = getDelegate(); 183 if (del != null) 184 del.changeClasses(elems, action); 185 } 186 187 public ClassElement[] getClasses() { 188 SourceElementImpl del = getDelegate(); 189 if (del != null) 190 return del.getClasses(); 191 else 192 return NO_CLASSES; 193 } 194 195 public ClassElement getClass(Identifier name) { 196 SourceElementImpl del = getDelegate(); 197 if (del != null) 198 return del.getClass(name); 199 else 200 return null; 201 } 202 203 public ClassElement[] getAllClasses() { 204 SourceElementImpl del = getDelegate(); 205 if (del != null) 206 return del.getAllClasses(); 207 else 208 return NO_CLASSES; 209 } 210 211 218 public Node.Cookie getCookie(Class type) { 219 if (type.isAssignableFrom(getClass())) 220 return this; 221 if (type.isAssignableFrom(DataObject.class)) 222 return javaDataObject.getCookie (type); 223 SourceElementImpl del = getDelegate(); 224 if (del != null) 225 return del.getCookie(type); 226 else 227 return javaDataObject.getCookie (type); 228 } 229 230 233 public void markCurrent(boolean beforeAfter) { 234 } 235 236 242 public void runAtomic(final Runnable run) { 243 getDelegate().runAtomic (run); 244 265 } 266 270 public void runAtomicAsUser(final Runnable run) throws SourceException { 271 getDelegate().runAtomicAsUser (run); 272 290 } 291 292 295 public synchronized void addPropertyChangeListener(PropertyChangeListener l) { 296 if (listeners == null) { 297 listeners = new LinkedList (); 298 } 299 boolean attach = listeners.isEmpty(); 300 listeners.add(l); 301 302 if (attach) { 303 SourceElementImpl del = getDelegate(); 304 if (del != null) 305 del.addPropertyChangeListener(this); 306 } 307 } 308 309 public synchronized void removePropertyChangeListener(PropertyChangeListener l) { 310 if (listeners == null) 311 return; 312 listeners.remove(l); 313 if (listeners.isEmpty()) { 314 SourceElementImpl del = getDelegate(); 315 if (del != null) 316 del.removePropertyChangeListener(this); 317 } 318 } 319 320 public Object readResolve() { 321 return null; 322 } 323 324 328 public void propertyChange(PropertyChangeEvent evt) { 329 List l; 330 synchronized (this) { 331 if (listeners == null) 332 return; 333 l = new java.util.ArrayList (listeners); 334 } 335 evt = new PropertyChangeEvent ( 337 srcElement, evt.getPropertyName(), 338 evt.getOldValue(), evt.getNewValue()); 339 340 for (Iterator it = l.iterator(); it.hasNext(); ) { 341 PropertyChangeListener ll = (PropertyChangeListener )it.next(); 342 ll.propertyChange(evt); 343 } 344 } 345 346 } 347 | Popular Tags |