KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > explorer > nodes > DatabaseNode


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.db.explorer.nodes;
21
22 import java.io.IOException JavaDoc;
23 import java.util.*;
24
25 import javax.swing.Action JavaDoc;
26
27 import org.openide.*;
28 import org.openide.nodes.*;
29 import org.openide.util.HelpCtx;
30 import org.openide.util.NbBundle;
31
32 import org.netbeans.modules.db.explorer.*;
33 import org.netbeans.modules.db.explorer.actions.*;
34 import org.netbeans.api.db.explorer.DatabaseException;
35 import org.netbeans.modules.db.explorer.infos.DatabaseNodeInfo;
36
37 public class DatabaseNode extends AbstractNode implements Node.Cookie {
38
39     /** Cookie */
40     protected DatabaseNodeInfo info;
41
42     /** Context menu flags */
43     private boolean writable = false;
44     private boolean cutflag = false, copyflag = false, delflag = false;
45
46     /** Properties */
47     public static final String JavaDoc ROOT = "root"; //NOI18N
48
public static final String JavaDoc DRIVER_LIST = "driverlist"; //NOI18N
49
public static final String JavaDoc DRIVER = "driver"; //NOI18N
50
public static final String JavaDoc CONNECTION = "connection"; //NOI18N
51
public static final String JavaDoc CATALOG = "catalog"; //NOI18N
52
public static final String JavaDoc TABLELIST = "tablelist"; //NOI18N
53
public static final String JavaDoc TABLE = "table"; //NOI18N
54
public static final String JavaDoc VIEW = "view"; //NOI18N
55
public static final String JavaDoc VIEWLIST = "viewlist"; //NOI18N
56
public static final String JavaDoc VIEWCOLUMN = "viewcolumn"; //NOI18N
57
public static final String JavaDoc INDEX = "index"; //NOI18N
58
public static final String JavaDoc COLUMN = "column"; //NOI18N
59
public static final String JavaDoc INDEXCOLUMN = "indexcolumn"; //NOI18N
60
public static final String JavaDoc PRIMARY_KEY = "pcolumn"; //NOI18N
61
public static final String JavaDoc INDEXED_COLUMN = "icolumn"; //NOI18N
62
public static final String JavaDoc FOREIGN_COLUMN = "fcolumn"; //NOI18N
63
public static final String JavaDoc FOREIGN_KEY = "fcolumn"; //NOI18N
64
public static final String JavaDoc EXPORTED_KEY = "ekey"; //NOI18N
65
public static final String JavaDoc IMPORTED_KEY = "fkey"; //NOI18N
66
public static final String JavaDoc PROCEDURE = "procedure"; //NOI18N
67
public static final String JavaDoc PROCEDURELIST = "procedurelist"; //NOI18N
68
public static final String JavaDoc PROCEDURE_COLUMN = "procedurecolumn"; //NOI18N
69

70     /** Constructor */
71     public DatabaseNode()
72     {
73         super(new DatabaseNodeChildren());
74     }
75
76     /** Constructor */
77     public DatabaseNode(Children child)
78     {
79         super(child);
80     }
81
82     /** Returns cookie */
83     public DatabaseNodeInfo getInfo()
84     {
85         return info;
86     }
87
88     /** Sets cookie */
89     public void setInfo(DatabaseNodeInfo nodeinfo)
90     {
91         info = (DatabaseNodeInfo)nodeinfo.clone();
92         processInfo();
93     }
94     
95     protected void processInfo() {
96         super.setName(info.getName());
97         setIconBase(info.getIconBase());
98
99         // Read options
100
// Cut, copy and delete flags
101

102         Map opts = (Map)info.get("options"); //NOI18N
103
if (opts != null) {
104             String JavaDoc str = (String JavaDoc)opts.get("cut"); //NOI18N
105
if (str != null) cutflag = str.toUpperCase().equals("YES"); //NOI18N
106
str = (String JavaDoc)opts.get("copy"); //NOI18N
107
if (str != null) copyflag = str.toUpperCase().equals("YES"); //NOI18N
108
str = (String JavaDoc)opts.get("delete"); //NOI18N
109
if (str != null) delflag = str.toUpperCase().equals("YES"); //NOI18N
110
}
111
112         try {
113             Vector prop = (Vector)info.get(DatabaseNodeInfo.PROPERTIES);
114             Enumeration prop_i = prop.elements();
115             while (prop_i.hasMoreElements()) {
116                 Map propmap = (Map)prop_i.nextElement();
117                 if (((String JavaDoc)propmap.get(DatabaseNodeInfo.CODE)).equals(DatabaseNodeInfo.NAME)) {
118                     writable = ((String JavaDoc)propmap.get(DatabaseNodeInfo.WRITABLE)).toUpperCase().equals("YES"); //NOI18N
119
}
120             }
121         } catch (Exception JavaDoc e) {}
122     }
123
124     /** Sets name */
125     public void setName(String JavaDoc newname)
126     {
127         super.setName(newname);
128         info.setName(newname);
129     }
130
131     public boolean canRename()
132     {
133         return writable;
134     }
135
136     /**
137     * Can be cut only if copyable flag is set.
138     */

139     public boolean canCut ()
140     {
141         return cutflag;
142     }
143
144     /**
145     * Can be copied only if copyable flag is set.
146     */

147     public boolean canCopy ()
148     {
149         return copyflag;
150     }
151
152     /**
153     * Can be destroyed only if copyable flag is set.
154     */

155     public boolean canDestroy()
156     {
157         return delflag;
158     }
159
160     public void destroy() throws IOException JavaDoc
161     {
162         info.delete();
163         DatabaseNodeInfo parent = info.getParent();
164         super.destroy();
165         try{
166             parent.refreshChildren();
167         } catch (Exception JavaDoc ex){
168             org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex);
169         }
170     }
171
172     public Node.Cookie getCookie(Class JavaDoc cls)
173     {
174         if (cls.isInstance(info)) return info;
175         return super.getCookie(cls);
176     }
177
178     public Action JavaDoc[] getActions(boolean context) {
179         if (context) {
180             return getContextActions();
181         }
182         Vector actions = info.getActions();
183         if (actions.size() > 0) {
184             return (Action JavaDoc[]) actions.toArray(new Action JavaDoc[actions.size()]);
185         }
186         return new Action JavaDoc[0];
187     }
188
189     protected Map createProperty(String JavaDoc name)
190     {
191         return null;
192     }
193
194     protected PropertySupport createPropertySupport(String JavaDoc name, Class JavaDoc type, String JavaDoc displayName, String JavaDoc shortDescription, DatabaseNodeInfo rep, boolean writable)
195     {
196         return new DatabasePropertySupport(name, type, displayName, shortDescription, rep, writable);
197     }
198
199     protected PropertySupport createPropertySupport(String JavaDoc name, Class JavaDoc type, String JavaDoc displayName, String JavaDoc shortDescription, DatabaseNodeInfo rep, boolean writable, boolean expert)
200     {
201         PropertySupport ps = new DatabasePropertySupport(name, type, displayName, shortDescription, rep, writable);
202         ps.setExpert(expert);
203         return ps;
204     }
205
206     /** Sheet for this node.
207     */

