view ConnectionManager/src/connectionmanager/DatabaseSelectorDialog.java @ 5:35f1cfcaa5a9

Add crude Java GUI.
author Daniele Nicolodi <daniele@science.unitn.it>
date Wed, 02 Jun 2010 17:29:18 +0200
parents
children 670905eb688a
line wrap: on
line source

package connectionmanager;

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

public class DatabaseSelectorDialog extends javax.swing.JDialog {

  private boolean cancelled = true;

  public String hostname;
  public String database;
  public String username;

  public DatabaseSelectorDialog(java.awt.Frame parent) {
    super(parent, true);
    initComponents();
    databasesTable.addMouseListener(new MouseListenerDoubleClick());
  }

  public DatabaseSelectorDialog() {
        super(new javax.swing.JFrame(), true);
        initComponents();
        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);
        DefaultTableModel model = (DefaultTableModel)databasesTable.getModel();
        hostname = (String)model.getValueAt(row, 0);
        database = (String)model.getValueAt(row, 1);
        username = (String)model.getValueAt(row, 2);
        doClose();
      }
    }
  }

  @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();
        jScrollPane1 = new javax.swing.JScrollPane();
        databasesTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Select a connection");

        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];
            }
        });
        jScrollPane1.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(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(183, 183, 183)
                        .add(cancelButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(newButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(selectButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(20, Short.MAX_VALUE)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 225, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
                    .add(selectButton)
                    .add(newButton)
                    .add(cancelButton))
                .add(207, 207, 207))
        );

        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
    cancelled = false;
    int row = databasesTable.getSelectedRow();
    DefaultTableModel model = (DefaultTableModel)databasesTable.getModel();
    hostname = (String)model.getValueAt(row, 0);
    database = (String)model.getValueAt(row, 1);
    username = (String)model.getValueAt(row, 2);
    doClose();
  }//GEN-LAST:event_selectButtonActionPerformed

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

  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 });
  }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JTable databasesTable;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton newButton;
    private javax.swing.JButton selectButton;
    // End of variables declaration//GEN-END:variables
}