1 19 20 package org.netbeans.modules.db.explorer; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.beans.PropertyChangeSupport ; 25 import java.text.MessageFormat ; 26 import java.util.Collection ; 27 import java.util.Comparator ; 28 import java.util.ResourceBundle ; 29 import java.util.TreeSet ; 30 import java.util.Vector ; 31 import javax.swing.SwingUtilities ; 32 import org.openide.ErrorManager; 33 34 import org.openide.nodes.AbstractNode; 35 import org.openide.nodes.Node; 36 import org.openide.nodes.Children; 37 import org.openide.util.NbBundle; 38 import org.openide.util.RequestProcessor; 39 40 import org.netbeans.api.db.explorer.DatabaseException; 41 import org.netbeans.modules.db.explorer.infos.DatabaseNodeInfo; 42 import org.netbeans.modules.db.explorer.infos.ProcedureNodeInfo; 43 import org.netbeans.modules.db.explorer.infos.TableNodeInfo; 44 import org.netbeans.modules.db.explorer.infos.ViewNodeInfo; 45 import org.netbeans.modules.db.explorer.nodes.DatabaseNode; 46 import org.openide.DialogDisplayer; 47 import org.openide.NotifyDescriptor; 48 49 51 55 58 public class DatabaseNodeChildren extends Children.Array { 59 60 private ResourceBundle bundle = NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle"); 62 private TreeSet children; 63 private transient PropertyChangeSupport propertySupport = new PropertyChangeSupport (this); 64 private static Object sync = new Object (); 66 private PropertyChangeListener listener = new PropertyChangeListener () { 67 public void propertyChange(PropertyChangeEvent event) { 68 if (event.getPropertyName().equals("finished")) { MUTEX.writeAccess(new Runnable () { 70 public void run() { 71 remove(getNodes()); nodes = getCh(); refresh(); } 75 }); 76 removeListener(); 77 } 78 } 79 }; 80 81 protected Collection initCollection() { 82 propertySupport.addPropertyChangeListener(listener); 83 84 RequestProcessor.getDefault().post(new Runnable () { 85 public void run () { 86 DatabaseNodeInfo nodeinfo = ((DatabaseNode)getNode()).getInfo(); 87 java.util.Map nodeord = (java.util.Map )nodeinfo.get(DatabaseNodeInfo.CHILDREN_ORDERING); 88 boolean sort = (nodeinfo.getName().equals("Drivers") || (nodeinfo instanceof TableNodeInfo) || (nodeinfo instanceof ViewNodeInfo) || (nodeinfo instanceof ProcedureNodeInfo)) ? false : true; TreeSet children = new TreeSet (new NodeComparator(nodeord, sort)); 90 91 try { 92 Vector chlist; 93 synchronized (sync) { 94 chlist = nodeinfo.getChildren(); 95 } 96 97 for (int i=0;i<chlist.size();i++) { 98 Node snode = null; 99 Object sinfo = chlist.elementAt(i); 100 101 if (sinfo instanceof DatabaseNodeInfo) { 102 DatabaseNodeInfo dni = (DatabaseNodeInfo) sinfo; 103 104 snode = createNode(dni); 106 107 } 108 else 109 if (sinfo instanceof Node) 110 snode = (Node)sinfo; 111 if (snode != null) 112 children.add(snode); 113 } 114 115 } catch (Exception e) { 130 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 131 showException(e); 132 children.clear(); 133 } 134 135 setCh(children); 136 137 propertySupport.firePropertyChange("finished", null, null); } 139 }, 0); 140 141 TreeSet ts = new TreeSet (); 142 ts.add(createWaitNode()); 143 return ts; 144 } 145 146 public boolean getChildrenInitialized() { 147 return isInitialized(); 148 } 149 150 155 private Node createWaitNode () { 156 AbstractNode n = new AbstractNode(Children.LEAF); 157 n.setName(bundle.getString("WaitNode")); n.setIconBase("org/netbeans/modules/db/resources/wait"); return n; 160 } 161 162 private TreeSet getCh() { 163 return children; 164 } 165 166 private void setCh(TreeSet children) { 167 this.children = children; 168 } 169 170 private void removeListener() { 171 propertySupport.removePropertyChangeListener(listener); 172 } 173 174 class NodeComparator implements Comparator { 175 private java.util.Map map = null; 176 private boolean sort; 177 178 public NodeComparator(java.util.Map map, boolean sort) { 179 this.map = map; 180 this.sort = sort; 181 } 182 183 public int compare(Object o1, Object o2) { 184 if (! sort) 185 return 1; 186 187 if (!(o1 instanceof DatabaseNode)) 188 return -1; 189 if (!(o2 instanceof DatabaseNode)) 190 return 1; 191 192 int o1val, o2val, diff; 193 Integer o1i = (Integer )map.get(o1.getClass().getName()); 194 if (o1i != null) 195 o1val = o1i.intValue(); 196 else 197 o1val = Integer.MAX_VALUE; 198 Integer o2i = (Integer )map.get(o2.getClass().getName()); 199 if (o2i != null) 200 o2val = o2i.intValue(); 201 else 202 o2val = Integer.MAX_VALUE; 203 204 diff = o1val-o2val; 205 if (diff == 0) 206 return ((DatabaseNode)o1).getInfo().getName().compareTo(((DatabaseNode)o2).getInfo().getName()); 207 return diff; 208 } 209 } 210 211 public DatabaseNode createNode(DatabaseNodeInfo info) { 212 String nclass = (String )info.get(DatabaseNodeInfo.CLASS); 213 DatabaseNode node = null; 214 215 try { 216 node = (DatabaseNode)Class.forName(nclass).newInstance(); 217 node.setInfo(info); 218 node.getInfo().setNode(node); 219 } catch (Exception e) { 220 showException(e); 221 } 222 223 return node; 224 } 225 226 public DatabaseNode createSubnode(DatabaseNodeInfo info, boolean addToChildrenFlag) throws DatabaseException { 227 DatabaseNode subnode = createNode(info); 228 if (subnode != null && addToChildrenFlag) { 229 DatabaseNodeInfo ninfo = ((DatabaseNode)getNode()).getInfo(); 230 ninfo.getChildren().add(info); 231 232 235 if (isInitialized()) 236 add(new Node[] {subnode}); 237 } 238 239 return subnode; 240 } 241 242 private void showException(final Exception e) { 243 SwingUtilities.invokeLater(new Runnable () { 244 public void run() { 245 ResourceBundle bundle = NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle"); String format = bundle.getString("EXC_ConnectionError"); String message = bundle.getString("ReadStructureErrorPrefix") + " " + MessageFormat.format(format, new String [] {e.getMessage()}); DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE)); 249 } 250 }); 251 } 252 } 253 | Popular Tags |