KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.datatransfer.Transferable JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.*;
25 import java.text.MessageFormat JavaDoc;
26
27
28 import org.openide.*;
29 import org.openide.nodes.NodeTransfer;
30 import org.openide.nodes.Node;
31 import org.openide.util.datatransfer.PasteType;
32 import org.openide.util.NbBundle;
33
34 import org.netbeans.lib.ddl.*;
35 import org.netbeans.lib.ddl.impl.*;
36 import org.netbeans.modules.db.explorer.*;
37 import org.netbeans.modules.db.explorer.infos.*;
38 import org.openide.util.Lookup;
39 import org.openide.util.datatransfer.ExTransferable;
40
41 // Node for Table/View/Procedure things.
42

43 public class TableNode extends DatabaseNode /*implements InstanceCookie*/ {
44     public void setInfo(DatabaseNodeInfo nodeinfo)
45     {
46         super.setInfo(nodeinfo);
47         getCookieSet().add(this);
48     }
49
50 /*
51     public String instanceName() {
52         return "org.netbeans.lib.sql.ConnectionSource"; //NOI18N
53     }
54
55     public Class instanceClass() throws IOException, ClassNotFoundException {
56         return Class.forName("org.netbeans.lib.sql.ConnectionSource", true, org.openide.Topmanager.getDefault ().currentClassLoader ()); //NOI18N
57     }
58
59     public Object instanceCreate()
60     {
61         DatabaseNodeInfo info = getInfo();
62         try {
63             Method met;
64             Class objclass = instanceClass();
65             String drv = info.getDriver();
66             String db = info.getDatabase();
67             String usr = info.getUser();
68             String pwd = info.getPassword();
69             Object obj = objclass.newInstance();
70
71             met = objclass.getMethod("setDriver", new Class[] {String.class}); //NOI18N
72             if (met != null) met.invoke(obj, new String[] {drv});
73             met = objclass.getMethod("setDatabase", new Class[] {String.class}); //NOI18N
74             if (met != null) met.invoke(obj, new String[] {db});
75             met = objclass.getMethod("setUsername", new Class[] {String.class}); //NOI18N
76             if (met != null) met.invoke(obj, new String[] {usr});
77             met = objclass.getMethod("setPassword", new Class[] {String.class}); //NOI18N
78             if (met != null) met.invoke(obj, new String[] {pwd});
79
80             return obj;
81
82         } catch (Exception ex) {
83             ex.printStackTrace ();
84             return null;
85         }
86     }
87 */

88     
89     public void setName(String JavaDoc newname)
90     {
91         try {
92             DatabaseNodeInfo info = getInfo();
93             Specification spec = (Specification)info.getSpecification();
94             AbstractCommand cmd = spec.createCommandRenameTable(info.getName(), newname);
95             cmd.setObjectOwner((String JavaDoc)info.get(DatabaseNodeInfo.SCHEMA));
96             cmd.execute();
97             super.setName(newname);
98             info.put(DatabaseNode.TABLE, newname);
99         } catch (CommandNotSupportedException ex) {
100             DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(ex.getMessage(), NotifyDescriptor.ERROR_MESSAGE));
101         } catch (Exception JavaDoc ex) {
102             // ex.printStackTrace();
103
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(ex.getMessage(), NotifyDescriptor.ERROR_MESSAGE));
104         }
105     }
106     
107     public Transferable JavaDoc clipboardCopy() throws IOException JavaDoc {
108         Transferable JavaDoc result;
109         final DbMetaDataTransferProvider dbTansferProvider = (DbMetaDataTransferProvider)Lookup.getDefault().lookup(DbMetaDataTransferProvider.class);
110         if (dbTansferProvider != null) {
111             ExTransferable exTransferable = ExTransferable.create(super.clipboardCopy());
112             ConnectionNodeInfo cni = (ConnectionNodeInfo)getInfo().getParent(DatabaseNode.CONNECTION);
113             final DatabaseConnection dbconn = ConnectionList.getDefault().getConnection(cni.getDatabaseConnection());
114             exTransferable.put(new ExTransferable.Single(dbTansferProvider.getTableDataFlavor()) {
115                 protected Object JavaDoc getData() {
116                     return dbTansferProvider.createTableData(dbconn.getDatabaseConnection(), dbconn.findJDBCDriver(), getInfo().getName());
117                 }
118             });
119             result = exTransferable;
120         } else {
121             result = super.clipboardCopy();
122         }
123         return result;
124     }
125
126     protected void createPasteTypes(Transferable JavaDoc t, List s)
127     {
128         super.createPasteTypes(t, s);
129         DatabaseNodeInfo nfo;
130         Node n = NodeTransfer.node(t, NodeTransfer.MOVE);
131         if (n != null && n.canDestroy ()) {
132             /*
133                         nfo = (TableNodeInfo)n.getCookie(TableNodeInfo.class);
134                         if (nfo != null) {
135                             s.add(new TablePasteType((TableNodeInfo)nfo, n));
136                             return;
137                         }
138             */

139             nfo = (ColumnNodeInfo)n.getCookie(ColumnNodeInfo.class);
140             if (nfo != null) {
141                 s.add(new ColumnPasteType((ColumnNodeInfo)nfo, n));
142                 return;
143             }
144
145         } else {
146             /*
147                         nfo = (DatabaseNodeInfo)NodeTransfer.copyCookie(t, TableNodeInfo.class);
148                         if (nfo != null) {
149                             s.add(new TablePasteType((TableNodeInfo)nfo, null));
150                             return;
151                         }
152             */

153             nfo = (DatabaseNodeInfo)NodeTransfer.cookie(t, NodeTransfer.MOVE, ColumnNodeInfo.class);
154             if (nfo != null) {
155                 s.add(new ColumnPasteType((ColumnNodeInfo)nfo, null));
156                 return;
157             }
158         }
159     }
160
161     /** Paste type for transfering tables.
162     */

