1 package org.netbeans.modules.java.bridge; 2 3 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.lang.ref.*; 25 import java.util.*; 26 27 import org.netbeans.api.java.classpath.ClassPath; 28 import org.openide.ErrorManager; 29 30 import org.openide.src.*; 31 32 import org.netbeans.api.mdr.events.*; 33 import org.netbeans.jmi.javamodel.CallableFeature; 34 import org.netbeans.jmi.javamodel.Field; 35 import org.netbeans.modules.java.JavaDataObject; 36 import org.openide.nodes.Node; 37 38 import org.netbeans.jmi.javamodel.Resource; 39 import org.netbeans.jmi.javamodel.JavaPackage; 40 import org.netbeans.jmi.javamodel.JavaPackageClass; 41 import org.netbeans.jmi.javamodel.JavaModelPackage; 42 import org.netbeans.jmi.javamodel.JavaClass; 43 import org.netbeans.jmi.javamodel.Method; 44 import org.netbeans.jmi.javamodel.Parameter; 45 import org.netbeans.jmi.javamodel.ParameterizedType; 46 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 47 import org.netbeans.modules.javacore.jmiimpl.javamodel.ResourceImpl; 48 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 49 import org.openide.filesystems.FileObject; 50 51 public class SourceElementImpl extends ElementImpl implements SourceElement.Impl, ElementOrder, 52 PropertyChangeListener { 53 54 private transient FeaturesCollection members; 55 transient TopClassesCollection topClasses; 56 transient ImportsCollection imports; 57 58 private ElementImpl.ElementListener classesListener, sourceListener, importsListener; 59 60 private IdentContextSupport identSupp; 61 62 private transient Resource resource; 63 64 private transient JavaDataObject javaObject; 65 66 private static final long serialVersionUID = 8506642610861188475L; 67 68 70 public SourceElementImpl(DefaultLangModel model, Resource resource, JavaDataObject jdo) { 71 super(model, resource); 72 73 this.resource = resource; 74 javaObject = jdo; 75 identSupp = new IdentContextSupport(11); 76 77 members = new FeaturesCollection (this); 78 topClasses = new TopClassesCollection (members, resource, this); 79 imports = new ImportsCollection (members, (Resource) resource); 80 } 81 82 public void connectListener () { 83 sourceListener = new SourceListener (this); 84 sourceListener.connect (); 85 classesListener = new TopClassesCollection.TopClassesListener (this); 86 classesListener.connect (); 87 org.netbeans.modules.javacore.jmiimpl.javamodel.ResourceImpl resImpl = 88 (org.netbeans.modules.javacore.jmiimpl.javamodel.ResourceImpl) resource; 89 if (resImpl.importsInited()) { 90 importsListener = new ImportsCollection.ImportsListener (this); 91 importsListener.connect (); 92 } else { 93 resImpl.addImportsListener(this); 94 } 95 } 96 97 boolean isResourceValid() { 98 return resource.isValid(); 99 } 100 101 public void propertyChange (PropertyChangeEvent event) { 103 importsListener = new ImportsCollection.ImportsListener (this); 104 importsListener.connect (); 105 } 106 107 protected void notifyElementCreated() { 108 } 110 111 protected void createFromModel(Element model) throws SourceException { 112 } 113 114 public Element[] getElements() { 115 return getClasses(); 116 } 117 118 public Identifier getPackage() { 119 repository.beginTrans(false); 120 try { 121 if (resource.isValid()) { 122 setClassPath(); 123 String javaPackage = resource.getPackageName(); 124 return javaPackage != null ? Identifier.create(javaPackage) : null; 125 } else { 126 return null; 127 } 128 } finally { 129 repository.endTrans(false); 130 } 131 } 132 133 public Import[] getImports() { 134 return imports.getImports (); 135 } 136 137 public ImportElement[] getImportElements() { 138 return (ImportElement[]) imports.getElements (); 139 } 140 141 public ClassElement getClass(Identifier id) { 142 return topClasses.getClass (id); 143 } 144 145 public ClassElement[] getClasses() { 146 return topClasses.getClasses(); 147 } 148 149 public ClassElement[] getAllClasses() { 150 ClassElement[] tops = getClasses(); 151 Collection list = new LinkedList(); 152 153 for (int i = 0; i < tops.length; i++) { 154 addAllClasses(tops[i], list); 155 } 156 return (ClassElement[])list.toArray(new ClassElement [0]); 157 } 158 159 private void addAllClasses(ClassElement c, Collection col) { 160 col.add(c); 161 ClassElement[] inner = c.getClasses(); 162 for (int i = 0; i < inner.length; i++) 163 addAllClasses(inner[i], col); 164 } 165 166 public void setPackage(Identifier packageID) throws SourceException { 169 checkWritable(false); 170 checkDocument(); 171 boolean failed = true; 172 repository.beginTrans (true); 173 try { 174 if (resource.isValid()) { 175 setClassPath(); 176 Identifier old = getPackage (); 177 if (old == packageID || 178 (old != null && packageID != null && 179 old.getSourceName().equals(packageID.getSourceName()))) { 180 failed = false; 181 return; 182 } 183 PropertyChangeEvent evt; 184 JavaPackageClass pkgProxy = ((JavaModelPackage)resource.refImmediatePackage()).getJavaPackage(); 185 String packId=packageID!=null?packageID.getFullName():""; 186 JavaClass defClasses[]; 187 int i; 188 evt = new PropertyChangeEvent (getEventSource(), PROP_PACKAGE, 189 old, packageID); 190 checkVetoablePropertyChange(evt); 191 192 resource.setPackageName(packId); 193 194 209 failed = false; 210 } else { 211 failed = false; 212 throwIsInvalid (); 213 } 214 } finally { 215 repository.endTrans (failed); 216 } 217 } 218 219 public void firePackageChange (Identifier oldId, Identifier newId) { 220 if (oldId == newId || 221 (oldId != null && newId != null && 222 oldId.getSourceName().equals(newId.getSourceName()))) { 223 return; 224 } 225 PropertyChangeEvent evt = new PropertyChangeEvent ( 226 getEventSource(), 227 PROP_PACKAGE, 228 oldId, newId 229 ); 230 fireOwnPropertyChange(evt); 231 232 } 235 236 public void changeImports(Import[] defs, int operation) throws SourceException { 237 checkWritable(false); 238 checkDocument(); 239 imports.changeImports (defs, operation); 240 } 241 242 public void changeClasses(ClassElement[] classes, int operation) throws SourceException { 243 checkWritable(false); 244 checkDocument(); 245 topClasses.changeMembers(classes, operation); 246 } 247 248 private void notifyCreate(Element[] els) { 249 for (int i = 0; i < els.length; i++) { 250 ElementImpl impl = (ElementImpl)els[i].getCookie(ElementImpl.class); 251 impl.notifyCreate(); 252 } 253 } 254 255 protected void notifyCreate() { 256 267 } 268 269 private Binding.Source getSourceBinding() { 270 return (Binding.Source)getBinding(); 271 } 272 273 277 public int getStatus() { 278 return SourceElement.STATUS_NOT; 279 } 280 281 public org.openide.util.Task prepare() { 282 return org.openide.util.Task.EMPTY; } 286 287 public void runAtomic(Runnable run) { 288 boolean failed = true; 289 repository.beginTrans (true); 290 try { 291 setClassPath(); 292 run.run (); 293 failed = false; 294 } finally { 295 repository.endTrans (failed); 296 } 297 } 298 299 public void runAtomicAsUser(Runnable run) throws SourceException { 300 runAtomic (run); } 302 303 public void updateMembers(String name, Element[] elements, int[] optMap) { 304 323 } 324 325 protected void checkWritable(boolean unsafeOp) throws SourceException { 326 SourceException e = null; 327 int status = resource.getStatus(); 328 if ((status & ResourceImpl.HAS_GENERICS) != 0) { 329 e = new SourceException.IO("Source containing generics cannot be modified: " + resource.getName()); } else if ((status & ResourceImpl.HAS_ANNOTATION) != 0) { 331 e = new SourceException.IO("Source containing annotations cannot be modified: " + resource.getName()); } 333 if (e != null) { 334 if (JDK15_CHECKS_DISABLED) { 335 if (unsafeOp) { 336 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 337 } 338 } else { 339 throw e; 340 } 341 } 342 } 343 344 public void setParent(ElementImpl impl) { 347 } 349 350 protected SourceElementImpl findSource() { 351 return this; 352 } 353 354 public Object readResolve() { 355 return null; 356 } 357 358 protected Element cloneSelf() { 359 return null; 360 } 361 362 protected Identifier createLocalIdentifier(Identifier id, int status) { 363 return identSupp.create(id, status); 364 } 365 366 protected boolean checkIdentifierContext(Identifier id) { 367 return identSupp.checkContext(id); 368 } 369 370 protected boolean parentValid() { 371 return isValid(); 372 } 373 374 protected void notifyRemove() { 375 394 } 395 396 403 public Node.Cookie getCookie(Class type) { 404 if (type.isAssignableFrom(getClass())) 405 return this; 406 return javaObject.getCookie(type); 409 } 410 411 413 static class SourceListener extends ElementImpl.ElementListener { 414 415 Identifier packageId = null; 416 417 SourceListener (SourceElementImpl impl) { 418 super (impl); 419 String pkgName = ((Resource) javaElement).getPackageName(); 420 packageId = pkgName != null ? Identifier.create (pkgName) : null; 421 } 422 423 434 435 public void doChange(MDRChangeEvent event) { 436 super.doChange (event); 437 if ((event instanceof AttributeEvent) && 438 ((AttributeEvent) event).getAttributeName ().equals ("packageName")) { Identifier oldId = packageId; 440 String name = (String ) ((AttributeEvent) event).getNewElement (); 441 packageId = name != null ? Identifier.create (name) : null; 442 ((SourceElementImpl) impl).firePackageChange (oldId, packageId); 443 } 444 } 445 } 446 447 } | Popular Tags |