Newer
Older
jrex / src / main / java / jrex / ui / SqlDialog.java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package jrex.ui;

import java.awt.Window;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import jrex.ui.model.PivotTableModel;

/**
 *
 * @author mark
 */
public class SqlDialog extends javax.swing.JDialog {

   private String sqlString = "";
   
   /**
    * Creates new form SqlDialog
    */
   public SqlDialog(Window parent, boolean modal, PivotTableModel tableModel) {
      super(parent);
      this.setModal(modal);
      this.setLocationRelativeTo(parent);
      initComponents();
      
      columnSelectionPanel.setLeftContent("Available Columns", Arrays.asList(tableModel.getHeadings().toArray()));
      columnSelectionPanel.setRightContent("Included Columns", new ArrayList());
   }

   /**
    * 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() {

      jLabel1 = new javax.swing.JLabel();
      txtTable = new javax.swing.JTextField();
      columnSelectionPanel = new jrex.ui.ColumnSelectionPanel();
      btnCancel = new javax.swing.JButton();
      btnOK = new javax.swing.JButton();

      setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

      jLabel1.setText("Table Name:");

      btnCancel.setText("Cancel");
      btnCancel.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnCancelActionPerformed(evt);
         }
      });

      btnOK.setText("OK");
      btnOK.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnOKActionPerformed(evt);
         }
      });

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(layout.createSequentialGroup()
                  .addComponent(jLabel1)
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                  .addComponent(txtTable))
               .addComponent(columnSelectionPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
               .addGroup(layout.createSequentialGroup()
                  .addComponent(btnCancel)
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                  .addComponent(btnOK)))
            .addContainerGap())
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(jLabel1)
               .addComponent(txtTable, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(columnSelectionPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(btnCancel)
               .addComponent(btnOK))
            .addContainerGap())
      );

      pack();
   }// </editor-fold>//GEN-END:initComponents

   private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
      dispose();
   }//GEN-LAST:event_btnCancelActionPerformed

   private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed
      Collection<Object> rightItems = columnSelectionPanel.getRightItems();
      String table = txtTable.getText();
      
      if(rightItems.isEmpty() || table.trim().isEmpty()) {
         return;
      }

      StringBuilder builder = new StringBuilder("INSERT INTO " + table + " (");

      rightItems.forEach(column -> builder.append(column.toString() + ", "));
      builder.deleteCharAt(builder.lastIndexOf(","));
      builder.deleteCharAt(builder.lastIndexOf(" "));
      builder.append(") VALUES (");
      rightItems.forEach(column -> builder.append("?"+column.toString() + ", "));
      builder.deleteCharAt(builder.lastIndexOf(","));
      builder.deleteCharAt(builder.lastIndexOf(" "));
      builder.append(");");
      
      this.sqlString = builder.toString();
      dispose();
   }//GEN-LAST:event_btnOKActionPerformed

   public String getSql() {
      return this.sqlString;
   }   
   
   
   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JButton btnCancel;
   private javax.swing.JButton btnOK;
   private jrex.ui.ColumnSelectionPanel columnSelectionPanel;
   private javax.swing.JLabel jLabel1;
   private javax.swing.JTextField txtTable;
   // End of variables declaration//GEN-END:variables


}