1 19 20 package org.netbeans.modules.java.bridge; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.util.*; 24 import org.openide.src.*; 25 import org.netbeans.modules.java.ElementFactory.Item; 26 import org.netbeans.modules.java.JavaDataObject; 27 import org.netbeans.jmi.javamodel.JavaClass; 28 import org.netbeans.jmi.javamodel.Constructor; 29 import org.netbeans.jmi.javamodel.Method; 30 import org.netbeans.jmi.javamodel.Initializer; 31 import org.netbeans.jmi.javamodel.Field; 32 import org.netbeans.jmi.javamodel.Import; 33 import org.netbeans.api.mdr.MDRepository; 34 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 35 import org.netbeans.modules.javacore.JMManager; 36 import org.openide.ErrorManager; 37 38 45 public class DefaultLangModel implements LangModel, LangModel.Updater, Runnable { 46 private static boolean initialized; 47 48 private static Class CLASS_MEMBER_IMPL; 49 50 private static Class CLASS_INITIALIZER_IMPL; 51 52 static final Positioner DEFAULT_POSITIONER = new DefaultInsertStrategy(); 53 54 57 private Object locked; 58 59 private Env env; 60 61 private Object writerNotify; 62 63 private Thread writingThread; 64 65 private int readerCount; 66 67 private Thread reader; 68 69 private BindingFactory bindingFactory; 71 72 private WrapperFactory wrapperFactory; 73 74 77 private EventQueue eventQueue; 78 79 81 private int writeLocks; 82 83 private int masterLocks; 84 85 89 private boolean transactionCommited; 90 91 95 private boolean transactionConstrained; 96 97 100 private boolean eventDispatch; 101 102 105 Collection preCommitListeners; 106 107 110 Collection postCommitListeners; 111 112 116 LinkedList outputQueue; 117 118 boolean firingEvents; 119 120 private JavaDataObject javaDataObject; 121 122 public DefaultLangModel(JavaDataObject jdo) { 123 this (new LangEnvImpl (jdo), jdo); 124 } 125 126 public DefaultLangModel(Env env, JavaDataObject jdo) { 127 this.env = env; 128 this.javaDataObject = jdo; 129 writerNotify = new Object (); 130 this.eventQueue = new EventQueue(null); 131 } 132 133 public JavaDataObject getJavaDataObject () { 134 return javaDataObject; 135 } 136 137 private static void initializeClasses() { 138 if (initialized) 139 return; 140 try { 141 CLASS_MEMBER_IMPL = Class.forName("org.netbeans.modules.java.bridge.MemberElementImpl"); CLASS_INITIALIZER_IMPL = Class.forName("org.netbeans.modules.java.bridge.InitializerElementImpl"); } catch (ClassNotFoundException ex) { 144 } 146 initialized = true; 147 } 148 149 154 public Object clone() { 155 return null; 156 } 157 158 162 163 public ClassElementImpl createTopClass(SourceElement src, JavaClass javaClass) { 164 ClassElementImpl c = new ClassElementImpl(this, javaClass); 165 getWrapper().wrapClass(c, src); 166 c.setParent(src); 167 return c; 168 } 169 170 public ClassElementImpl createInnerClass(ClassElement parent, JavaClass javaClass) { 171 ClassElementImpl c = new ClassElementImpl(this, javaClass); 172 getWrapper().wrapClass(c, parent); 173 c.setParent(parent); 174 return c; 175 } 176 177 public FieldElementImpl createField(ClassElement parent, Field field) { 178 FieldElementImpl impl = new FieldElementImpl(this, field); 179 getWrapper().wrapField(impl, parent); 180 impl.setParent(parent); 181 return impl; 182 } 183 184 public ConstructorElementImpl createConstructor(ClassElement parent, Constructor constructor) { 185 ConstructorElementImpl impl = new ConstructorElementImpl(this, constructor); 186 getWrapper().wrapConstructor(impl, parent); 187 impl.setParent(parent); 188 return impl; 189 } 190 191 public MethodElementImpl createMethod(ClassElement parent, Method method) { 192 MethodElementImpl impl = new MethodElementImpl(this, method); 193 getWrapper().wrapMethod(impl, parent); 194 impl.setParent(parent); 195 return impl; 196 } 197 198 public ImportImpl createImport(SourceElement parent, Import imp) { 199 ImportImpl impl = new ImportImpl(this, imp); 200 getWrapper().wrapImport(impl, parent); 201 impl.setParent(parent); 202 return impl; 203 } 204 205 public InitializerElementImpl createInitializer(ClassElement parent, Initializer initializer) { 206 InitializerElementImpl impl = new InitializerElementImpl(this, initializer); 207 getWrapper().wrapInitializer(impl, parent); 208 impl.setParent(parent); 209 return impl; 210 } 211 212 214 public SourceElementImpl createSource() { 215 MDRepository repo = JavaMetamodel.getDefaultRepository(); 216 repo.beginTrans(false); 217 try { 218 return new SourceElementImpl(this, null, null); 219 } finally { 220 repo.endTrans(); 221 } 222 } 223 224 public ClassElementImpl createTopClass(SourceElement src) { 225 ClassElementImpl c = new ClassElementImpl(this, null); 226 getWrapper().wrapClass(c, src); 227 c.setParent(src); 228 return c; 229 } 230 231 public ClassElementImpl createInnerClass(ClassElement parent) { 232 ClassElementImpl c = new ClassElementImpl(this, null); 233 getWrapper().wrapClass(c, parent); 234 c.setParent(parent); 235 return c; 236 } 237 238 public FieldElementImpl createField(ClassElement parent) { 239 FieldElementImpl impl = new FieldElementImpl(this, null); 240 getWrapper().wrapField(impl, parent); 241 impl.setParent(parent); 242 return impl; 243 } 244 245 public ConstructorElementImpl createConstructor(ClassElement parent) { 246 ConstructorElementImpl impl = new ConstructorElementImpl(this, null); 247 getWrapper().wrapConstructor(impl, parent); 248 impl.setParent(parent); 249 return impl; 250 } 251 252 public MethodElementImpl createMethod(ClassElement parent) { 253 MethodElementImpl impl = new MethodElementImpl(this, null); 254 getWrapper().wrapMethod(impl, parent); 255 impl.setParent(parent); 256 return impl; 257 } 258 259 public ImportImpl createImport(SourceElement parent) { 260 ImportImpl impl = new ImportImpl(this, null); 261 getWrapper().wrapImport(impl, parent); 262 impl.setParent(parent); 263 return impl; 264 } 265 266 public InitializerElementImpl createInitializer(ClassElement parent) { 267 InitializerElementImpl impl = new InitializerElementImpl(this, null); 268 getWrapper().wrapInitializer(impl, parent); 269 impl.setParent(parent); 270 return impl; 271 } 272 273 275 final Identifier resolveIdent(Element context, Identifier original) { 276 return env.resolveTypeIdent(context, original); 277 } 278 279 final Type resolveType(Element context, Type t) { 280 return env.resolveType(context, t); 281 } 282 283 public void addPreCommitListener(CommitListener l) { 284 synchronized (this) { 285 if (preCommitListeners == null) 286 preCommitListeners = new LinkedList(); 287 } 288 synchronized (preCommitListeners) { 289 preCommitListeners.add(l); 290 } 291 } 292 293 public void addPostCommitListener(CommitListener l) { 294 synchronized (this) { 295 if (postCommitListeners == null) 296 postCommitListeners = new LinkedList(); 297 } 298 synchronized (postCommitListeners) { 299 postCommitListeners.add(l); 300 } 301 } 302 303 public void removePreCommitListener(CommitListener l) { 304 if (preCommitListeners == null) 305 return; 306 synchronized (preCommitListeners) { 307 preCommitListeners.remove(l); 308 } 309 } 310 311 public void removePostCommitListener(CommitListener l) { 312 if (postCommitListeners == null) 313 return; 314 synchronized (postCommitListeners) { 315 postCommitListeners.remove(l); 316 } 317 } 318 319 final void notifyEventsDispatched(boolean dispatchOn) { 320 this.eventDispatch = dispatchOn; 321 } 322 323 public final Object writeLock() { 324 return null; 325 331 } 332 333 private void createEventQueue() { 334 eventQueue = new EventQueue(eventQueue); 335 } 336 337 341 final Object masterWriteLock() { 342 356 return null; 357 } 358 359 public Object tryWriteLock() { 360 return tryWriteLock(false); 361 } 362 363 private Object tryWriteLock(boolean master) { 364 374 return null; 375 } 376 377 public final Object doWriteLock() { 378 403 return null; 404 } 405 406 412 public final void releaseWriteLock(Object handle) { 413 releaseWriteLock(handle, false); 414 } 415 416 final void releaseWriteLock(Object handle, boolean forkThread) { 417 462 } 463 464 468 private void enqueue(EventQueue q) { 469 476 } 477 478 public void run() { 479 } 481 482 private void processOutputQueue() { 483 510 } 511 512 private void firePreCommitEvents(EventQueue q) { 513 fireCommitEvents(preCommitListeners, q); 514 } 515 516 private void firePostCommitEvents(EventQueue what) { 517 fireCommitEvents(postCommitListeners, what); 518 } 519 520 private void fireCommitEvents(Collection origListeners, EventQueue data) { 521 542 } 543 544 private EventQueue mergeEventQueues() { 545 559 return null; 560 } 561 562 public void commitChanges() { 563 571 } 572 573 public void runAtomic(Runnable r) throws SourceException { 574 Object token = writeLock(); 575 try { 576 r.run(); 577 commitChanges(); 578 } finally { 579 releaseWriteLock(token); 580 } 581 } 582 583 protected boolean isConstrained() { 584 return this.transactionConstrained; 585 } 586 587 protected final org.openide.nodes.Node.Cookie findElementCookie(Element el, Class clazz) { 588 return env.findCookie(el, clazz); 589 } 590 591 595 public boolean runUpdate(Runnable r, boolean disableConstraints) 596 throws SourceException { 597 Object token = tryWriteLock(true); 598 if (token == null) 599 return false; 600 boolean saveConstraint = this.transactionConstrained; 601 602 try { 603 this.transactionConstrained = !disableConstraints; 604 r.run(); 605 609 commitChanges(); 610 return true; 611 } catch (Exception ex) { 612 ex.printStackTrace(); 613 throw new SourceException("Unexpected implementation error"); } finally { 615 this.transactionConstrained = saveConstraint; 616 releaseWriteLock(token, true); 617 } 618 } 619 620 private void doRunAtomic(ExceptionRunnable r) throws SourceException { 621 try { 623 r.run(); 624 } catch (SourceException ex) { 625 throw ex; 626 } catch (Exception ex) { 627 throw new SourceException("Unexpected implementation error."); } 629 } 630 631 public void runAtomic(ExceptionRunnable r) throws SourceException { 632 boolean fire = false; 633 boolean ok = false; 634 Object token = null; 635 636 token = writeLock(); 637 try { 638 doRunAtomic(r); 639 commitChanges(); 640 } finally { 641 releaseWriteLock(token); 642 } 643 } 644 645 public Object getManagementLock() { 646 return writerNotify; 647 } 648 649 public final void readLock() { 650 669 } 670 671 public final void releaseReadLock() { 672 682 } 683 684 protected EventQueue getEventQueue() { 685 return this.eventQueue; 686 } 687 688 public boolean isWriteLocked() { 689 return this.writingThread != null; 690 } 691 692 public Item createImport(Import im, int from, int to) { 693 return null; 694 } 695 696 public void notifyElementChanged(Element ref, Element old) { 697 eventQueue.elementChanged(ref, old); 698 } 699 700 public void notifyElementCreated(Element ref) { 701 eventQueue.elementCreated(ref); 702 } 703 704 public void notifyElementRemoved(Element ref) { 705 eventQueue.elementRemoved(ref); 706 } 707 708 public void fireModelElementChange(ElementImpl bean, PropertyChangeEvent evt) { 709 queuePropertyChange(bean, evt.getPropertyName(), evt); 710 } 711 712 public void fireModelElementChange(Element.Impl bean, PropertyChangeEvent evt) { 713 queuePropertyChange((ElementImpl)bean, evt.getPropertyName(), evt); 714 } 715 716 719 protected BindingFactory getBindingFactory() { 720 if (this.bindingFactory != null) { 721 return this.bindingFactory; 722 } 723 return this.bindingFactory = env.getBindingFactory(); 724 } 725 726 public WrapperFactory getWrapper() { 727 if (this.wrapperFactory != null) { 728 return this.wrapperFactory; 729 } 730 synchronized (this) { 731 if (this.wrapperFactory == null) { 732 this.wrapperFactory = env.getWrapperFactory(); 733 } 734 return this.wrapperFactory; 735 } 736 } 737 738 private void queuePropertyChange(ElementImpl bean, String name, PropertyChangeEvent evt) { 741 eventQueue.addPropertyChange(bean, evt); 742 } 743 744 private ElementImpl getElementImpl(Element el) { 745 return (ElementImpl)el.getCookie(ElementImpl.class); 746 } 747 748 public void updateMembers(Element target, String propertyName, Element[] els, 749 int[] orderIndices, 750 int[] optMap) { 751 if (target instanceof ClassElement) { 752 ClassElementImpl impl = (ClassElementImpl)getElementImpl(target); 753 impl.updateMembers(propertyName, els, orderIndices, optMap); 754 } else { 755 SourceElementImpl impl = (SourceElementImpl)getElementImpl(target); 756 impl.updateMembers(propertyName, els, optMap); 757 } 758 } 759 760 public void updateMemberOrder(Element target, String id, 761 Element[] orderedMembers) { 762 ClassElementImpl impl = (ClassElementImpl)getElementImpl(target); 763 impl.updateMemberOrder(orderedMembers); 764 } 765 766 public void activate(Element target) { 767 ElementImpl impl = getElementImpl(target); 768 impl.notifyCreate(); 769 } 770 771 public Binding getElementBinding(Element target) { 772 ElementImpl impl = getElementImpl(target); 773 return impl.getRawBinding(); 774 } 775 776 public boolean isSameContext(Element context, Identifier id) { 777 ElementImpl impl = getElementImpl(context); 778 if (impl == null) 779 return false; 780 return impl.checkIdentifierContext(id); 781 } 782 783 public Identifier createLocalIdentifier(Element context, Identifier id, int status) { 784 ElementImpl impl = getElementImpl(context); 785 if (impl == null) 786 throw new IllegalArgumentException ("Unknown context class: " + context.getClass()); return impl.createLocalIdentifier(id, status); 788 } 789 790 public Identifier createLocalIdentifier(Element context, String full, String source, 791 int status) { 792 Identifier id = Identifier.create(full, source); 793 return createLocalIdentifier(context, id, status); 794 } 795 796 798 private void doFire (Set removed, Set created, Map changed) { 799 if (postCommitListeners != null) { 800 synchronized (postCommitListeners) { 801 for (Iterator it = postCommitListeners.iterator(); it.hasNext();) { 802 CommitListener l = (CommitListener)it.next(); 803 l.changesCommited(created, removed, changed); 804 } 805 } 806 } 807 } 808 809 public void fireElementChanged (Element oldElem, Element newElem) { 810 Map changed = new HashMap (); 811 changed.put (newElem, new Element [] {oldElem, newElem}); 812 813 doFire (new HashSet (), new HashSet (), changed); 814 } 815 816 public void fireElementRemoved (Element elem) { 817 Set removed = new HashSet (); 818 removed.add (elem); 819 820 doFire (removed, new HashSet (), new HashMap ()); 821 } 822 823 public void fireElementAdded (Element elem) { 824 Set created = new HashSet (); 825 created.add (elem); 826 827 doFire (new HashSet (), created, new HashMap ()); 828 } 829 830 public void fireElementSet (Element oldElem, Element newElem) { 831 Set removed = new HashSet (); 832 removed.add (oldElem); 833 Set created = new HashSet (); 834 created.add (newElem); 835 836 doFire (removed, created, new HashMap ()); 837 } 838 839 public Element findElement(Element.Impl impl) { 840 if (!(impl instanceof ElementImpl)) 841 return null; 842 return ((ElementImpl)impl).getElement(); 843 } 844 845 public void firePropertyChange(Element el, PropertyChangeEvent evt) { 846 ElementImpl impl = getElementImpl(el); 847 if (impl == null) { 848 JMManager.getLog().log(ErrorManager.INFORMATIONAL, "DefaultLangModel.firePropertyChange(): impl == null"); return; 851 } 852 impl.fireOwnPropertyChange(evt); 853 } 854 855 public void updateBody(Element el, String bodyContent) throws UnsupportedOperationException { 856 865 } 866 867 public void invalidateModel(SourceElement el) { 868 SourceElementImpl impl = (SourceElementImpl)getElementImpl(el); 869 Object token = writeLock(); 870 try { 871 if (impl == null) 872 return; 873 impl.notifyRemove(); 874 commitChanges(); 875 } finally { 876 releaseWriteLock(token); 877 } 878 } 879 } 880 | Popular Tags |