KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.PropertyChangeSupport JavaDoc;
25 import java.sql.DatabaseMetaData JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.text.MessageFormat JavaDoc;
28 import org.netbeans.api.db.explorer.ConnectionManager;
29 import org.netbeans.modules.db.explorer.DatabaseConnection;
30 import org.netbeans.modules.db.explorer.sql.editor.SQLEditorSupport;
31 import org.openide.DialogDisplayer;
32 import org.openide.NotifyDescriptor;
33 import org.openide.nodes.Node;
34 import org.openide.util.RequestProcessor;
35 import org.netbeans.modules.db.explorer.dataview.DataViewWindow;
36 import org.netbeans.modules.db.explorer.infos.ColumnNodeInfo;
37 import org.netbeans.modules.db.explorer.infos.DatabaseNodeInfo;
38 import org.netbeans.modules.db.explorer.infos.TableNodeInfo;
39 import org.netbeans.modules.db.explorer.infos.ViewColumnNodeInfo;
40 import org.netbeans.modules.db.explorer.infos.ViewNodeInfo;
41
42 public class ViewDataAction extends DatabaseAction {
43     
44     // TODO: simplify this class. there is no need for the property listeners,
45
// the retrieval of the quote string and the opening of the SQL editor
46
// can run in a RequestProcessor.
47

48     static final long serialVersionUID =-894644054833609687L;
49
50     private String JavaDoc quoteStr;
51
52     /** The support to fire property change */
53     final private PropertyChangeSupport JavaDoc propertySupport;
54     
55     private Node[] nodes;
56     private DataViewWindow win;
57     
58     public ViewDataAction() {
59         propertySupport = new PropertyChangeSupport JavaDoc(this);
60         propertySupport.addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
61             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
62                 if (evt.getPropertyName().equals("retrieved")) //NOI18N
63
finishAction();
64             }
65         });
66     }
67     
68     protected boolean enable(Node[] activatedNodes) {
69         if (activatedNodes != null)
70             if (activatedNodes.length == 1)
71                 return true;
72             else if (activatedNodes.length > 0) {
73                 int t = 0;
74                 int v = 0;
75                 for (int i = 0; i < activatedNodes.length; i++) {
76                     if (activatedNodes[i].getCookie(ColumnNodeInfo.class) != null) {
77                         t++;
78                         continue;
79                     }
80                     if (activatedNodes[i].getCookie(ViewColumnNodeInfo.class) != null)
81                         v++;
82                 }
83                 if (t != activatedNodes.length && v != activatedNodes.length)
84                     return false;
85                 else
86                     return true;
87             } else
88                 return false;
89         else
90             return false;
91     }
92
93     public void performAction (Node[] activatedNodes) {
94         if (activatedNodes != null && activatedNodes.length > 0) {
95             nodes = activatedNodes;
96             final DatabaseNodeInfo info = (DatabaseNodeInfo) activatedNodes[0].getCookie(DatabaseNodeInfo.class);
97             
98             RequestProcessor.getDefault().post(new Runnable JavaDoc() {
99                 public void run () {
100                     try {
101                         DatabaseMetaData JavaDoc dmd = info.getConnection().getMetaData();
102                         quoteStr = dmd.getIdentifierQuoteString();
103                         if (quoteStr == null)
104                             quoteStr = ""; //NOI18N
105
else
106                             quoteStr.trim();
107
108                         propertySupport.firePropertyChange("retrieved", null, null); //NOI18N
109
} catch (SQLException JavaDoc exc) {
110                         //PENDING
111
}
112                 }
113             }, 0);
114         }
115     }
116         
117     private void finishAction() {
118         String JavaDoc expression = ""; //NOI18N
119
StringBuffer JavaDoc cols = new StringBuffer JavaDoc();
120         Node node;
121
122         try {
123             node = nodes[0];
124             DatabaseNodeInfo info = (DatabaseNodeInfo) node.getCookie(DatabaseNodeInfo.class);
125
126             String JavaDoc schema = info.getSchema();
127             if (schema == null)
128                 schema = ""; //NOI18N
129
else
130                 schema = schema.trim();
131
132             String JavaDoc onome;
133             if (info instanceof TableNodeInfo || info instanceof ViewNodeInfo) {
134                 onome = quote(info.getName());
135                 if (!schema.equals("")) //NOI18N
136
onome = quote(schema) + "." + onome; //NOI18N
137

138                 expression = "select * from " + onome; //NOI18N
139
} else if (info instanceof ColumnNodeInfo || info instanceof ViewColumnNodeInfo) {
140                 onome = quote((info instanceof ViewColumnNodeInfo) ? info.getView() : info.getTable());
141                 if (!schema.equals("")) //NOI18N
142
onome = quote(schema) + "." + onome; //NOI18N
143

144                 for (int i = 0; i < nodes.length; i++) {
145                     node = nodes[i];
146                     info = (DatabaseNodeInfo) node.getCookie(DatabaseNodeInfo.class);
147                     if (info instanceof ColumnNodeInfo || info instanceof ViewColumnNodeInfo) {
148                         if (cols.length() > 0)
149                             cols.append(", "); //NOI18N
150
cols.append(quote(info.getName()));
151                     }
152                 }
153
154                 expression = "select " + cols.toString() + " from " + onome; //NOI18N
155
}
156             
157             String JavaDoc name = ((DatabaseConnection)info.getDatabaseConnection()).getName();
158             SQLEditorSupport.openSQLEditor(ConnectionManager.getDefault().getConnection(name), expression, true);
159         } catch(Exception JavaDoc exc) {
160             String JavaDoc message = MessageFormat.format(bundle().getString("ShowDataError"), new String JavaDoc[] {exc.getMessage()}); // NOI18N
161
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE));
162         }
163     }
164
165     private String JavaDoc quote(String JavaDoc name) {
166         return quoteStr + name + quoteStr;
167     }
168 }
169
Popular Tags