KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > gui > JDBCTreeModel


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/gui/JDBCTreeModel.java#4 $
3 // This software is subject to the terms of the Common Public License
4 // Agreement, available at the following URL:
5 // http://www.opensource.org/licenses/cpl.html.
6 // Copyright (C) 2002-2007 Julian Hyde and others
7 // All Rights Reserved.
8 // You must accept the terms of that agreement to use this software.
9 */

10 package mondrian.gui;
11
12 import javax.swing.event.TreeModelListener JavaDoc;
13 import javax.swing.tree.TreePath JavaDoc;
14 import java.sql.Connection JavaDoc;
15 import java.sql.DatabaseMetaData JavaDoc;
16 import java.sql.ResultSet JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Vector JavaDoc;
20
21 /**
22  *
23  * @author sean
24  * @version $Id: //open/mondrian/src/main/mondrian/gui/JDBCTreeModel.java#4 $
25  */

26 public class JDBCTreeModel implements javax.swing.tree.TreeModel JavaDoc {
27     private Vector JavaDoc treeModelListeners = new Vector JavaDoc();
28     Connection JavaDoc connection;
29     DatabaseMetaData JavaDoc metadata;
30     List JavaDoc catalogs;
31     Node root;
32
33     /** Creates a new instance of JDBCTreeModel */
34     public JDBCTreeModel(Connection JavaDoc c) {
35         connection = c;
36         try {
37             metadata = connection.getMetaData();
38             catalogs = new ArrayList JavaDoc();
39             String JavaDoc catalogName = connection.getCatalog();
40             Node cat = new Node(catalogName, Node.CATALOG);
41
42             ResultSet JavaDoc trs = metadata.getTables(cat.name, null, null, null);
43             while (trs.next()) {
44                 Node table = new Node(trs.getString(3), Node.TABLE);
45                 cat.children.add(table);
46                 //get the tables for each catalog.
47
ResultSet JavaDoc crs = metadata.getColumns(cat.name, null, table.name, null);
48                 while (crs.next()) {
49                     Node column = new Node(crs.getString(4), Node.COLUMN);
50                     table.children.add(column);
51                 }
52             }
53             root = cat;
54
55         } catch (Exception JavaDoc ex) {
56             ex.printStackTrace();
57         }
58     }
59
60
61     /** Adds a listener for the <code>TreeModelEvent</code>
62      * posted after the tree changes.
63      *
64      * @param l the listener to add
65      * @see #removeTreeModelListener
66      *
67      */

68     public void addTreeModelListener(TreeModelListener JavaDoc l) {
69         treeModelListeners.add(l);
70     }
71
72     /** Returns the child of <code>parent</code> at index <code>index</code>
73      * in the parent's
74      * child array. <code>parent</code> must be a node previously obtained
75      * from this data source. This should not return <code>null</code>
76      * if <code>index</code>
77      * is a valid index for <code>parent</code> (that is <code>index >= 0 &&
78      * index < getChildCount(parent</code>)).
79      *
80      * @param parent a node in the tree, obtained from this data source
81      * @return the child of <code>parent</code> at index <code>index</code>
82      *
83      */

84     public Object JavaDoc getChild(Object JavaDoc parent, int index) {
85         if (parent instanceof Node) {
86             return ((Node)parent).children.get(index);
87         }
88
89         return null;
90     }
91
92     /** Returns the number of children of <code>parent</code>.
93      * Returns 0 if the node
94      * is a leaf or if it has no children. <code>parent</code> must be a node
95      * previously obtained from this data source.
96      *
97      * @param parent a node in the tree, obtained from this data source
98      * @return the number of children of the node <code>parent</code>
99      *
100      */

101     public int getChildCount(Object JavaDoc parent) {
102         if (parent instanceof Node) {
103             return ((Node)parent).children.size();
104         }
105         return 0;
106     }
107
108     /** Returns the index of child in parent. If <code>parent</code>
109      * is <code>null</code> or <code>child</code> is <code>null</code>,
110      * returns -1.
111      *
112      * @param parent a note in the tree, obtained from this data source
113      * @param child the node we are interested in
114      * @return the index of the child in the parent, or -1 if either
115      * <code>child</code> or <code>parent</code> are <code>null</code>
116      *
117      */

118     public int getIndexOfChild(Object JavaDoc parent, Object JavaDoc child) {
119        if (parent instanceof Node) {
120             return ((Node)parent).children.indexOf(child);
121        }
122
123        return -1;
124     }
125
126     /** Returns the root of the tree. Returns <code>null</code>
127      * only if the tree has no nodes.
128      *
129      * @return the root of the tree
130      *
131      */

132     public Object JavaDoc getRoot() {
133         return root;
134     }
135
136     /** Returns <code>true</code> if <code>node</code> is a leaf.
137      * It is possible for this method to return <code>false</code>
138      * even if <code>node</code> has no children.
139      * A directory in a filesystem, for example,
140      * may contain no files; the node representing
141      * the directory is not a leaf, but it also has no children.
142      *
143      * @param node a node in the tree, obtained from this data source
144      * @return true if <code>node</code> is a leaf
145      *
146      */

147     public boolean isLeaf(Object JavaDoc node) {
148         return getChildCount(node) == 0;
149     }
150
151     /** Removes a listener previously added with
152      * <code>addTreeModelListener</code>.
153      *
154      * @see #addTreeModelListener
155      * @param l the listener to remove
156      *
157      */

158     public void removeTreeModelListener(TreeModelListener JavaDoc l) {
159         treeModelListeners.remove(l);
160     }
161
162     /** Messaged when the user has altered the value for the item identified
163      * by <code>path</code> to <code>newValue</code>.
164      * If <code>newValue</code> signifies a truly new value
165      * the model should post a <code>treeNodesChanged</code> event.
166      *
167      * @param path path to the node that the user has altered
168      * @param newValue the new value from the TreeCellEditor
169      *
170      */

171     public void valueForPathChanged(TreePath JavaDoc path, Object JavaDoc newValue) {
172     }
173
174     class Node {
175         static final int CATALOG = 0;
176         static final int TABLE = 1;
177         static final int COLUMN = 2;
178         String JavaDoc name;
179         int type;
180         ArrayList JavaDoc children;
181
182         public Node(String JavaDoc n, int t) {
183             name = n;
184             type = t;
185             children = new ArrayList JavaDoc();
186         }
187
188         public String JavaDoc toString() {
189             return name;
190         }
191     }
192 }
193
194 // End JDBCTreeModel.java
195
Popular Tags