1 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 ; 35 import java.util.Comparator ; 36 import java.util.HashMap ; 37 import java.util.Map ; 38 39 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 } 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 source, String propertyName, Object oldValue, Object 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 () { 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 nodeMap = new HashMap (); 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 Arrays.sort(ejbs, new Comparator () { 131 public int compare(Object o1, Object 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 |