KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.*;
25 import java.text.MessageFormat JavaDoc;
26 import java.util.*;
27
28 import org.openide.nodes.NodeTransfer;
29 import org.openide.nodes.Node;
30 import org.openide.util.NbBundle;
31 import org.openide.util.datatransfer.PasteType;
32
33 import org.netbeans.lib.ddl.*;
34 import org.netbeans.lib.ddl.impl.*;
35 import org.netbeans.modules.db.*;
36 import org.netbeans.modules.db.explorer.*;
37 import org.netbeans.modules.db.explorer.infos.*;
38
39 public class IndexNode extends DatabaseNode {
40     /*
41         public void setName(String newname)
42         {
43             try {
44                 DatabaseNodeInfo info = getInfo();
45                 String table = (String)info.get(DatabaseNode.TABLE);
46                 Specification spec = (Specification)info.getSpecification();
47                 RenameColumn cmd = spec.createCommandRenameColumn(table);
48                 cmd.renameColumn(info.getName(), newname);
49                 cmd.execute();
50                 super.setName(newname);
51             } catch (Exception e) {
52                 e.printStackTrace();
53             }
54         }
55     */

56
57     protected void createPasteTypes(Transferable JavaDoc t, List s)
58     {
59         super.createPasteTypes(t, s);
60         Node node = NodeTransfer.node(t, NodeTransfer.MOVE);
61         if (node != null) {
62             ColumnNodeInfo nfo = (ColumnNodeInfo)node.getCookie(ColumnNodeInfo.class);
63             if (nfo != null) s.add(new IndexPasteType((ColumnNodeInfo)nfo, null));
64         }
65     }
66
67     class IndexPasteType extends PasteType
68     {
69         /** transferred info */
70         private DatabaseNodeInfo info;
71
72         /** the node to destroy or null */
73         private Node node;
74
75         /** Constructs new TablePasteType for the specific type of operation paste.
76         */

77         public IndexPasteType(ColumnNodeInfo info, Node node)
78         {
79             this.info = info;
80             this.node = node;
81         }
82
83         /* @return Human presentable name of this paste type. */
84         public String JavaDoc getName() {
85             return NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("IndexPasteTypeName"); //NOI18N
86
}
87
88         /** Performs the paste action.
89         * @return Transferable which should be inserted into the clipboard after
90         * paste action. It can be null, which means that clipboard content
91         * should stay the same.
92         */

93         public Transferable JavaDoc paste() throws IOException JavaDoc {
94             IndexNodeInfo destinfo = (IndexNodeInfo)getInfo();
95             
96             if (info != null) {
97                 Specification spec;
98
99                 try {
100                     spec = (Specification)info.getSpecification();
101                     DriverSpecification drvSpec = info.getDriverSpecification();
102                     drvSpec.getIndexInfo(info.getTable(), false, true);
103                     ResultSet rs = drvSpec.getResultSet();
104                     if (rs != null) {
105                         String JavaDoc index = destinfo.getName();
106                         HashSet ixrm = new HashSet();
107                         HashMap rset = new HashMap();
108
109                         while (rs.next()) {
110                             rset = drvSpec.getRow();
111                             String JavaDoc ixname = (String JavaDoc) rset.get(new Integer JavaDoc(6));
112                             String JavaDoc colname = (String JavaDoc) rset.get(new Integer JavaDoc(9));
113                             if (ixname.equals(index))
114                                 ixrm.add(colname);
115                             rset.clear();
116                         }
117                         rs.close();
118
119                         if (ixrm.contains(info.getName())) {
120                             String JavaDoc message = MessageFormat.format(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("EXC_IndexContainsColumn"), new String JavaDoc[] {index, info.getName()}); // NOI18N
121
throw new IOException JavaDoc(message);
122                         }
123
124                         CreateIndex icmd = spec.createCommandCreateIndex(info.getTable());
125                         icmd.setIndexName(destinfo.getName());
126                         Iterator enu = ixrm.iterator();
127                         while (enu.hasNext()) {
128                             icmd.specifyColumn((String JavaDoc)enu.next());
129                         }
130
131                         icmd.specifyColumn(info.getName());
132                         DropIndex dicmd = spec.createCommandDropIndex(index);
133                         dicmd.setObjectOwner((String JavaDoc)destinfo.get(DatabaseNodeInfo.SCHEMA));
134                         dicmd.execute();
135                         icmd.setObjectOwner((String JavaDoc)destinfo.get(DatabaseNodeInfo.SCHEMA));
136                         icmd.execute();
137
138                         drvSpec.getIndexInfo(destinfo.getTable(), false, true);
139                         rs = drvSpec.getResultSet();
140                         if (rs != null) {
141                             IndexNodeInfo ixinfo;
142                             while (rs.next()) {
143                                 rset = drvSpec.getRow();
144                                 String JavaDoc ixname = (String JavaDoc) rset.get(new Integer JavaDoc(6));
145                                 String JavaDoc colname = (String JavaDoc) rset.get(new Integer JavaDoc(9));
146                                 if (ixname.equals(index) && colname.equals(info.getName())) {
147                                     ixinfo = (IndexNodeInfo) DatabaseNodeInfo.createNodeInfo(destinfo, DatabaseNode.INDEX, rset);
148                                     if (ixinfo != null)
149                                         ((DatabaseNodeChildren)destinfo.getNode().getChildren()).createSubnode(ixinfo,true);
150                                     else
151                                         throw new Exception JavaDoc(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("EXC_UnableToCreateIndexNodeInfo")); //NOI18N
152
}
153                                 rset.clear();
154                             }
155                             rs.close();
156                         }
157                     }
158                 } catch (Exception JavaDoc e) {
159                     throw new IOException JavaDoc(e.getMessage());
160                 }
161
162             } else
163                 throw new IOException JavaDoc(NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("EXC_CannotFindIndexOwnerInformation"));
164             
165             return null;
166         }
167     }
168     
169     public String JavaDoc getShortDescription() {
170         return NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ND_Index"); //NOI18N
171
}
172
173 }
174
Popular Tags