view src/MPipeline2/src/mpipeline/ltpdapreferences/RepositoryPrefGroupPanel.java @ 33:5e7477b94d94 database-connection-manager

Add known repositories list to LTPDAPreferences
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Mon, 05 Dec 2011 16:20:06 +0100
parents 5a49956df427
children
line wrap: on
line source

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RepositoryPrefGroupPanel.java
 *
 * Created on Aug 15, 2010, 3:22:54 PM
 */
package mpipeline.ltpdapreferences;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.TableModelListener;
import javax.swing.event.TableModelEvent;
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);
  }
}

/**
 *
 * @author hewitson
 */
public class RepositoryPrefGroupPanel extends javax.swing.JPanel implements Observer, ChangeListener, ActionListener, TableModelListener {

  private RepositoryPrefGroup repoPrefs;

  /** Creates new form RepositoryPrefGroupPanel */
  public RepositoryPrefGroupPanel(RepositoryPrefGroup repoPrefs) {
    this.repoPrefs = repoPrefs;
    initComponents();
    databasesTable.setDefaultRenderer(Object.class, new CellRenderer());
    
    setPreferences();
    repoPrefs.addObserver(this);

    maxConnectionsNumberSpinner.addChangeListener(this);
    cachePasswordComboBox.addActionListener(this);
    expirySpinner.addChangeListener(this);
    databasesTable.getModel().addTableModelListener(this);
  }

  public void setPreferences() {
    updateMaxConnectionsNumber();
    updateCachePasswords();
    updateExpiry();
    updateRepositories();
  }

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

    jLabel3 = new javax.swing.JLabel();
    expirySpinner = new javax.swing.JSpinner();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    maxConnectionsNumberSpinner = new javax.swing.JSpinner();
    cachePasswordComboBox = new javax.swing.JComboBox();
    jLabel4 = new javax.swing.JLabel();
    scrollPane = new javax.swing.JScrollPane();
    databasesTable = new javax.swing.JTable();
    jLabel5 = new javax.swing.JLabel();
    addButton = new javax.swing.JButton();
    removeButton = new javax.swing.JButton();

    jLabel3.setText("Cached passwords expire after:");

    expirySpinner.setModel(new javax.swing.SpinnerNumberModel(0, 0, 10000000, 10));

    jLabel1.setText("Maximum number of simultaneous database connections:");

    jLabel2.setText("Cache password:");

    maxConnectionsNumberSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(1), Integer.valueOf(1), null, Integer.valueOf(1)));

    cachePasswordComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "No", "Yes", "Ask" }));

    jLabel4.setText("[seconds]");

    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);
    scrollPane.setViewportView(databasesTable);

    jLabel5.setText("Repositories:");

    addButton.setText("Add");
    addButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        addButtonActionPerformed(evt);
      }
    });

    removeButton.setText("Remove");
    removeButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        removeButtonActionPerformed(evt);
      }
    });

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(layout.createSequentialGroup()
        .addContainerGap()
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
          .add(scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
          .add(layout.createSequentialGroup()
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
              .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                .add(jLabel2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(cachePasswordComboBox, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel3))
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(expirySpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 76, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(jLabel4))
          .add(jLabel5)
          .add(layout.createSequentialGroup()
            .add(addButton)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(removeButton))
          .add(layout.createSequentialGroup()
            .add(jLabel1)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(maxConnectionsNumberSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 48, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(layout.createSequentialGroup()
        .addContainerGap()
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(jLabel1)
          .add(maxConnectionsNumberSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(jLabel2)
          .add(cachePasswordComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(jLabel3)
          .add(expirySpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
          .add(jLabel4))
        .add(18, 18, 18)
        .add(jLabel5)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
          .add(addButton)
          .add(removeButton))
        .addContainerGap())
    );
  }// </editor-fold>//GEN-END:initComponents

private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
  DefaultTableModel model = (DefaultTableModel) databasesTable.getModel();
  RepositoryDialog dialog = new RepositoryDialog(javax.swing.SwingUtilities.windowForComponent(this), model);
  dialog.setVisible(true);
  if (! dialog.cancelled)
    model.insertRow(model.getRowCount(), new Object[]{dialog.hostname, dialog.database, dialog.username});
}//GEN-LAST:event_addButtonActionPerformed

private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
  DefaultTableModel model = (DefaultTableModel) databasesTable.getModel();
  int selected = databasesTable.getSelectedRow();
  if (selected > -1)
    model.removeRow(selected);
}//GEN-LAST:event_removeButtonActionPerformed

  public void update(Observable o, Object arg) {
    if (arg != null) {
      if (arg instanceof Integer) {
        int identifier = ((Integer) arg).intValue();
        switch (identifier) {
          case RepositoryPrefGroup.REPOSITORY_EXPIRY_CHANGED:
            updateExpiry();
            break;
          case RepositoryPrefGroup.REPOSITORY_MAX_CONNECTIONS_NUMBER_CHANGED:
            updateMaxConnectionsNumber();
            break;
          case RepositoryPrefGroup.REPOSITORY_CACHE_PASSWORD_CHANGED:
            updateCachePasswords();
            break;
        }
      }
    }
  }

  private void updateMaxConnectionsNumber() {
    maxConnectionsNumberSpinner.setValue(repoPrefs.getMaxConnectionsNumber());
  }
  
  private void updateCachePasswords() {
    cachePasswordComboBox.setSelectedIndex(repoPrefs.getCachePassword().intValue());
  }
  
  private void updateExpiry() {
    expirySpinner.setValue(repoPrefs.getExpiry());
  }

  private void updateRepositories() {
    DefaultTableModel model = (DefaultTableModel) databasesTable.getModel();
    ArrayList<String[]> repos = repoPrefs.getRepositories();
    for (String[] repo : repos) {
      model.insertRow(model.getRowCount(), repo);
    }
  }
  
  public void stateChanged(ChangeEvent e) {
    if (e.getSource() == expirySpinner) {
      repoPrefs.setExpiry((Integer) expirySpinner.getValue());
      return;
    }
    if (e.getSource() == maxConnectionsNumberSpinner) {
      repoPrefs.setMaxConnectionsNumber((Integer) maxConnectionsNumberSpinner.getValue());
      return;
    }
  }

  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == cachePasswordComboBox) {
      repoPrefs.setCachePassword((Integer) cachePasswordComboBox.getSelectedIndex());
      return;  
    }
  }
  
  public void tableChanged(TableModelEvent e) {
    
    
    if (e.getSource() == databasesTable.getModel()) {
      DefaultTableModel model = (DefaultTableModel) databasesTable.getModel();
      int rows = model.getRowCount();
      ArrayList<String[]> value = new ArrayList<String[]>(rows);
      for (int i = 0; i < rows; i++)
        value.add(new String[]{(String) model.getValueAt(i, 0), 
                               (String) model.getValueAt(i, 1),
                               (String) model.getValueAt(i, 2)});
      repoPrefs.setRepositories(value);
      return;
    }
  }
  
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton addButton;
  private javax.swing.JComboBox cachePasswordComboBox;
  private javax.swing.JTable databasesTable;
  private javax.swing.JSpinner expirySpinner;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JLabel jLabel5;
  private javax.swing.JSpinner maxConnectionsNumberSpinner;
  private javax.swing.JButton removeButton;
  private javax.swing.JScrollPane scrollPane;
  // End of variables declaration//GEN-END:variables
}