KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > dbschema > nodes > TableChildren


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.dbschema.nodes;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.util.*;
25
26 import org.openide.nodes.Children;
27 import org.openide.nodes.Node;
28 import org.openide.util.WeakListeners;
29
30 import org.netbeans.modules.dbschema.*;
31
32 /** Normal implementation of children list for a table element node.
33  */

34 public class TableChildren extends Children.Keys {
35     /** Converts property names to filter. */
36     protected static HashMap propToFilter;
37
38     /** The table element whose subelements are represented. */
39     protected DBElement element;
40     /** Filter for elements, or <code>null</code> to disable. */
41     protected TableElementFilter filter;
42     /** Factory for creating new child nodes. */
43     protected DBElementNodeFactory factory;
44     /** Weak listener to the element and filter changes */
45     private PropertyChangeListener JavaDoc wPropL;
46     /** Listener to the element and filter changes. This reference must
47     * be kept to prevent the listener from finalizing when we are alive */

48     private DBElementListener propL;
49     /** Central memory of mankind is used when some elements are changed */
50     protected Collection[] cpl;
51     /** Flag saying whether we have our nodes initialized */
52     private boolean nodesInited = false;
53   
54     /** For sorting groups of elements. */
55     private static Comparator comparator = new Comparator () {
56         public int compare (Object JavaDoc o1, Object JavaDoc o2) {
57             if (o1 instanceof DBMemberElement)
58                 if (o2 instanceof DBMemberElement)
59                     return ((DBMemberElement)o1).getName().getName().compareToIgnoreCase(((DBMemberElement) o2).getName().getName());
60                 else
61                 return -1;
62             else
63                 if (o2 instanceof DBMemberElement)
64                     return 1;
65                 else
66                     return 0;
67         }
68     };
69
70     static {
71         propToFilter = new HashMap ();
72         propToFilter.put (DBElementProperties.PROP_TABLES, new Integer JavaDoc (TableElementFilter.TABLE | TableElementFilter.VIEW));
73         propToFilter.put (DBElementProperties.PROP_COLUMNS, new Integer JavaDoc (TableElementFilter.COLUMN));
74         propToFilter.put (DBElementProperties.PROP_COLUMN_PAIRS, new Integer JavaDoc (TableElementFilter.COLUMN_PAIR));
75         propToFilter.put (DBElementProperties.PROP_INDEXES, new Integer JavaDoc (TableElementFilter.INDEX));
76         propToFilter.put (DBElementProperties.PROP_KEYS, new Integer JavaDoc (TableElementFilter.FK));
77     }
78  
79     /** Create class children with the default factory.
80     * The children are initially unfiltered.
81     * @param element attached class element (non-<code>null</code>)
82     */

83     public TableChildren (final DBElement element) {
84         this(DefaultDBFactory.READ_ONLY, element);
85     }
86
87     /** Create class children.
88     * The children are initially unfiltered.
89     * @param factory the factory to use to create new children
90     * @param element attached class element (non-<code>null</code>)
91     */

92     public TableChildren (final DBElementNodeFactory factory, final DBElement element) {
93         super();
94         this.element = element;
95         this.factory = factory;
96         this.filter = null;
97     }
98   
99     /********** Implementation of filter cookie **********/
100
101     /* @return The class of currently asociated filter or null
102     * if no filter is asociated with these children.
103     */

104     public Class JavaDoc getFilterClass () {
105         return TableElementFilter.class;
106     }
107
108     /* @return The filter currently asociated with these children
109     */

110     public Object JavaDoc getFilter () {
111         return filter;
112     }
113
114     /* Sets new filter for these children.
115     * @param filter New filter. Null == disable filtering.
116     */

117     public void setFilter (final Object JavaDoc filter) {
118         if (!(filter instanceof TableElementFilter))
119             throw new IllegalArgumentException JavaDoc();
120
121         this.filter = (TableElementFilter)filter;
122         // change element nodes according to the new filter
123
if (nodesInited)
124             refreshAllKeys ();
125     }
126
127     // Children implementation ..............................................................
128

129     /* Overrides initNodes to run the preparation task of the
130     * source element, call refreshKeys and start to
131     * listen to the changes in the element too. */

132     protected void addNotify () {
133         refreshAllKeys ();
134         // listen to the changes in the class element
135
if (wPropL == null) {
136             propL = new DBElementListener();
137             wPropL = WeakListeners.propertyChange (propL, element);
138         }
139         
140         element.addPropertyChangeListener (wPropL);
141         nodesInited = true;
142     }
143
144     protected void removeNotify () {
145         setKeys (java.util.Collections.EMPTY_SET);
146         nodesInited = false;
147     }
148   
149     /* Creates node for given key.
150      * The node is created using node factory.
151      */

152     protected Node[] createNodes (final Object JavaDoc key) {
153         if (key instanceof ColumnElement)
154             return new Node[] {factory.createColumnNode((ColumnElement) key)};
155             
156         if (key instanceof ColumnPairElement)
157             return new Node[] {factory.createColumnPairNode((ColumnPairElement) key)};
158
159         if (element instanceof TableElement) {
160             boolean viewSupport = isViewSupport((TableElement) element);
161             if (((TableElement) element).isTableOrView() || viewSupport) {
162 // if (((TableElement) element).isTableOrView()) {
163
if (key instanceof IndexElement)
164                     return new Node[] { factory.createIndexNode((IndexElement)key) };
165                 if (key instanceof ForeignKeyElement)
166                     return new Node[] { factory.createForeignKeyNode((ForeignKeyElement)key) };
167             }
168         }
169             
170         // ?? unknown type
171
return new Node[0];
172     }
173
174
175     /************** utility methods ************/
176
177     /** Updates all the keys (elements) according to the current filter &
178     * ordering.
179     */

180     protected void refreshAllKeys () {
181         cpl = new Collection [getOrder ().length];
182         refreshKeys (TableElementFilter.ALL);
183     }
184
185     /** Updates all the keys with given filter.
186     */

187     protected void refreshKeys (int filter) {
188         int[] order = getOrder ();
189         LinkedList keys = new LinkedList();
190         
191         // build ordered and filtered keys for the subelements
192
for (int i = 0; i < order.length; i++)
193             if (((order[i] & filter) != 0) || (cpl [i] == null))
194                 keys.addAll(cpl [i] = getKeysOfType(order[i]));
195             else
196                 keys.addAll(cpl [i]);
197         
198         // set new keys
199
setKeys(keys);
200     }
201
202     /** Filters and returns the keys of specified type.
203      */

204     protected Collection getKeysOfType (final int elementType) {
205         LinkedList keys = new LinkedList();
206
207         ColumnElement[] columns = null;
208         ColumnPairElement[] pairs = null;
209         if (element instanceof TableElement)
210             columns = ((TableElement) element).getColumns();
211         if (element instanceof IndexElement)
212             columns = ((IndexElement) element).getColumns();
213         if (element instanceof ForeignKeyElement)
214             pairs = ((ForeignKeyElement) element).getColumnPairs();
215         
216         if ((elementType & TableElementFilter.COLUMN) != 0)
217             if (columns != null)
218                 filterModifiers(columns, keys);
219             else
220                 if (pairs != null)
221                     filterModifiers(pairs, keys);
222
223         if (element instanceof TableElement) {
224
225             boolean viewSupport = isViewSupport (((TableElement) element));
226
227                 
228             if (((TableElement) element).isTableOrView() || viewSupport) {
229 // if (((TableElement) element).isTableOrView()) {
230
if ((elementType & TableElementFilter.INDEX) != 0)
231                     filterModifiers(((TableElement) element).getIndexes(), keys);
232                 if ((elementType & TableElementFilter.FK) != 0)
233                     filterModifiers(((TableElement) element).getKeys(), keys);
234             }
235         }
236         
237         if ((filter == null) || filter.isSorted ())
238             Collections.sort(keys, comparator);
239
240         return keys;
241     }
242     
243     /* check is view are suppported for this table
244      * database name could be null see bug 53887
245      **/

246     private boolean isViewSupport( TableElement element){
247             String JavaDoc db = element.getDeclaringSchema().getDatabaseProductName();
248             boolean viewSupport =false;
249             if (db!=null){
250                 db = db.toLowerCase();
251                 viewSupport = (db.indexOf("oracle") != -1 || db.indexOf("microsoft sql server") != -1) ? true : false;
252             }
253             return viewSupport;
254     }
255   
256     /** Filters MemberElements for modifiers, and adds them to the given collection.
257     */

258     private void filterModifiers (DBMemberElement[] elements, Collection keys) {
259         int i, k = elements.length;
260         for (i = 0; i < k; i ++)
261             keys.add (elements [i]);
262     }
263
264     /** Returns order form filter.
265     */

266     protected int[] getOrder () {
267         return (filter == null || (filter.getOrder() == null)) ? TableElementFilter.DEFAULT_ORDER : filter.getOrder();
268     }
269
270     // innerclasses ...........................................................................
271

272     /** The listener for listening to the property changes in the filter.
273     */

274     private final class DBElementListener implements PropertyChangeListener JavaDoc {
275         public DBElementListener () {}
276         
277         /** This method is called when the change of properties occurs in the element.
278         * PENDING - (for Hanz - should be implemented better, change only the
279         * keys which belong to the changed property).
280         * -> YES MY LORD! ANOTHER WISH?
281         */

282         public void propertyChange (PropertyChangeEvent JavaDoc evt) {
283             Integer JavaDoc i = (Integer JavaDoc) propToFilter.get (evt.getPropertyName ());
284             if (i != null)
285                 refreshKeys(i.intValue());
286         }
287     } // end of ElementListener inner class*/
288
}
289
Popular Tags