KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjar > project > ui > EjbContainerChildren


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.ejbjar.project.ui;
21
22 import org.netbeans.api.java.classpath.ClassPath;
23 import org.netbeans.modules.j2ee.dd.api.common.VersionNotSupportedException;
24 import org.netbeans.modules.j2ee.dd.api.ejb.Ejb;
25 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar;
26 import org.netbeans.modules.j2ee.dd.api.ejb.EnterpriseBeans;
27 import org.netbeans.modules.j2ee.dd.api.ejb.Entity;
28 import org.netbeans.modules.j2ee.dd.api.ejb.MessageDriven;
29 import org.netbeans.modules.j2ee.dd.api.ejb.Session;
30 import org.netbeans.modules.j2ee.spi.ejbjar.EjbNodesFactory;
31 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
32 import org.openide.filesystems.FileObject;
33 import org.openide.nodes.Children;
34 import org.openide.nodes.Node;
35
36 import java.beans.PropertyChangeEvent JavaDoc;
37 import java.beans.PropertyChangeListener JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.Arrays JavaDoc;
40 import java.util.Collections JavaDoc;
41 import java.util.Comparator JavaDoc;
42 import java.util.List JavaDoc;
43 import javax.swing.SwingUtilities JavaDoc;
44
45 /**
46  * Ejbs contained within a module
47  * @author Chris Webster
48  */

49 public class EjbContainerChildren extends Children.Keys<Ejb> implements PropertyChangeListener JavaDoc {
50
51     private final EjbJar model;
52     private final ClassPath srcPath;
53     private final FileObject ddFile;
54     private final EjbNodesFactory nodeFactory;
55
56     public EjbContainerChildren(EjbJar model, ClassPath srcPath, FileObject ddFile, EjbNodesFactory nodeFactory) {
57         this.model = model;
58         //this.srcPath = srcPath;
59
// method controllers need full classpath to handle beans correctly
60
ClassPath bootClassPath = ClassPath.getClassPath(srcPath.getRoots()[0], ClassPath.BOOT);
61         this.srcPath = ClassPathSupport.createProxyClassPath(new ClassPath[]{srcPath, bootClassPath});
62         this.ddFile = ddFile;
63         this.nodeFactory = nodeFactory;
64     }
65
66     protected void addNotify() {
67         super.addNotify();
68         updateKeys();
69         model.addPropertyChangeListener(this);
70     }
71
72     private void updateKeys() {
73         EnterpriseBeans beans = model.getEnterpriseBeans();
74         List JavaDoc<Ejb> keys = Collections.<Ejb>emptyList();
75         if (beans != null) {
76             Session[] sessionBeans = beans.getSession();
77             Entity[] entityBeans = beans.getEntity();
78             MessageDriven[] messageBeans = beans.getMessageDriven();
79             Comparator JavaDoc<Ejb> ejbComparator = new Comparator JavaDoc<Ejb>() {
80                 public int compare(Ejb ejb1, Ejb ejb2) {
81                     return getEjbDisplayName(ejb1).compareTo(getEjbDisplayName(ejb2));
82                 }
83
84                 private String JavaDoc getEjbDisplayName(Ejb ejb) {
85                     String JavaDoc name = ejb.getDefaultDisplayName();
86                     if (name == null) {
87                         name = ejb.getEjbName();
88                     }
89                     if (name == null) {
90                         name = "";
91                     }
92                     return name;
93                 }
94             };
95             Arrays.sort(sessionBeans, ejbComparator);
96             Arrays.sort(entityBeans, ejbComparator);
97             Arrays.sort(messageBeans, ejbComparator);
98             keys = new ArrayList JavaDoc<Ejb>(sessionBeans.length + entityBeans.length + messageBeans.length);
99             addKeyValues(keys, Arrays.asList(sessionBeans));
100             addKeyValues(keys, Arrays.asList(messageBeans));
101             addKeyValues(keys, Arrays.asList(entityBeans));
102         }
103         setKeys(keys);
104     }
105
106     protected void removeNotify() {
107         model.removePropertyChangeListener(this);
108         setKeys(Collections.<Ejb>emptyList());
109         super.removeNotify();
110     }
111
112     protected Node[] createNodes(Ejb key) {
113         Node[] node = null;
114         if (key instanceof Session) {
115             // do not create node for web service
116
Session s = (Session) key;
117             boolean isWebService = false;
118             try {
119                 isWebService = s.getServiceEndpoint() != null;
120             } catch (VersionNotSupportedException vnse) {
121                 // J2EE 1.3 web services are not directly suppored
122
}
123             if (!isWebService && nodeFactory != null) {
124                 node = new Node[] { nodeFactory.createSessionNode(s, model, srcPath)};
125             }
126         }
127         if (key instanceof Entity && nodeFactory != null) {
128             node = new Node[] { nodeFactory.createEntityNode((Entity)key, model, srcPath, ddFile)};
129         }
130         if (key instanceof MessageDriven && nodeFactory != null) {
131             node = new Node[] { nodeFactory.createMessageNode((MessageDriven) key, model, srcPath)};
132         }
133         return node == null ? new Node[0] : node;
134     }
135
136     public void propertyChange(PropertyChangeEvent JavaDoc pce) {
137         SwingUtilities.invokeLater(new Runnable JavaDoc() {
138             public void run() {
139                 updateKeys();
140             }
141         });
142     }
143
144     private void addKeyValues(List JavaDoc<Ejb> keyContainer, List JavaDoc<? extends Ejb> beans) {
145         keyContainer.addAll(beans);
146     }
147 }
148
Popular Tags