1 19 20 package org.netbeans.modules.mdrxml.util; 21 22 26 import java.lang.ref.*; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import javax.jmi.reflect.*; 30 import org.openide.util.RequestProcessor; 31 import org.netbeans.api.looks.*; 32 import org.netbeans.spi.looks.*; 33 import org.netbeans.api.mdr.*; 34 import org.netbeans.api.mdr.events.*; 35 import xmlmodel.*; 36 37 public class XMLModelEventTranslator implements MDRPreChangeListener { 38 39 private static HashMap xmlPackages; 40 private static final int DELAY = 60000; 41 42 protected XmlmodelPackage xmlPkg; 43 protected HashMap contexts = new HashMap (); 44 protected HashMap registeredNodes = new HashMap (); 45 46 47 private XMLModelEventTranslator(XmlmodelPackage xmlPkg) { 48 this.xmlPkg = xmlPkg; 49 ((MDRChangeSource)this.xmlPkg).addListener(this); 50 } 51 52 void addNodeSubstitute(NodeEventTranslator t) { 53 this.addNodeSubstitute((RefBaseObject)((Look.NodeSubstitute)t.get()).getRepresentedObject(), t); 54 } 55 56 void addNodeSubstitute(RefBaseObject key, NodeEventTranslator t) { 57 this.registeredNodes.put(key.refMofId(), t); 58 } 59 60 void removeNodeSubstitute(RefBaseObject key, NodeEventTranslator t) { 61 String id = key.refMofId(); 62 if (registeredNodes.get (id) == t) { 63 this.registeredNodes.remove (id); 64 } 65 } 66 67 public synchronized void plannedChange(MDRChangeEvent event) { 68 if ((event.getType() & InstanceEvent.EVENT_INSTANCE_DELETE) == InstanceEvent.EVENT_INSTANCE_DELETE) { 69 Node node = (Node) event.getSource(); 70 RefBaseObject affectedObject = node.getParentNode(); 71 if (affectedObject == null && (node instanceof RootNode)) 72 affectedObject = this.xmlPkg; 73 Look.NodeSubstitute affectedSubstitute = this.getNodeSubstitute(affectedObject); 74 if (affectedSubstitute != null) { 75 this.contexts.put(event.getSource(), affectedSubstitute); 76 } 77 } 78 } 79 80 public void changeCancelled(MDRChangeEvent event) { 81 this.contexts.remove(event.getSource()); 82 } 83 84 public void change(MDRChangeEvent event) { 85 Look.NodeSubstitute affectedSubstitute = null; 86 if ((event.getType() & InstanceEvent.EVENT_INSTANCE_DELETE) == InstanceEvent.EVENT_INSTANCE_DELETE) { 87 affectedSubstitute = (Look.NodeSubstitute) this.contexts.remove(event.getSource()); 88 } 89 else if ((event.getType() & InstanceEvent.EVENT_INSTANCE_CREATE) == InstanceEvent.EVENT_INSTANCE_CREATE) { 90 Node node = (Node) ((InstanceEvent)event).getInstance(); 91 RefBaseObject parent = node.getParentNode(); 92 if (parent == null && node instanceof RootNode) 93 parent = this.xmlPkg; 94 affectedSubstitute = this.getNodeSubstitute(parent); 95 } 96 else if ((event.getType() & AssociationEvent.EVENT_ASSOCIATION_ADD) == AssociationEvent.EVENT_ASSOCIATION_ADD) { 97 affectedSubstitute = this.getNodeSubstitute(((AssociationEvent)event).getFixedElement()); 98 } 99 100 if (affectedSubstitute != null) 101 affectedSubstitute.refreshChildren(); 102 } 103 104 private synchronized Look.NodeSubstitute getNodeSubstitute(RefBaseObject key) { 105 if (key == null) 106 return null; 107 Reference ref = (Reference) this.registeredNodes.get(key.refMofId()); 108 if (ref == null) 109 return null; 110 Look.NodeSubstitute substitute = (Look.NodeSubstitute) ref.get(); 111 if (substitute == null) { 112 this.registeredNodes.remove(key.refMofId()); 113 } 114 return substitute; 115 } 116 117 118 public static XMLModelEventTranslator getTranslator(XmlmodelPackage pkg) { 119 Reference ref = (Reference) xmlPackages.get(pkg); 120 if (ref == null) 121 return null; 122 return (XMLModelEventTranslator) ref.get(); 123 } 124 125 public static XMLModelEventTranslator addXMLPackage(Look.NodeSubstitute substitute) { 126 if (substitute == null || !(substitute.getRepresentedObject() instanceof XmlmodelPackage)) 127 return null; 128 if (xmlPackages == null) { 129 xmlPackages = new HashMap (5); 130 RequestProcessor.postRequest(new CleanerTask(),DELAY); 131 } 132 XMLModelEventTranslator translator = null; 133 XmlmodelPackage pkg = (XmlmodelPackage) substitute.getRepresentedObject(); 134 WeakReference ref = (WeakReference) xmlPackages.get(pkg); 135 if (ref != null) { 136 translator = (XMLModelEventTranslator) ref.get(); 137 if (translator != null) 138 return translator; 139 } 140 translator = new XMLModelEventTranslator(pkg); 141 synchronized (xmlPackages) { 142 xmlPackages.put(pkg, new WeakReference(translator)); 143 } 144 return translator; 145 } 146 147 148 private static class CleanerTask implements Runnable { 149 public void run() { 150 synchronized (xmlPackages) { 151 Iterator it = xmlPackages.values().iterator(); 152 while (it.hasNext()) { 153 Reference ref = (Reference) it.next(); 154 XMLModelEventTranslator et = (XMLModelEventTranslator) ref.get(); 155 if (et == null) { 156 it.remove(); 157 } 158 else if (et.registeredNodes.size() == 0) { 159 ((MDRChangeSource)et.xmlPkg).removeListener (et); 160 it.remove (); 161 } 162 } 163 } 164 RequestProcessor.postRequest(this,DELAY); 165 } 166 } 167 168 } 169 | Popular Tags |