view ConnectionManager/src/connectionmanager/DatabaseSelectorDialog.java @ 7:ea3ae6ce9fea default tip

Improved Java interface look.
author Daniele Nicolodi <daniele@grinta.net>
date Wed, 23 Nov 2011 20:27:21 +0100
parents 670905eb688a
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.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.DefaultTableCellRenderer;


class CellRenderer extends DefaultTableCellRenderer {
    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();
        getRootPane().setDefaultButton(selectButton);
        databasesTable.setDefaultRenderer(Object.class, new CellRenderer());
        databasesTable.addMouseListener(new MouseListenerDoubleClick());
    }

    public DatabaseSelectorDialog() {
        super(new javax.swing.JFrame(), true);
        initComponents();
        getRootPane().setDefaultButton(selectButton);
        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);
                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();
        scrollPane = new javax.swing.JScrollPane();
        databasesTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Select database");
        setResizable(false);
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        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 [][] {
                {"1", null, null},
                {"2", null, null},
                {"3", null, null},
                {"4", null, null},
                {"5", null, null},
                {"6", null, null},
                {"7", null, null},
                {"8", null, null}
            },
            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.setFocusable(false);
        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);
        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, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(184, 184, 184)
                        .add(cancelButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(selectButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 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, 212, Short.MAX_VALUE)
                .add(18, 18, 18)
                .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
        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 formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
        System.out.printf("key %d is %d\n", evt.getKeyCode(), evt.VK_DOWN);
        if (evt.getKeyCode() == evt.VK_DOWN) {
            int row = databasesTable.getSelectedRow() + 1;
            databasesTable.addRowSelectionInterval(row, row);
            return;
        }
        if (evt.getKeyCode() == evt.VK_UP) {
            int row = databasesTable.getSelectedRow() - 1;
            databasesTable.addRowSelectionInterval(row, row);
            return;
        }
    }//GEN-LAST:event_formKeyPressed

    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
}