001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.db.explorer.infos;
043:
044: import java.sql.*;
045: import java.util.*;
046: import java.util.logging.Level;
047: import java.util.logging.Logger;
048:
049: import org.openide.nodes.Node;
050:
051: import org.netbeans.lib.ddl.*;
052: import org.netbeans.api.db.explorer.DatabaseException;
053: import org.netbeans.lib.ddl.impl.*;
054: import org.netbeans.modules.db.explorer.DatabaseNodeChildren;
055: import org.netbeans.modules.db.explorer.infos.*;
056: import org.netbeans.modules.db.explorer.nodes.*;
057:
058: public class ForeignKeyNodeInfo extends TableNodeInfo {
059: static final long serialVersionUID = -8633867970381524742L;
060:
061: public void initChildren(Vector children) throws DatabaseException {
062: try {
063: String table = (String) get(DatabaseNode.TABLE);
064: String fk_name = (String) get(DatabaseNode.IMPORTED_KEY);
065: DriverSpecification drvSpec = getDriverSpecification();
066: drvSpec.getImportedKeys(table);
067: ResultSet rs = drvSpec.getResultSet();
068: if (rs != null) {
069: HashMap rset = new HashMap();
070: ColumnNodeInfo info;
071: while (rs.next()) {
072: rset = drvSpec.getRow();
073: if (rset.get(new Integer(8)) != null)
074: if (((String) rset.get(new Integer(12)))
075: .startsWith(fk_name)) {
076: info = (ColumnNodeInfo) DatabaseNodeInfo
077: .createNodeInfo(
078: this ,
079: DatabaseNode.FOREIGN_COLUMN,
080: rset);
081:
082: if (info != null) {
083: String tempTName = (String) rset
084: .get(new Integer(3));
085: tempTName = (tempTName == "") ? ""
086: : tempTName + "."; // NOI18N
087: info.setName(info.getName()
088: + " -> "
089: + tempTName
090: + ((String) rset
091: .get(new Integer(4)))); // NOI18N
092: children.add(info);
093: } else
094: throw new Exception(
095: bundle()
096: .getString(
097: "EXC_UnableToCreateForeignNodeInfo")); //NOI18N
098: }
099: rset.clear();
100: }
101: rs.close();
102: }
103: } catch (Exception e) {
104: throw new DatabaseException(e.getMessage());
105: }
106: }
107:
108: public void refreshChildren() throws DatabaseException {
109: // create list (infos)
110: Vector charr = new Vector();
111: put(DatabaseNodeInfo.CHILDREN, charr);
112: initChildren(charr);
113:
114: // create sub-tree (by infos)
115: try {
116:
117: Node[] subTreeNodes = new Node[charr.size()];
118:
119: // current sub-tree
120: DatabaseNodeChildren children = (DatabaseNodeChildren) getNode()
121: .getChildren();
122:
123: // remove current sub-tree
124: children.remove(children.getNodes());
125:
126: // build refreshed sub-tree
127: for (int i = 0; i < charr.size(); i++)
128: subTreeNodes[i] = children
129: .createNode((DatabaseNodeInfo) charr
130: .elementAt(i));
131:
132: // add built sub-tree
133: children.add(subTreeNodes);
134:
135: } catch (Exception ex) {
136: Logger.getLogger("global").log(Level.INFO, null, ex);
137: }
138: }
139:
140: }
|