1 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 ; 37 import java.beans.PropertyChangeListener ; 38 import java.util.ArrayList ; 39 import java.util.Arrays ; 40 import java.util.Collections ; 41 import java.util.Comparator ; 42 import java.util.List ; 43 import javax.swing.SwingUtilities ; 44 45 49 public class EjbContainerChildren extends Children.Keys<Ejb> implements PropertyChangeListener { 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 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 <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 <Ejb> ejbComparator = new Comparator <Ejb>() { 80 public int compare(Ejb ejb1, Ejb ejb2) { 81 return getEjbDisplayName(ejb1).compareTo(getEjbDisplayName(ejb2)); 82 } 83 84 private String getEjbDisplayName(Ejb ejb) { 85 String 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 <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 Session s = (Session) key; 117 boolean isWebService = false; 118 try { 119 isWebService = s.getServiceEndpoint() != null; 120 } catch (VersionNotSupportedException vnse) { 121 } 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 pce) { 137 SwingUtilities.invokeLater(new Runnable () { 138 public void run() { 139 updateKeys(); 140 } 141 }); 142 } 143 144 private void addKeyValues(List <Ejb> keyContainer, List <? extends Ejb> beans) { 145 keyContainer.addAll(beans); 146 } 147 } 148 | Popular Tags |