KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > explorer > actions > AddIndexAction


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.actions;
21
22 import java.sql.*;
23 import java.text.MessageFormat JavaDoc;
24 import java.util.*;
25
26 import org.openide.*;
27 import org.openide.nodes.*;
28
29 import org.netbeans.lib.ddl.*;
30 import org.netbeans.lib.ddl.impl.*;
31 import org.netbeans.lib.ddl.adaptors.*;
32 import org.netbeans.modules.db.explorer.*;
33 import org.netbeans.modules.db.explorer.dlg.*;
34 import org.netbeans.modules.db.explorer.nodes.*;
35 import org.netbeans.modules.db.explorer.infos.*;
36
37 public class AddIndexAction extends DatabaseAction {
38     public void performAction (Node[] activatedNodes) {
39         Node node;
40         if (activatedNodes != null && activatedNodes.length>0)
41             node = activatedNodes[0];
42         else
43             return;
44
45         try {
46             DatabaseNodeInfo info = (DatabaseNodeInfo)node.getCookie(DatabaseNodeInfo.class);
47             IndexListNodeInfo nfo = (IndexListNodeInfo)info.getParent(nodename);
48
49             String JavaDoc tablename = (String JavaDoc)nfo.get(DatabaseNode.TABLE);
50             String JavaDoc columnname = (String JavaDoc)nfo.get(DatabaseNode.COLUMN);
51
52             Specification spec = (Specification)nfo.getSpecification();
53             String JavaDoc index = (String JavaDoc)nfo.get(DatabaseNode.INDEX);
54             DriverSpecification drvSpec = info.getDriverSpecification();
55
56             // List columns not present in current index
57
Vector cols = new Vector(5);
58
59             drvSpec.getColumns(tablename, "%");
60             ResultSet rs = drvSpec.getResultSet();
61             HashMap rset = new HashMap();
62             while (rs.next()) {
63                 rset = drvSpec.getRow();
64                 cols.add((String JavaDoc) rset.get(new Integer JavaDoc(4)));
65                 rset.clear();
66             }
67             rs.close();
68
69             if (cols.size() == 0)
70                 throw new Exception JavaDoc(bundle().getString("EXC_NoUsableColumnInPlace")); // NOI18N
71

72             // Create and execute command
73
AddIndexDialog dlg = new AddIndexDialog(cols, spec, info);
74             dlg.setIndexName(tablename + "_idx"); // NOI18N
75
if (dlg.run()) {
76                 nfo.addIndex(dlg.getIndexName());
77             }
78         } catch(Exception JavaDoc exc) {
79             String JavaDoc message = MessageFormat.format(bundle().getString("ERR_UnableToPerformOperation"), new String JavaDoc[] {node.getName(), exc.getMessage()}); // NOI18N
80
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE));
81         }
82     }
83 }
84
Popular Tags