1 19 20 package org.netbeans.modules.db.explorer.actions; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.beans.PropertyChangeSupport ; 25 import java.sql.DatabaseMetaData ; 26 import java.sql.SQLException ; 27 import java.text.MessageFormat ; 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 48 static final long serialVersionUID =-894644054833609687L; 49 50 private String quoteStr; 51 52 53 final private PropertyChangeSupport propertySupport; 54 55 private Node[] nodes; 56 private DataViewWindow win; 57 58 public ViewDataAction() { 59 propertySupport = new PropertyChangeSupport (this); 60 propertySupport.addPropertyChangeListener(new PropertyChangeListener () { 61 public void propertyChange(PropertyChangeEvent evt) { 62 if (evt.getPropertyName().equals("retrieved")) 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 () { 99 public void run () { 100 try { 101 DatabaseMetaData dmd = info.getConnection().getMetaData(); 102 quoteStr = dmd.getIdentifierQuoteString(); 103 if (quoteStr == null) 104 quoteStr = ""; else 106 quoteStr.trim(); 107 108 propertySupport.firePropertyChange("retrieved", null, null); } catch (SQLException exc) { 110 } 112 } 113 }, 0); 114 } 115 } 116 117 private void finishAction() { 118 String expression = ""; StringBuffer cols = new StringBuffer (); 120 Node node; 121 122 try { 123 node = nodes[0]; 124 DatabaseNodeInfo info = (DatabaseNodeInfo) node.getCookie(DatabaseNodeInfo.class); 125 126 String schema = info.getSchema(); 127 if (schema == null) 128 schema = ""; else 130 schema = schema.trim(); 131 132 String onome; 133 if (info instanceof TableNodeInfo || info instanceof ViewNodeInfo) { 134 onome = quote(info.getName()); 135 if (!schema.equals("")) onome = quote(schema) + "." + onome; 138 expression = "select * from " + onome; } else if (info instanceof ColumnNodeInfo || info instanceof ViewColumnNodeInfo) { 140 onome = quote((info instanceof ViewColumnNodeInfo) ? info.getView() : info.getTable()); 141 if (!schema.equals("")) onome = quote(schema) + "." + onome; 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(", "); cols.append(quote(info.getName())); 151 } 152 } 153 154 expression = "select " + cols.toString() + " from " + onome; } 156 157 String name = ((DatabaseConnection)info.getDatabaseConnection()).getName(); 158 SQLEditorSupport.openSQLEditor(ConnectionManager.getDefault().getConnection(name), expression, true); 159 } catch(Exception exc) { 160 String message = MessageFormat.format(bundle().getString("ShowDataError"), new String [] {exc.getMessage()}); DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE)); 162 } 163 } 164 165 private String quote(String name) { 166 return quoteStr + name + quoteStr; 167 } 168 } 169 | Popular Tags |