Newer
Older
jrex / src / jrex / WideToLongPivotDialog.java
package jrex;


import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import java.awt.Window;
import java.util.Collection;

/**
 *
 * @author Mark George <mark.george@otago.ac.nz>
 */
public class WideToLongPivotDialog extends javax.swing.JDialog {

	private final SimpleListModel preservedModel = new SimpleListModel();
	private final SimpleListModel valuesModel = new SimpleListModel();
	private ListMultimap<String, String> columns;

	/**
	 * Creates new form PivotDialog
	 */
	@SuppressWarnings("unchecked")
	public WideToLongPivotDialog(Window parent) {
		super(parent);
		setModal(true);
		setLocationRelativeTo(parent);
		initComponents();

		lstPreserved.setModel(preservedModel);
		lstValues.setModel(valuesModel);
	}

	/**
	 * 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();
      scrollLeft = new javax.swing.JScrollPane();
      lstPreserved = new javax.swing.JList();
      jScrollPane1 = new javax.swing.JScrollPane();
      lstValues = new javax.swing.JList();
      btnInclude = new javax.swing.JButton();
      btnExclude = new javax.swing.JButton();
      jLabel2 = new javax.swing.JLabel();
      btnOk = new javax.swing.JButton();
      btnCancel = new javax.swing.JButton();

      setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

      jLabel1.setText("Preserved Columns");
      jLabel1.setName("jLabel1"); // NOI18N

      scrollLeft.setName("scrollLeft"); // NOI18N

      lstPreserved.setName("lstPreserved"); // NOI18N
      scrollLeft.setViewportView(lstPreserved);

      jScrollPane1.setName("jScrollPane1"); // NOI18N

      lstValues.setName("lstValues"); // NOI18N
      jScrollPane1.setViewportView(lstValues);

      btnInclude.setText(">");
      btnInclude.setName("btnInclude"); // NOI18N
      btnInclude.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnIncludeActionPerformed(evt);
         }
      });

      btnExclude.setText("<");
      btnExclude.setName("btnExclude"); // NOI18N
      btnExclude.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnExcludeActionPerformed(evt);
         }
      });

      jLabel2.setText("Values");
      jLabel2.setName("jLabel2"); // NOI18N

      btnOk.setText("OK");
      btnOk.setName("btnOk"); // NOI18N
      btnOk.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnOkActionPerformed(evt);
         }
      });

      btnCancel.setText("Cancel");
      btnCancel.setName("btnCancel"); // NOI18N
      btnCancel.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnCancelActionPerformed(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, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addGap(97, 97, 97))
               .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                     .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnCancel))
                     .addComponent(scrollLeft, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(btnInclude)
                     .addComponent(btnExclude))
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
               .addGroup(layout.createSequentialGroup()
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                     .addComponent(btnOk))
                  .addGap(0, 0, Short.MAX_VALUE)))
            .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(jLabel2))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addComponent(scrollLeft, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
               .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE)
               .addGroup(layout.createSequentialGroup()
                  .addComponent(btnInclude)
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                  .addComponent(btnExclude)))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(btnOk)
               .addComponent(btnCancel))
            .addContainerGap())
      );

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

   private void btnIncludeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIncludeActionPerformed
		if(lstPreserved.isSelectionEmpty()) return;
		Object selected = lstPreserved.getSelectedValue();
		int selectedIndex = lstPreserved.getSelectedIndex();
		valuesModel.addElement(selected);
		preservedModel.removeElement(selected);
		lstPreserved.setSelectedIndex(selectedIndex);
		lstPreserved.requestFocus();
   }//GEN-LAST:event_btnIncludeActionPerformed

   private void btnExcludeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcludeActionPerformed
		if(lstValues.isSelectionEmpty()) return;
		Object selected = lstValues.getSelectedValue();
		int selectedIndex = lstValues.getSelectedIndex();
		preservedModel.addElement(selected);
		valuesModel.removeElement(selected);
		lstValues.setSelectedIndex(selectedIndex);
		lstValues.requestFocus();
   }//GEN-LAST:event_btnExcludeActionPerformed

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

	@SuppressWarnings("unchecked")
   private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkActionPerformed
      columns = ArrayListMultimap.create();
		columns.putAll("preserved", preservedModel.getAll());
		columns.putAll("values", valuesModel.getAll());
		dispose();
   }//GEN-LAST:event_btnOkActionPerformed


   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JButton btnCancel;
   private javax.swing.JButton btnExclude;
   private javax.swing.JButton btnInclude;
   private javax.swing.JButton btnOk;
   private javax.swing.JLabel jLabel1;
   private javax.swing.JLabel jLabel2;
   private javax.swing.JScrollPane jScrollPane1;
   private javax.swing.JList lstPreserved;
   private javax.swing.JList lstValues;
   private javax.swing.JScrollPane scrollLeft;
   // End of variables declaration//GEN-END:variables

	@SuppressWarnings("unchecked")
	public ListMultimap<String, String> mapColumns(PivotTableModel model) {
		Collection<String> headers = model.getHeaders();

		preservedModel.updateItems(headers);

		setVisible(true);
		return columns;
	}
}