KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > explorer > DatabaseNodeChildren


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.db.explorer;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.PropertyChangeSupport JavaDoc;
25 import java.text.MessageFormat JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Comparator JavaDoc;
28 import java.util.ResourceBundle JavaDoc;
29 import java.util.TreeSet JavaDoc;
30 import java.util.Vector JavaDoc;
31 import javax.swing.SwingUtilities JavaDoc;
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 //import org.openide.util.Mutex;
50

51 // XXX This entire class is junk. Should have a sensible data model independent of
52
// nodes and display it using Children.Keys (or Looks) and everything would be
53
// much easier. -jglick
54

55 // I totally agree. It was planed to redesign the module and base it on a data model
56
// unfortunately this project was cancelled. Radko
57

58 public class DatabaseNodeChildren extends Children.Array {
59
60     private ResourceBundle JavaDoc bundle = NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle"); //NOI18N
61

62     private TreeSet JavaDoc children;
63     private transient PropertyChangeSupport JavaDoc propertySupport = new PropertyChangeSupport JavaDoc(this);
64     private static Object JavaDoc sync = new Object JavaDoc(); // synchronizing object
65

66     private PropertyChangeListener JavaDoc listener = new PropertyChangeListener JavaDoc() {
67         public void propertyChange(PropertyChangeEvent JavaDoc event) {
68             if (event.getPropertyName().equals("finished")) { //NOI18N
69
MUTEX.writeAccess(new Runnable JavaDoc() {
70                     public void run() {
71                         remove(getNodes()); //remove wait node
72
nodes = getCh(); // change children ...
73
refresh(); // ... and refresh them
74
}
75                 });
76                 removeListener();
77             }
78         }
79     };
80
81     protected Collection JavaDoc initCollection() {
82         propertySupport.addPropertyChangeListener(listener);
83
84         RequestProcessor.getDefault().post(new Runnable JavaDoc() {
85             public void run () {
86                 DatabaseNodeInfo nodeinfo = ((DatabaseNode)getNode()).getInfo();
87                 java.util.Map JavaDoc nodeord = (java.util.Map JavaDoc)nodeinfo.get(DatabaseNodeInfo.CHILDREN_ORDERING);
88                 boolean sort = (nodeinfo.getName().equals("Drivers") || (nodeinfo instanceof TableNodeInfo) || (nodeinfo instanceof ViewNodeInfo) || (nodeinfo instanceof ProcedureNodeInfo)) ? false : true; //NOI18N
89
TreeSet JavaDoc children = new TreeSet JavaDoc(new NodeComparator(nodeord, sort));
90
91                 try {
92                     Vector JavaDoc 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 JavaDoc sinfo = chlist.elementAt(i);
100
101                         if (sinfo instanceof DatabaseNodeInfo) {
102                             DatabaseNodeInfo dni = (DatabaseNodeInfo) sinfo;
103
104                             // aware! in this method is clone of instance dni created
105
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 //commented out for 3.6 release, need to solve for next Studio release
116
// if (getNode() instanceof RootNode) {
117
// // open connection (after initCollection done)
118
// SwingUtilities.invokeLater(new Runnable() {
119
// public void run() {
120
// try {
121
// // add connection (if needed) and make the connection to SAMPLE database connected
122
// PointbasePlus.addOrConnectAccordingToOption();
123
// } catch(Exception ex) {
124
// org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex);
125
// }
126
// }
127
// });
128
// }
129
} catch (Exception JavaDoc 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); //NOI18N
138
}
139         }, 0);
140
141         TreeSet JavaDoc ts = new TreeSet JavaDoc();
142         ts.add(createWaitNode());
143         return ts;
144     }
145     
146     public boolean getChildrenInitialized() {
147         return isInitialized();
148     }
149
150     /* Creates and returns the instance of the node
151     * representing the status 'WAIT' of the node.
152     * It is used when it spent more time to create elements hierarchy.
153     * @return the wait node.
154     */

155     private Node createWaitNode () {
156         AbstractNode n = new AbstractNode(Children.LEAF);
157         n.setName(bundle.getString("WaitNode")); //NOI18N
158
n.setIconBase("org/netbeans/modules/db/resources/wait"); //NOI18N
159
return n;
160     }
161
162     private TreeSet JavaDoc getCh() {
163         return children;
164     }
165
166     private void setCh(TreeSet JavaDoc children) {
167         this.children = children;
168     }
169
170     private void removeListener() {
171         propertySupport.removePropertyChangeListener(listener);
172     }
173
174     class NodeComparator implements Comparator JavaDoc {
175         private java.util.Map JavaDoc map = null;
176         private boolean sort;
177
178         public NodeComparator(java.util.Map JavaDoc map, boolean sort) {
179             this.map = map;
180             this.sort = sort;
181         }
182
183         public int compare(Object JavaDoc o1, Object JavaDoc 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 JavaDoc o1i = (Integer JavaDoc)map.get(o1.getClass().getName());
194             if (o1i != null)
195                 o1val = o1i.intValue();
196             else
197                 o1val = Integer.MAX_VALUE;
198             Integer JavaDoc o2i = (Integer JavaDoc)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 JavaDoc nclass = (String JavaDoc)info.get(DatabaseNodeInfo.CLASS);
213         DatabaseNode node = null;
214
215         try {
216             node = (DatabaseNode)Class.forName(nclass).newInstance();
217             node.setInfo(info); /* makes a copy of info, use node.getInfo() to access it */
218             node.getInfo().setNode(node); /* this is a weak, be cool, baby ;) */
219         } catch (Exception JavaDoc 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             //workaround for issue #31617, children should be initialized if they are not
233
// getNodes();
234

235             if (isInitialized())
236                 add(new Node[] {subnode});
237         }
238
239         return subnode;
240     }
241     
242     private void showException(final Exception JavaDoc e) {
243         SwingUtilities.invokeLater(new Runnable JavaDoc() {
244             public void run() {
245                 ResourceBundle JavaDoc bundle = NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle"); //NOI18N
246
String JavaDoc format = bundle.getString("EXC_ConnectionError"); //NOI18N
247
String JavaDoc message = bundle.getString("ReadStructureErrorPrefix") + " " + MessageFormat.format(format, new String JavaDoc[] {e.getMessage()}); //NOI18N
248
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE));
249             }
250         });
251     }
252 }
253
Popular Tags