KickJava   Java API By Example, From Geeks To Geeks.

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


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.cookies.FilterCookie;
29
import org.openide.util.WeakListeners;
30
31 import org.netbeans.modules.dbschema.*;
32
33 /** Normal implementation of children list for a table element node.
34  */

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

49     private DBElementListener propL;
50     /** Central memory of mankind is used when some elements are changed */
51     protected Collection[] cpl;
52     /** Flag saying whether we have our nodes initialized */
53     private boolean nodesInited = false;
54   
55     static {
56         propToFilter = new HashMap ();
57         propToFilter.put (DBElementProperties.PROP_TABLES, new Integer JavaDoc (TableElementFilter.TABLE));
58         propToFilter.put (DBElementProperties.PROP_COLUMNS, new Integer JavaDoc (TableElementFilter.COLUMN));
59         propToFilter.put (DBElementProperties.PROP_INDEXES, new Integer JavaDoc (TableElementFilter.INDEX));
60         propToFilter.put (DBElementProperties.PROP_KEYS, new Integer JavaDoc (TableElementFilter.FK));
61     }
62     
63     /** Create class children with the default factory.
64     * The children are initially unfiltered.
65     * @param element attached class element (non-<code>null</code>)
66     */

67     public SchemaChildren (final SchemaElement element) {
68         this(DefaultDBFactory.READ_ONLY, element);
69     }
70
71     /** Create class children.
72     * The children are initially unfiltered.
73     * @param factory the factory to use to create new children
74     * @param element attached class element (non-<code>null</code>)
75     */

76     public SchemaChildren (final DBElementNodeFactory factory, final SchemaElement element) {
77         super();
78         this.element = element;
79         this.factory = factory;
80         this.filter = null;
81     }
82
83     /********** Implementation of filter cookie **********/
84
85     /* @return The class of currently asociated filter or null
86     * if no filter is asociated with these children.
87     */

88     public Class JavaDoc getFilterClass () {
89         return SchemaElementFilter.class;
90     }
91
92     /* @return The filter currently asociated with these children
93     */

94     public Object JavaDoc getFilter () {
95         return filter;
96     }
97
98     /* Sets new filter for these children.
99     * @param filter New filter. Null == disable filtering.
100     */

101     public void setFilter (final Object JavaDoc filter) {
102         if (!(filter instanceof SchemaElementFilter))
103             throw new IllegalArgumentException JavaDoc();
104
105         this.filter = (SchemaElementFilter)filter;
106         // change element nodes according to the new filter
107
if (nodesInited)
108             refreshAllKeys ();
109     }
110
111     // Children implementation ..............................................................
112

113     /* Overrides initNodes to run the preparation task of the
114     * source element, call refreshKeys and start to
115     * listen to the changes in the element too. */

116     protected void addNotify () {
117         refreshAllKeys ();
118         
119         // listen to the changes in the class element
120
if (wPropL == null) {
121             propL = new DBElementListener();
122             wPropL = WeakListeners.propertyChange (propL, element);
123         }
124         else {
125             // #55249 - need to recreate the listener with the right element
126
wPropL = WeakListeners.propertyChange (propL, element);
127         }
128         
129         element.addPropertyChangeListener (wPropL);
130         nodesInited = true;
131     }
132
133     protected void removeNotify () {
134         setKeys (java.util.Collections.EMPTY_SET);
135         nodesInited = false;
136     }
137
138     /* Creates node for given key.
139     * The node is created using node factory.
140     */

141     protected Node[] createNodes (final Object JavaDoc key) {
142         if (key instanceof TableElement)
143             return new Node[] { factory.createTableNode((TableElement)key) };
144             
145         // ?? unknown type
146
return new Node[0];
147     }
148
149
150     /************** utility methods ************/
151
152     /** Updates all the keys (elements) according to the current filter &
153     * ordering.
154     */

155     protected void refreshAllKeys () {
156         cpl = new Collection [getOrder ().length];
157
158         refreshKeys (SchemaElementFilter.TABLE);
159     }
160
161     /** Updates all the keys with given filter.
162     */

163     protected void refreshKeys (int filter) {
164         int[] order = getOrder ();
165         LinkedList keys = new LinkedList();
166         
167         // build ordered and filtered keys for the subelements
168
for (int i = 0; i < order.length; i++)
169             if (((order[i] & filter) != 0) || (cpl [i] == null))
170                 keys.addAll(cpl [i] = getKeysOfType(order[i]));
171             else
172                 keys.addAll(cpl [i]);
173         
174         // set new keys
175
setKeys(keys);
176     }
177
178     /** Filters and returns the keys of specified type.
179     */

180     protected Collection getKeysOfType (final int elementType) {
181         LinkedList keys = new LinkedList();
182
183         if ((elementType & SchemaElementFilter.TABLE) != 0)
184             filterModifiers(((SchemaElement)element).getTables(), keys);
185
186         return keys;
187     }
188
189     /** Filters MemberElements for modifiers, and adds them to the given collection.
190     */

191     private void filterModifiers (DBElement[] elements, Collection keys) {
192         int i, k = elements.length;
193         
194         for (i = 0; i < k; i ++)
195             keys.add (elements [i]);
196     }
197
198     /** Returns order form filter.
199     */

200     protected int[] getOrder () {
201         return (filter == null || (filter.getOrder() == null)) ? SchemaElementFilter.DEFAULT_ORDER : filter.getOrder();
202     }
203
204     // innerclasses ...........................................................................
205

206     /** The listener for listening to the property changes in the filter.
207     */

208     private final class DBElementListener implements PropertyChangeListener JavaDoc {
209         public DBElementListener () {}
210         
211         /** This method is called when the change of properties occurs in the element.
212         * PENDING - (for Hanz - should be implemented better, change only the
213         * keys which belong to the changed property).
214         * -> YES MY LORD! ANOTHER WISH?
215         */

216         public void propertyChange (PropertyChangeEvent JavaDoc evt) {
217             Integer JavaDoc i = (Integer JavaDoc) propToFilter.get (evt.getPropertyName ());
218             if (i != null)
219                 refreshKeys(i.intValue());
220         }
221     } // end of ElementListener inner class*/
222
}
223
Popular Tags