KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ddloaders > multiview > EnterpriseBeansNode


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 NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.ddloaders.multiview;
21
22 import org.netbeans.modules.j2ee.dd.api.ejb.Ejb;
23 import org.netbeans.modules.j2ee.dd.api.ejb.EnterpriseBeans;
24 import org.netbeans.modules.j2ee.dd.api.ejb.Entity;
25 import org.netbeans.modules.j2ee.dd.api.ejb.MessageDriven;
26 import org.netbeans.modules.j2ee.dd.api.ejb.Session;
27 import org.netbeans.modules.xml.multiview.SectionNode;
28 import org.netbeans.modules.xml.multiview.ui.BoxPanel;
29 import org.netbeans.modules.xml.multiview.ui.SectionNodeInnerPanel;
30 import org.netbeans.modules.xml.multiview.ui.SectionNodeView;
31 import org.openide.nodes.Children;
32 import org.openide.nodes.Node;
33
34 import java.util.Arrays JavaDoc;
35 import java.util.Comparator JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Map JavaDoc;
38
39 /**
40  * @author pfiala
41  */

42 public class EnterpriseBeansNode extends EjbSectionNode {
43
44     protected EnterpriseBeans enterpriseBeans;
45     private boolean doCheck = false;
46     private boolean checking = false;
47
48     public EnterpriseBeansNode(SectionNodeView sectionNodeView, EnterpriseBeans enterpriseBeans) {
49         super(sectionNodeView, enterpriseBeans, Utils.getBundleMessage("LBL_EnterpriseBeans"),
50                 Utils.ICON_BASE_ENTERPRISE_JAVA_BEANS_NODE);
51         this.enterpriseBeans = enterpriseBeans;
52         setExpanded(true);
53         //getSectionNodePanel().refreshView();
54
}
55
56     private SectionNode createNode(Ejb ejb) {
57         SectionNodeView sectionNodeView = getSectionNodeView();
58         if (ejb instanceof Session) {
59             return new SessionNode(sectionNodeView, (Session) ejb);
60         } else if (ejb instanceof Entity) {
61             return new EntityNode(sectionNodeView, (Entity) ejb);
62         } else if (ejb instanceof MessageDriven) {
63             return new MessageDrivenNode(sectionNodeView, (MessageDriven) ejb);
64         } else {
65             return null;
66         }
67     }
68
69     public SectionNodeInnerPanel createInnerPanel() {
70         SectionNodeView sectionNodeView = getSectionNodeView();
71         BoxPanel boxPanel = new BoxPanel(sectionNodeView) {
72             public void dataModelPropertyChange(Object JavaDoc source, String JavaDoc propertyName, Object JavaDoc oldValue, Object JavaDoc newValue) {
73                 if (source == enterpriseBeans) {
74                     if (oldValue != null && newValue == null||oldValue== null && newValue!= null) {
75                         checkChildren();
76                     }
77                 }
78             }
79
80             public void refreshView() {
81                 checkChildren();
82             }
83         };
84         populateBoxPanel(boxPanel);
85         return boxPanel;
86     }
87
88     private void checkChildren() {
89         Utils.runInAwtDispatchThread(new Runnable JavaDoc() {
90             public void run() {
91                 doCheck = true;
92                 if (setChecking(true)) {
93                     try {
94                         while (doCheck) {
95                             doCheck = false;
96                             check();
97                         }
98                     } finally {
99                         setChecking(false);
100                     }
101                 }
102             }
103         });
104     }
105
106     private synchronized boolean setChecking(boolean value) {
107         if (value) {
108             if (checking) {
109                 return false;
110             } else {
111                 checking = true;
112                 return true;
113             }
114         } else {
115             checking = false;
116             return true;
117         }
118     }
119
120     private void check() {
121         Map JavaDoc nodeMap = new HashMap JavaDoc();
122         Children children = getChildren();
123         Node[] nodes = children.getNodes();
124         for (int i = 0; i < nodes.length; i++) {
125             Node node = nodes[i];
126             nodeMap.put(((SectionNode) node).getKey(), node);
127         }
128         Ejb[] ejbs = enterpriseBeans.getEjbs();
129         // Group beans according to their type and sort them according to their display name
130
Arrays.sort(ejbs, new Comparator JavaDoc() {
131             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
132                 Ejb ejb1 = (Ejb) o1;
133                 Ejb ejb2 = (Ejb) o2;
134                 int i = getType(ejb1) - getType(ejb2);
135                 return i != 0 ? i : Utils.getEjbDisplayName(ejb1).compareTo(Utils.getEjbDisplayName(ejb2));
136             }
137
138             private int getType(Ejb ejb) {
139                 return (ejb instanceof Session) ? 1 : (ejb instanceof Entity) ? 2 : 3;
140             }
141         });
142         boolean dirty = nodes.length != ejbs.length;
143         Node[] newNodes = new Node[ejbs.length];
144         for (int i = 0; i < ejbs.length; i++) {
145             Ejb ejb = ejbs[i];
146             SectionNode node = (SectionNode) nodeMap.get(ejb);
147             if (node == null) {
148                 node = createNode(ejb);
149                 dirty = true;
150             }
151             newNodes[i] = node;
152             if (!dirty) {
153                 dirty = ((SectionNode) nodes[i]).getKey() != node.getKey();
154             }
155         }
156         if (dirty) {
157             children.remove(nodes);
158             children.add(newNodes);
159             populateBoxPanel();
160         }
161     }
162
163 }
164
Popular Tags