Mercurial > hg > ltpda
view src/MPipeline2/src/mpipeline/repository/QueryResultsTableDialog.java @ 29:54f14716c721 database-connection-manager
Update Java code
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Mon, 05 Dec 2011 16:20:06 +0100 |
parents | f0afece42f48 |
children |
line wrap: on
line source
/* * Class QueryResultsTable <one line to give the program's name and a brief idea of what it does.> * * Copyright (c) 2009 Max-Planck-Gesellschaft, Martin Hewitson <martin.hewitson at aei.mpg.de> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ /* * QueryResultsTable.java * * Created on 25-Jul-2009, 18:57:11 */ package mpipeline.repository; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.ArrayList; import java.util.regex.Pattern; import java.util.regex.Matcher; import javax.swing.JButton; import javax.swing.JOptionPane; import javax.swing.table.DefaultTableModel; import mpipeline.canvas.BlockDiagram; import mpipeline.canvas.MBlock; import mpipeline.canvas.MCanvas; import mpipeline.jltpda.LTPDAalgorithm; import mpipeline.main.MainWindow; import mpipeline.plisttable.JPlist; /** * * @author Martin Hewitson <martin.hewitson at aei.mpg.de> */ public class QueryResultsTableDialog extends javax.swing.JDialog { private boolean cancelled = true; private ResultSet results = null; private ArrayList<String> colNames = new ArrayList<String>(); private MainWindow mw = null; private java.sql.Connection conn = null; /** Creates new form QueryResultsTable * @param parent * @param modal * @param rs * @param query * @param withConstructorButton */ public QueryResultsTableDialog(MainWindow parent, boolean modal, ResultSet rs, String query, boolean withConstructorButton) { super(parent, modal); initComponents(); if (!withConstructorButton) { // createConstructors.setVisible(false); createConstructors.setText("Retrieve selected Objects"); createConstructors.setToolTipText(""); pack(); } mw = parent; results = rs; queryTxtField.setText(query); queryTxtField.setEditable(false); try { ResultSetMetaData rsmd = rs.getMetaData(); int Ncol = rsmd.getColumnCount(); DefaultTableModel aModel = (DefaultTableModel) resultsTable.getModel(); colNames = new ArrayList<String>(); for (int kk = 1; kk <= Ncol; kk++) { colNames.add(rsmd.getColumnName(kk)); } aModel.setColumnIdentifiers(colNames.toArray()); // Loop through the ResultSet and transfer in the Model rs.beforeFirst(); while (rs.next()) { Object[] objects = new Object[Ncol]; for (int i = 0; i < Ncol; i++) { Object o = rs.getObject(i + 1); if (o == null) { objects[i] = ""; } else { objects[i] = rs.getObject(i + 1); } } aModel.addRow(objects); } resultsTable.setModel(aModel); } catch (SQLException ex) { JOptionPane.showMessageDialog(this, "Failed to populate results table. ", "Table error", JOptionPane.ERROR_MESSAGE); } requestFocus(true); } public boolean isCancelled() { return cancelled; } public JButton getCreateConstructors() { return createConstructors; } public QueryResultsTable getResultsTable() { return resultsTable; } public ArrayList<String> getColNames() { return colNames; } public void setUsedConn(java.sql.Connection conn) { this.conn = conn; } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jPanel2 = new javax.swing.JPanel(); doneBtn = new javax.swing.JButton(); createConstructors = new javax.swing.JButton(); jScrollPane2 = new javax.swing.JScrollPane(); queryTxtField = new javax.swing.JTextArea(); jScrollPane1 = new javax.swing.JScrollPane(); resultsTable = new mpipeline.repository.QueryResultsTable(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setLocationByPlatform(true); jPanel2.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED)); doneBtn.setText("Done"); doneBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { doneBtnActionPerformed(evt); } }); createConstructors.setText("Create Constructors"); createConstructors.setToolTipText("<html>Create constructor blocks for the selected rows.<br>\nThe results must contain an 'obj_id' columns. If the 'obj_type' column<br>\nis present, this constructor type is used, otherwise an AO constructor is created.</html>"); createConstructors.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { createConstructorsActionPerformed(evt); } }); org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup() .addContainerGap() .add(createConstructors) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 409, Short.MAX_VALUE) .add(doneBtn)) ); jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup() .addContainerGap(19, Short.MAX_VALUE) .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(doneBtn) .add(createConstructors)) .addContainerGap()) ); queryTxtField.setColumns(20); queryTxtField.setLineWrap(true); queryTxtField.setRows(5); queryTxtField.setWrapStyleWord(true); jScrollPane2.setViewportView(queryTxtField); resultsTable.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { }, new String [] { } )); resultsTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF); jScrollPane1.setViewportView(resultsTable); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 653, Short.MAX_VALUE) .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 653, Short.MAX_VALUE)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() .addContainerGap() .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 93, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(26, 26, 26) .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED) .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); pack(); }// </editor-fold>//GEN-END:initComponents private void doneBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doneBtnActionPerformed cancelled = false; this.dispose(); }//GEN-LAST:event_doneBtnActionPerformed private void createConstructorsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createConstructorsActionPerformed if (mw != null) { try { int obj_id_col = colNames.indexOf("obj_id"); if (obj_id_col == -1) { throw (new SQLException()); } // IF the LTPDAworkbench exist then create blocks from the selected IDs. // get selected rows int[] rows = resultsTable.getSelectedRows(); for (int kk = 0; kk < rows.length; kk++) { int ri = rows[kk]; // get obj_id Object o = resultsTable.getModel().getValueAt(ri, obj_id_col); if (o == null) { System.err.println("Failed to get object id from table."); return; } int obj_id = Integer.parseInt(o.toString()); // get object type String obj_type = null; try { java.sql.PreparedStatement stmt = conn.prepareStatement( "SELECT obj_type FROM objmeta WHERE obj_id=?"); stmt.setInt(1, obj_id); ResultSet rs = stmt.executeQuery(); rs.next(); obj_type = (String) rs.getObject(1); } catch (SQLException ex) { } if (obj_type == null) { JOptionPane.showMessageDialog(this, "Unable to determine object type for object with id: " + obj_id, "Search error", JOptionPane.ERROR_MESSAGE); return; } // build constructor block ArrayList<LTPDAalgorithm> algos = mw.getLibrary().findMatches(obj_type); if (algos.isEmpty()) { JOptionPane.showMessageDialog(this, "No constructor block found for object type: " + obj_type, "Search error", JOptionPane.ERROR_MESSAGE); return; } if (algos.size() > 1) { System.out.println("More than one match found for '" + obj_type + "'; using the first: " + algos.get(0).getMname()); } // make a block MBlock b = new MBlock(algos.get(0)); b.removeAllInputs(); // extract hostname and database from connection object String url = conn.getMetaData().getURL(); Pattern r = Pattern.compile("^jdbc:mysql://(.*)/(.*)$"); Matcher m = r.matcher(url); m.find(); String hostname = m.group(1); String database = m.group(2); // make a plist for this block JPlist pl = new JPlist(); pl.add("HOSTNAME", hostname, "char"); pl.add("DATABASE", database, "char"); pl.add("ID", "" + obj_id, "char"); pl.add("BINARY", true, "logical"); b.setPlist(pl); // add to current canvas BlockDiagram diag = mw.getActiveDiagram(); if (diag != null) { MCanvas c = diag.getCanvas(); if (c != null) { c.addBlock(b); c.autoPlace(b); } } else { JOptionPane.showMessageDialog(this, "No active canvas detected. Can't create blocks.", "Fields error", JOptionPane.ERROR_MESSAGE); return; } } // end loop over rows } catch (SQLException ex) { JOptionPane.showMessageDialog(this, "The results must contain the field 'obj_id' in order to create constructor blocks.", "Fields error", JOptionPane.ERROR_MESSAGE); } } }//GEN-LAST:event_createConstructorsActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton createConstructors; private javax.swing.JButton doneBtn; private javax.swing.JPanel jPanel2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JTextArea queryTxtField; private mpipeline.repository.QueryResultsTable resultsTable; // End of variables declaration//GEN-END:variables }