view src/ConnectionManager/src/connectionmanager/DatabaseSelectorDialog.java @ 28:01b86b780ba7 database-connection-manager

Remove LTPDARepositoryManager implementation. Java code
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Mon, 05 Dec 2011 16:20:06 +0100
parents 960fe1aa1c10
children
line wrap: on
line source

package connectionmanager;

import java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.DefaultTableCellRenderer;

class CellRenderer extends DefaultTableCellRenderer {

  @Override
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    return super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
  }
}

public class DatabaseSelectorDialog extends javax.swing.JDialog {

  public boolean cancelled = true;
  public String hostname;
  public String database;
  public String username;

  public DatabaseSelectorDialog(java.awt.Frame parent) {
    super(parent, true);
    initComponents();
    // center in parent window
    super.setLocationRelativeTo(parent);
    databasesTable.setDefaultRenderer(Object.class, new CellRenderer());
    databasesTable.addMouseListener(new MouseListenerDoubleClick());
  }

  public DatabaseSelectorDialog() {
    super(new javax.swing.JFrame(), true);
    initComponents();
    databasesTable.setDefaultRenderer(Object.class, new CellRenderer());
    databasesTable.addMouseListener(new MouseListenerDoubleClick());
  }

  private class MouseListenerDoubleClick extends MouseAdapter {

    @Override
    public void mouseClicked(MouseEvent e) {
      if (e.getClickCount() >= 2) {
        cancelled = false;
        Point p = e.getPoint();
        int row = databasesTable.rowAtPoint(p);
        doSelect(row);
      }
    }
  }

  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    cancelButton = new javax.swing.JButton();
    newButton = new javax.swing.JButton();
    selectButton = new javax.swing.JButton();
    scrollPane = new javax.swing.JScrollPane();
    databasesTable = new javax.swing.JTable();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("Database connection");
    setResizable(false);

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

    newButton.setText("New");
    newButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        newButtonActionPerformed(evt);
      }
    });

    selectButton.setText("Select");
    selectButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        selectButtonActionPerformed(evt);
      }
    });

    databasesTable.setModel(new javax.swing.table.DefaultTableModel(
      new Object [][] {

      },
      new String [] {
        "Hostname", "Database", "Username"
      }
    ) {
      Class[] types = new Class [] {
        java.lang.String.class, java.lang.String.class, java.lang.String.class
      };
      boolean[] canEdit = new boolean [] {
        false, false, false
      };

      public Class getColumnClass(int columnIndex) {
        return types [columnIndex];
      }

      public boolean isCellEditable(int rowIndex, int columnIndex) {
        return canEdit [columnIndex];
      }
    });
    databasesTable.setGridColor(new java.awt.Color(204, 204, 204));
    databasesTable.setIntercellSpacing(new java.awt.Dimension(0, 0));
    databasesTable.setRowHeight(24);
    databasesTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    databasesTable.setShowVerticalLines(false);
    databasesTable.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyPressed(java.awt.event.KeyEvent evt) {
        tableKeyPressed(evt);
      }
    });
    scrollPane.setViewportView(databasesTable);

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
        .addContainerGap()
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
          .add(org.jdesktop.layout.GroupLayout.LEADING, scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE)
          .add(layout.createSequentialGroup()
            .add(newButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 62, Short.MAX_VALUE)
            .add(184, 184, 184)
            .add(cancelButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 77, Short.MAX_VALUE)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
            .add(selectButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
        .addContainerGap()
        .add(scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
          .add(newButton)
          .add(selectButton)
          .add(cancelButton))
        .addContainerGap())
    );

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

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
      cancelled = true;
      doClose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void selectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectButtonActionPerformed
      int row = databasesTable.getSelectedRow();
      if (row > -1) {
        doSelect(row);
      }
    }//GEN-LAST:event_selectButtonActionPerformed

    private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newButtonActionPerformed
      DatabaseDialog dialog = new DatabaseDialog(this, databasesTable.getModel());
      setVisible(false);
      dialog.setVisible(true);
      if (!dialog.cancelled) {
        cancelled = false;
        hostname = dialog.hostname;
        database = dialog.database;
        username = "";
        doClose();
      } else {
        setVisible(true);
      }
    }//GEN-LAST:event_newButtonActionPerformed

private void tableKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tableKeyPressed
  if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) {
    int row = databasesTable.getSelectedRow();
    if (row > -1) {
      doSelect(row);
    }
  }
}//GEN-LAST:event_tableKeyPressed

  private void doSelect(int row) {
    cancelled = false;

    DefaultTableModel model = (DefaultTableModel) databasesTable.getModel();
    hostname = (String) model.getValueAt(row, 0);
    database = (String) model.getValueAt(row, 1);
    username = (String) model.getValueAt(row, 2);

    if (database == null) {
      // filter model      
      DefaultTableModel submodel = new DefaultTableModel(0, 3);
      int rows = model.getRowCount();
      for (int i = 0; i < rows; i++) {
        String h = (String) model.getValueAt(i, 0);
        if ((h != null) && h.equals(hostname)) {
          submodel.addRow(new Object[]{hostname,
                                       model.getValueAt(i, 1),
                                       model.getValueAt(i, 2)});
        }
      }
      DatabaseDialog dialog = new DatabaseDialog(this, submodel);
      setVisible(false);
      dialog.setVisible(true);
      if (!dialog.cancelled) {
        cancelled = false;
        hostname = dialog.hostname;
        database = dialog.database;
        username = "";
        doClose();
      }
    }
    doClose();
  }

  private void doClose() {
    setVisible(false);
    dispose();
  }

  public void add(String hostname, String database, String username) {
    DefaultTableModel model = (DefaultTableModel) databasesTable.getModel();
    model.insertRow(model.getRowCount(), new Object[]{hostname, database, username});
    databasesTable.addRowSelectionInterval(0, 0);
  }
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton cancelButton;
  private javax.swing.JTable databasesTable;
  private javax.swing.JButton newButton;
  private javax.swing.JScrollPane scrollPane;
  private javax.swing.JButton selectButton;
  // End of variables declaration//GEN-END:variables
}