KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.openide.actions.*;
23 import org.openide.nodes.*;
24
25 import org.openide.util.HelpCtx;
26 import org.openide.util.NbBundle;
27 import org.openide.util.actions.SystemAction;
28
29 import org.netbeans.modules.dbschema.*;
30
31 /** The default implementation of the db nodes factory.
32 * Uses the standard node implementations in this package.
33 */

34 public class DefaultDBFactory implements DBElementNodeFactory, IconStrings {
35     public static final String JavaDoc WAIT =
36         "org/openide/src/resources/wait"; // NOI18N
37

38     public static final String JavaDoc ERROR =
39         "org/openide/src/resources/error"; // NOI18N
40

41     /** Default instance of the factory with read-write properties. */
42     public static final DefaultDBFactory READ_WRITE = new DefaultDBFactory(true);
43
44     /** Default instance of the factory with read-only properties. */
45     public static final DefaultDBFactory READ_ONLY = new DefaultDBFactory(false);
46
47     /** Should be the element nodes read-only or writeable
48      * (properties, clipboard operations,...)
49      */

50     private boolean _writeable;
51
52     /** Create a new factory.
53      * @param writeable <code>true</code> if the produced nodes
54      * should have writable properties
55      * @see DBElementNode#writeable
56      */

57     public DefaultDBFactory (boolean writeable) {
58         _writeable = writeable;
59     }
60
61     /* Test whether this factory produces writeable nodes.
62      * @return <code>true</code> if so
63      */

64     public boolean isWriteable() {
65         return _writeable;
66     }
67   
68     /* Returns the node asociated with specified element.
69      * @return DBElementNode
70      */

71     public Node createSchemaNode (final SchemaElement element) {
72         return new SchemaElementNode(element, createSchemaChildren(element), isWriteable());
73     }
74   
75     /** Create children for a schema node.
76      * Could be subclassed to customize, e.g., the ordering of children.
77      * The default implementation used {@link SchemaChildren}.
78      * @param element a schema element
79      * @return children for the schema element
80      */

81     protected Children createSchemaChildren (SchemaElement element) {
82         return createSchemaChildren(element, (isWriteable() ? READ_WRITE : READ_ONLY));
83     }
84
85     /** Create children for a schema node, with specified factory.
86      * The default implementation used {@link SchemaChildren}.
87      * @param element a schema element
88      * @param factory the factory which will be used to create children
89      * @return children for the schema element
90      */

91     final protected Children createSchemaChildren (SchemaElement element, DBElementNodeFactory factory) {
92         SchemaChildren children = new SchemaChildren(factory, element);
93         boolean writeable = isWriteable();
94         
95         return children;
96     }
97
98     /* Returns the node asociated with specified element.
99      * @return DBElementNode
100      */

101     public Node createColumnNode (final ColumnElement element) {
102         return new ColumnElementNode(element, isWriteable());
103     }
104     
105     /* Returns the node asociated with specified element.
106      * @return DBElementNode
107      */

108     public Node createColumnPairNode (final ColumnPairElement element) {
109         return new ColumnPairElementNode(element, isWriteable());
110     }
111
112     /** Make a node representing an index.
113      * @param element the index
114      * @return an index node instance
115      */

116     public Node createIndexNode (final IndexElement element) {
117         return new IndexElementNode(element, (TableChildren) createIndexChildren(element), isWriteable());
118     }
119     
120     /** Create children for an index node.
121      * Could be subclassed to customize, e.g., the ordering of children.
122      * The default implementation used {@link IndexChildren}.
123      * @param element a index element
124      * @return children for the index element
125      */

126     protected Children createIndexChildren (IndexElement element) {
127         return createIndexChildren(element, (isWriteable() ? READ_WRITE : READ_ONLY));
128     }
129
130     /** Create children for a index node, with specified factory.
131      * The default implementation used {@link IndexChildren}.
132      * @param element a index element
133      * @param factory the factory which will be used to create children
134      * @return children for the index element
135      */

136     final protected Children createIndexChildren (IndexElement element, DBElementNodeFactory factory) {
137         TableChildren children = new TableChildren(factory, element);
138         boolean writeable = isWriteable();
139
140         return children;
141     }
142
143
144     /** Make a node representing a foreign key.
145      * @param element the foreign key
146      * @return a foreign key node instance
147      */

148     public Node createForeignKeyNode (final ForeignKeyElement element) {
149         return new ForeignKeyElementNode(element, (TableChildren) createForeignKeyChildren(element), isWriteable());
150     }
151     
152     /** Create children for an index node.
153      * Could be subclassed to customize, e.g., the ordering of children.
154      * The default implementation used {@link IndexChildren}.
155      * @param element a index element
156      * @return children for the index element
157      */

158     protected Children createForeignKeyChildren(ForeignKeyElement element) {
159         return createForeignKeyChildren(element, (isWriteable() ? READ_WRITE : READ_ONLY));
160     }
161
162     /** Create children for a index node, with specified factory.
163      * The default implementation used {@link IndexChildren}.
164      * @param element a index element
165      * @param factory the factory which will be used to create children
166      * @return children for the index element
167      */

168     final protected Children createForeignKeyChildren (ForeignKeyElement element, DBElementNodeFactory factory) {
169         TableChildren children = new TableChildren(factory, element);
170         boolean writeable = isWriteable();
171
172         return children;
173     }
174
175     /* Returns the node asociated with specified element.
176      * @return DBElementNode
177      */

178     public Node createTableNode (TableElement element) {
179         return new TableElementNode(element, createTableChildren(element), isWriteable());
180     }
181
182     /** Create children for a table node.
183      * Could be subclassed to customize, e.g., the ordering of children.
184      * The default implementation used {@link TableChildren}.
185      * @param element a table element
186      * @return children for the table element
187      */

188     protected Children createTableChildren (TableElement element) {
189         return createTableChildren(element, (isWriteable() ? READ_WRITE : READ_ONLY));
190     }
191
192     /** Create children for a table node, with specified factory.
193      * The default implementation used {@link TableChildren}.
194      * @param element a table element
195      * @param factory the factory which will be used to create children
196      * @return children for the table element
197      */

198     final protected Children createTableChildren (TableElement element, DBElementNodeFactory factory) {
199         TableChildren children = new TableChildren(factory, element);
200         TableElementFilter filter = new TableElementFilter();
201         boolean writeable = isWriteable();
202         
203         filter.setOrder(new int[] {TableElementFilter.TABLE, TableElementFilter.VIEW});
204         children.setFilter(filter);
205                 String JavaDoc db = element.getDeclaringSchema().getDatabaseProductName();
206                 boolean viewSupport =false;
207                 if (db!=null){
208                     db = db.toLowerCase();
209                     viewSupport = (db.indexOf("oracle") != -1 || db.indexOf("microsoft sql server") != -1) ? true : false;
210                 }
211
212         if (((TableElement) element).isTableOrView() || viewSupport)
213 // if (element.isTableOrView())
214
children.add(new Node[] {
215                 new ElementCategoryNode(0, factory, element, writeable),
216                 new ElementCategoryNode(1, factory, element, writeable),
217                 new ElementCategoryNode(2, factory, element, writeable),
218             });
219         else
220             children.add(new Node[] {
221                 new ElementCategoryNode(0, factory, element, writeable),
222             });
223         
224         return children;
225     }
226   
227
228     /* Creates and returns the instance of the node
229      * representing the status 'WAIT' of the DataNode.
230      * It is used when it spent more time to create elements hierarchy.
231      * @return the wait node.
232      */

233         public Node createWaitNode() {
234             AbstractNode n = new AbstractNode(Children.LEAF);
235             n.setName(NbBundle.getMessage(DefaultDBFactory.class,"Wait"));
236             n.setIconBase(WAIT);
237             return n;
238         }
239         
240     /* Creates and returns the instance of the node
241      * representing the status 'ERROR' of the DataNode
242      * @return the error node.
243      */

244         public Node createErrorNode() {
245             AbstractNode n = new AbstractNode(Children.LEAF);
246             n.setName(NbBundle.getMessage(DefaultDBFactory.class,"Error")); // NO18N
247
n.setIconBase(ERROR);
248             return n;
249         }
250     /** Array of the actions of the category nodes. */
251     private static final SystemAction[] CATEGORY_ACTIONS = new SystemAction[] {
252         SystemAction.get(ToolsAction.class),
253     };
254
255     /** The names of the category nodes */
256     static final String JavaDoc[] NAMES = new String JavaDoc[] {
257         NbBundle.getMessage(DefaultDBFactory.class, "Columns"), //NOI18N
258
NbBundle.getMessage(DefaultDBFactory.class, "Indexes"), //NOI18N
259
NbBundle.getMessage(DefaultDBFactory.class, "FKs"), //NOI18N
260
NbBundle.getMessage(DefaultDBFactory.class, "Tables") //NOI18N
261
};
262
263     /** Filters under each category node */
264     static final int[][] FILTERS = new int[][] {
265         { TableElementFilter.COLUMN },
266         { TableElementFilter.INDEX },
267         { TableElementFilter.FK },
268         { SchemaElementFilter.TABLE },
269     };
270
271     /** Array of the icons used for category nodes */
272     static final String JavaDoc[] CATEGORY_ICONS = new String JavaDoc[] {
273         COLUMNS_CATEGORY, INDEXES_CATEGORY, FKS_CATEGORY, TABLE
274     };
275
276     /**
277      * Category node - represents one section under table element node -
278      * columns, indexes, fks.
279      */

280     static class ElementCategoryNode extends AbstractNode {
281         /** The table element for this node */
282         DBElement element;
283
284         /** The type of the category node - for new types. */
285         int newTypeIndex;
286
287         /** Create new element category node for the specific category.
288         * @param index The index of type (0=columns, 1=indexes, 2=fks)
289         * @param factory The factory which is passed down to the table children
290         * object
291         * @param element the table element for which this node is created
292         */

293         ElementCategoryNode (int index, DBElementNodeFactory factory, TableElement element, boolean writeable) {
294             this(index, new TableChildren(factory, element));
295             this.element = element;
296             newTypeIndex = writeable ? index : -1;
297             switch (index) {
298                 case 0: setName(NbBundle.getMessage(DefaultDBFactory.class, "Columns")); break; //NOI18N
299
case 1: setName(NbBundle.getMessage(DefaultDBFactory.class, "Indexes")); break; //NOI18N
300
case 2: setName(NbBundle.getMessage(DefaultDBFactory.class, "FKs")); break; //NOI18N
301
}
302         }
303     
304         ElementCategoryNode (int index, DBElementNodeFactory factory, SchemaElement element, boolean writeable) {
305             this(index, new SchemaChildren(factory, element));
306             this.element = element;
307             newTypeIndex = writeable ? index : -1;
308             setName(NbBundle.getMessage(DefaultDBFactory.class, "Tables")); //NOI18N
309
}
310
311         /** Create new element node.
312          * @param index The index of type (0=columns, 1=indexes, 2=fks)
313          * @param children the table children of this node
314          */

315         private ElementCategoryNode (int index, TableChildren children) {
316             super(children);
317             setDisplayName(NAMES[index]);
318             systemActions = CATEGORY_ACTIONS;
319             TableElementFilter filter = new TableElementFilter();
320             filter.setOrder(FILTERS[index]);
321             children.setFilter(filter);
322             setIconBase(CATEGORY_ICONS[index]);
323         }
324     
325         private ElementCategoryNode (int index, SchemaChildren children) {
326             super(children);
327             setDisplayName(NAMES[index]);
328             systemActions = CATEGORY_ACTIONS;
329             setIconBase(CATEGORY_ICONS[index]);
330         }
331
332         public HelpCtx getHelpCtx () {
333             return new HelpCtx("dbschema_ctxhelp_wizard"); //NOI18N
334
}
335     }
336 }
337
Popular Tags