KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > explorer > infos > ForeignKeyNodeInfo


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.infos;
21
22 import java.sql.*;
23 import java.util.*;
24
25 import org.openide.nodes.Node;
26
27 import org.netbeans.lib.ddl.*;
28 import org.netbeans.api.db.explorer.DatabaseException;
29 import org.netbeans.lib.ddl.impl.*;
30 import org.netbeans.modules.db.explorer.DatabaseNodeChildren;
31 import org.netbeans.modules.db.explorer.infos.*;
32 import org.netbeans.modules.db.explorer.nodes.*;
33
34 public class ForeignKeyNodeInfo extends TableNodeInfo {
35     static final long serialVersionUID =-8633867970381524742L;
36
37     public void initChildren(Vector children) throws DatabaseException {
38         try {
39             String JavaDoc table = (String JavaDoc)get(DatabaseNode.TABLE);
40             String JavaDoc fk_name = (String JavaDoc)get(DatabaseNode.IMPORTED_KEY);
41             DriverSpecification drvSpec = getDriverSpecification();
42             drvSpec.getImportedKeys(table);
43             ResultSet rs = drvSpec.getResultSet();
44             if (rs != null) {
45                 HashMap rset = new HashMap();
46                 ColumnNodeInfo info;
47                 while (rs.next()) {
48                     rset = drvSpec.getRow();
49                     if (rset.get(new Integer JavaDoc(8)) != null)
50                         if (((String JavaDoc) rset.get(new Integer JavaDoc(12))).startsWith(fk_name)) {
51                             info = (ColumnNodeInfo)DatabaseNodeInfo.createNodeInfo(this, DatabaseNode.FOREIGN_COLUMN, rset);
52
53                             if (info != null) {
54                                 String JavaDoc tempTName = (String JavaDoc) rset.get(new Integer JavaDoc(3));
55                                 tempTName = (tempTName == "") ? "" : tempTName + "."; // NOI18N
56
info.setName(info.getName() + " -> " + tempTName + ((String JavaDoc) rset.get(new Integer JavaDoc(4)))); // NOI18N
57
children.add(info);
58                             } else
59                                 throw new Exception JavaDoc(bundle().getString("EXC_UnableToCreateForeignNodeInfo")); //NOI18N
60
}
61                     rset.clear();
62                 }
63                 rs.close();
64             }
65         } catch (Exception JavaDoc e) {
66             throw new DatabaseException(e.getMessage());
67         }
68     }
69
70     public void refreshChildren() throws DatabaseException
71     {
72         // create list (infos)
73
Vector charr = new Vector();
74         put(DatabaseNodeInfo.CHILDREN, charr);
75         initChildren(charr);
76         
77         // create sub-tree (by infos)
78
try {
79
80             Node[] subTreeNodes = new Node[charr.size()];
81
82             // current sub-tree
83
DatabaseNodeChildren children = (DatabaseNodeChildren)getNode().getChildren();
84
85             // remove current sub-tree
86
children.remove(children.getNodes());
87
88             // build refreshed sub-tree
89
for(int i=0; i<charr.size(); i++)
90                 subTreeNodes[i] = children.createNode((DatabaseNodeInfo)charr.elementAt(i));
91
92             // add built sub-tree
93
children.add(subTreeNodes);
94
95         } catch (Exception JavaDoc ex) {
96             org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex);
97         }
98     }
99
100 }
101
Popular Tags