163     private class TablePasteType extends PasteType
164     {
165         /** transferred info */
166         private DatabaseNodeInfo info;
167
168         /** the node to destroy or null */
169         private Node node;
170
171         /** Constructs new TablePasteType for the specific type of operation paste.
172         */

173         public TablePasteType(TableNodeInfo info, Node node)
174         {
175             this.info = info;
176             this.node = node;
177         }
178
179         /* @return Human presentable name of this paste type. */
180         public String JavaDoc getName()
181         {
182             ResourceBundle bundle = NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle"); //NOI18N
183
return bundle.getString("PasteTableName"); //NOI18N
184
}
185
186         /** Performs the paste action.
187         * @return Transferable which should be inserted into the clipboard after
188         * paste action. It can be null, which means that clipboard content
189         * should stay the same.
190         */

191         public Transferable JavaDoc paste() throws IOException JavaDoc
192         {
193             TableNodeInfo info = (TableNodeInfo)getInfo();
194             ResourceBundle bundle = NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle_noi18n"); //NOI18N
195
TableListNodeInfo ownerinfo = (TableListNodeInfo)getInfo().getParent(DatabaseNode.TABLELIST);
196             if (info != null) {
197                 TableNodeInfo exinfo = ownerinfo.getChildrenTableInfo(info);
198                 DatabaseNodeChildren chi = (DatabaseNodeChildren)getChildren();
199                 String JavaDoc name = info.getName();
200                 if (exinfo != null) {
201                     String JavaDoc namefmt = bundle.getString("PasteTableNameFormat"); //NOI18N
202
name = MessageFormat.format(namefmt, new String JavaDoc[] {name});
203                 }
204
205                 try {
206
207                     // Create in database
208
// PENDING
209

210                     ownerinfo.addTable(name);
211                     if (node != null) node.destroy ();
212
213                 } catch (Exception JavaDoc e) {
214                     throw new IOException JavaDoc(e.getMessage());
215                 }
216
217             } else
218                 throw new IOException JavaDoc(bundle.getString("EXC_CannotFindTableOwnerInformation")); //NOI18N
219

220             return null;
221         }
222     }
223
224     /** Paste type for transfering columns.
225     */

226     private class ColumnPasteType extends PasteType {
227         final ResourceBundle bundle = NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle"); //NOI18N
228

229         /** transferred info */
230         private DatabaseNodeInfo info;
231
232         /** the node to destroy or null */
233         private Node node;
234
235         /** Constructs new TablePasteType for the specific type of operation paste.
236         */

237         public ColumnPasteType(ColumnNodeInfo info, Node node)
238         {
239             this.info = info;
240             this.node = node;
241         }
242
243         /* @return Human presentable name of this paste type. */
244         public String JavaDoc getName() {
245             return bundle.getString("PasteColumnName"); //NOI18N
246
}
247
248         /** Performs the paste action.
249         * @return Transferable which should be inserted into the clipboard after
250         * paste action. It can be null, which means that clipboard content
251         * should stay the same.
252         */

253         public Transferable JavaDoc paste() throws IOException JavaDoc
254         {
255             ResourceBundle bundle = NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle"); //NOI18N
256
TableNodeInfo ownerinfo = (TableNodeInfo)getInfo();
257             if (info != null) {
258                 try {
259                     String JavaDoc name = info.getName();
260                     ColumnNodeInfo coli = (ColumnNodeInfo)info;
261                     TableColumn col = coli.getColumnSpecification();
262                     Specification spec = (Specification)ownerinfo.getSpecification();
263                     AddColumn cmd = (AddColumn)spec.createCommandAddColumn(ownerinfo.getTable());
264                     cmd.getColumns().add(col);
265                     cmd.setObjectOwner((String JavaDoc)info.get(DatabaseNodeInfo.SCHEMA));
266                     cmd.execute();
267                     ownerinfo.addColumn(name);
268                     if (node != null) node.destroy();
269                 } catch (final Exception JavaDoc ex) {
270                     ex.printStackTrace();
271                     /*
272                                         SwingUtilities.invokeLater(new Runnable() {
273                                             public void run() {
274                                                 Topmanager.getDefault().notify(new NotifyDescriptor.Message("Unable to process command, "+e.getMessage(), NotifyDescriptor.ERROR_MESSAGE));
275                                             }
276                                         });
277                     */

278                 }
279             } else throw new IOException JavaDoc(bundle.getString("EXC_CannotFindColumnOwnerInformation")); //NOI18N
280
return null;
281         }
282     }
283
284     public String JavaDoc getShortDescription() {
285         return NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ND_Table"); //NOI18N
286
}
287     
288 }
289
Popular Tags