208     protected Sheet createSheet()
209     {
210         Sheet sheet = Sheet.createDefault();
211         Sheet.Set ps = sheet.get(Sheet.PROPERTIES);
212         Vector prop = (Vector)info.get(DatabaseNodeInfo.PROPERTIES);
213         Enumeration prop_i = prop.elements();
214         while (prop_i.hasMoreElements()) {
215             boolean canWrite, expert = false;
216             Map propmap = (Map)prop_i.nextElement();
217             String JavaDoc key = (String JavaDoc)propmap.get(DatabaseNodeInfo.CODE);
218             String JavaDoc expkey = (String JavaDoc)propmap.get("expert"); //NOI18N
219
if (expkey != null) expert = expkey.toUpperCase().equals("YES"); //NOI18N
220

221             try {
222
223                 PropertySupport psitem = null;
224                 String JavaDoc pname = null, pclass = null, pdesc = null;
225                 if (propmap == null) {
226                     propmap = createProperty(key);
227                     if (propmap != null) info.put(key, propmap);
228                 }
229
230                 if (key.equals("name")) { //NOI18N
231
if (!info.isReadOnly()) psitem = new PropertySupport.Name(this);
232                 } else {
233                     Class JavaDoc pc = null;
234                     pname = (String JavaDoc)propmap.get(DatabaseNodeInfo.NAME);
235                     if (info.canAdd(propmap, pname)) {
236                         pclass = (String JavaDoc)propmap.get(DatabaseNodeInfo.CLASS);
237                         canWrite = info.canWrite(propmap, pname, writable);
238                         if (pclass.equals("java.lang.Boolean")) pc = Boolean JavaDoc.class; //NOI18N
239
else if (pclass.equals("java.lang.Integer")) pc = Integer.TYPE; //NOI18N
240
else pc = Class.forName(pclass);
241
242                         try {
243                             pname = NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString(pname);
244                         } catch (MissingResourceException e) {
245                             pdesc = NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("DatabaseNodeUntitled"); //NOI18N
246
}
247
248                         psitem = createPropertySupport(key, pc, pname, pdesc, info, canWrite, expert);
249                     }
250                 }
251
252                 if (psitem != null) ps.put(psitem);
253                 // else throw new DatabaseException("no property for "+pname+" "+pclass);
254

255             } catch (Exception JavaDoc ex) {
256                 ex.printStackTrace();
257             }
258         }
259
260         return sheet;
261     }
262
263     /** Deletes subnode.
264     * Called by deleteNode.
265     * @param node Node to delete.
266     */

267     protected void deleteNode(DatabaseNode node)
268     throws DatabaseException
269     {
270         try {
271             DatabaseNodeInfo ninfo = node.getInfo();
272             DatabaseNodeChildren children = (DatabaseNodeChildren)getChildren();
273             info.getChildren().removeElement(ninfo);
274             children.remove(new Node[] {node});
275         } catch (Exception JavaDoc e) {
276             throw new DatabaseException(e.getMessage());
277         }
278     }
279
280     /** Deletes node and subnodes.
281     * Called by delete actions
282     */

283     public void deleteNode()
284     throws DatabaseException
285     {
286         try {
287             DatabaseNode parent = (DatabaseNode)getParentNode().getCookie(null);
288             if (parent != null) parent.deleteNode(this);
289         } catch (Exception JavaDoc e) {
290             throw new DatabaseException(e.getMessage());
291         }
292     }
293     
294     public HelpCtx getHelpCtx () {
295         return new HelpCtx ("dbexpovew");
296     }
297     
298     public String JavaDoc getShortDescription() {
299         String JavaDoc code = getInfo().getCode();
300         
301         if (code.equals(DatabaseNode.INDEXCOLUMN))
302             return NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ND_Column"); //NOI18N
303
else if (code.equals("fklist"))
304             return NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ND_ForeignKeyList"); //NOI18N
305
else if (code.equals("ilist"))
306             return NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ND_IndexList"); //NOI18N
307
else
308             return ""; //NOI18N
309
}
310
311 }
312
Popular Tags