KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > mdrxml > util > XMLModelEventTranslator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is Forte for Java, Community Edition. The Initial
16  * Developer of the Original Software is Sun Microsystems, Inc. Portions
17  * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.mdrxml.util;
21
22 /**
23  *
24  * @author Tomas Zezula
25  */

26 import java.lang.ref.*;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
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 JavaDoc xmlPackages;
40     private static final int DELAY = 60000;
41     
42     protected XmlmodelPackage xmlPkg;
43     protected HashMap JavaDoc contexts = new HashMap JavaDoc();
44     protected HashMap JavaDoc registeredNodes = new HashMap JavaDoc();
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 JavaDoc 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 JavaDoc(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 JavaDoc {
149         public void run() {
150                 synchronized (xmlPackages) {
151                     Iterator JavaDoc 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