KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > tools > mapping > reversedb2 > dbmetatreemodel > ReverseDbTreeNode


1 /* Copyright 2002-2005 The Apache Software Foundation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */

15
16 package org.apache.ojb.tools.mapping.reversedb2.dbmetatreemodel;
17
18 import javax.swing.tree.TreeNode JavaDoc;
19 import org.apache.ojb.tools.mapping.reversedb2.propertyEditors.EditableTreeNodeWithProperties;
20
21
22 /**
23  * Abstract implementation of a treenode representing an metadata object
24  * in a database. It implements loading the children of the node in a
25  * separate thread, thus not blocking the user interface.
26  * @author Administrator
27  */

28 public abstract class ReverseDbTreeNode extends EditableTreeNodeWithProperties
29     implements java.io.Serializable JavaDoc
30 {
31     transient private java.sql.DatabaseMetaData JavaDoc dbMeta;
32     private DatabaseMetaDataTreeModel dbMetaTreeModel;
33     
34     /**
35      * List of children of this treenode.
36      */

37     protected java.util.ArrayList JavaDoc alChildren = new java.util.ArrayList JavaDoc();
38     
39     private boolean isFilled = false;
40     
41     transient private Object JavaDoc populationLock = new Object JavaDoc();
42     private boolean populationInProgress = false;
43     
44     private ReverseDbTreeNode parent = null;
45     
46     public ReverseDbTreeNode(
47         java.sql.DatabaseMetaData JavaDoc pdbMeta,
48         DatabaseMetaDataTreeModel pdbMetaTreeModel,
49         ReverseDbTreeNode pparent)
50     {
51         this.dbMeta = pdbMeta;
52         this.dbMetaTreeModel = pdbMetaTreeModel;
53         this.parent = pparent;
54         alChildren.add(new javax.swing.tree.DefaultMutableTreeNode JavaDoc("..."));
55     }
56         
57     /**
58      * @see TreeNode#getChildAt(int)
59      */

60     public TreeNode JavaDoc getChildAt(int index)
61     {
62         if (!this.isFilled) this.load(false, false, true);
63         return (TreeNode JavaDoc)this.alChildren.get(index);
64     }
65     
66     /**
67      * @see TreeNode#getChildCount()
68      */

69     public int getChildCount()
70     {
71         return this.alChildren.size();
72     }
73     
74     /**
75      * @see TreeNode#getParent()
76      */

77     public TreeNode JavaDoc getParent()
78     {
79         return this.parent;
80     }
81     
82     /**
83      * @see TreeNode#getIndex(TreeNode)
84      */

85     public int getIndex(TreeNode JavaDoc o)
86     {
87         return this.alChildren.indexOf(o);
88     }
89     
90     /**
91      * @see TreeNode#getAllowsChildren()
92      */

93     public abstract boolean getAllowsChildren();
94     
95     /**
96      * @see TreeNode#isLeaf()
97      */

98     public abstract boolean isLeaf();
99     
100     /**
101      * @see TreeNode#children()
102      */

103     public java.util.Enumeration JavaDoc children ()
104     {
105         if (!this.isFilled) this.load(false, false, true);
106         return java.util.Collections.enumeration(this.alChildren);
107     }
108     
109     /**
110      * Loads the children of this TreeNode. If another Thread is already active on this node the method returns
111      * without doing anything (if a separate Thread is started the method returns anyway, but the Thread might
112      * do nothing).
113      * @param recursive If true, all children down to the leaf node are retrieved
114      * @param replace If true the children are loaded unconditionally. If false the
115      * retrieval is only done if the node has not been populated before.
116      * @param inNewThread if true the load is done in a new thread.
117      */

118     public void load(final boolean recursive, final boolean replace, final boolean inNewThread)
119     {
120         if (inNewThread)
121         {
122             new Thread JavaDoc()
123             {
124                 public void run()
125                 {
126                     load(recursive, replace, false);
127                 }
128             }.start();
129             return;
130         }
131         if (!populationInProgress)
132         {
133             synchronized (this.populationLock)
134             {
135                 this.populationInProgress = true;
136                 if (replace || !this.isFilled)
137                 {
138                     this.isFilled = _load();
139                 }
140                 this.populationInProgress = false;
141             }
142             if (!recursive)
143                 this.getDbMetaTreeModel ().setStatusBarMessage ("Done");
144         }
145         if (recursive)
146         {
147             java.util.Enumeration JavaDoc e = this.children();
148             while (e.hasMoreElements())
149             {
150                 Object JavaDoc o = e.nextElement();
151                 if (o instanceof ReverseDbTreeNode)
152                     ((ReverseDbTreeNode) o).load(recursive, replace, false);
153             }
154             this.getDbMetaTreeModel ().setStatusBarMessage ("Done");
155         }
156     }
157     
158     /**
159      * Loads the children of this TreeNode. If the node is already populated, this method returns without action. If
160      * there is already a Thread populating this treenode the method waits until the other Thread has finished
161      * @param recursive if true, all children down to the leaf node are retrieved.
162      * @param replace if false and the list of children is already populated, return without action. If recursive
163      * is true, all children down to the leaf of the tree are checked.
164      */

165     public void loadWait(
166         final boolean recursive,
167         final boolean replace,
168         final boolean inNewThread)
169     {
170         if (inNewThread)
171         {
172             new Thread JavaDoc()
173             {
174                 public void run()
175                 {
176                     loadWait(recursive, replace, false);
177                 }
178             }.start();
179             return;
180         }
181         
182         synchronized (this.populationLock)
183         {
184             this.populationInProgress = true;
185             if (replace || !this.isFilled)
186             {
187                 this.isFilled = _load();
188             }
189             if (recursive)
190             {
191                 java.util.Enumeration JavaDoc e = this.children();
192                 while (e.hasMoreElements())
193                 {
194                     Object JavaDoc o = e.nextElement();
195                     if (o instanceof ReverseDbTreeNode)
196                         ((ReverseDbTreeNode)o).loadWait(recursive, replace, false);
197                 }
198             }
199             this.populationInProgress = false;
200         }
201         this.getDbMetaTreeModel ().setStatusBarMessage("Done");
202     }
203     
204     /**
205      * Access method for the DatabaseMetaData object of this tree model
206      */

207     protected java.sql.DatabaseMetaData JavaDoc getDbMeta()
208     {
209         return dbMeta;
210     }
211     
212     /**
213      * Access method for the TreeModel this node is associated to.
214      */

215     protected DatabaseMetaDataTreeModel getDbMetaTreeModel()
216     {
217         return dbMetaTreeModel;
218     }
219     
220     /**
221      * Purpose of this method is to fill the children of the node. It should
222      * replace all children in alChildren (the arraylist containing the children)
223      * of this node and notify the TreeModel that a change has occurred.
224      */

225     protected abstract boolean _load();
226     
227 }
228
Popular Tags