/*
* Copyright (c) 2009 The Jackson Laboratory
*
* This software was developed by Gary Churchill's Lab at The Jackson
* Laboratory (see http://research.jax.org/faculty/churchill).
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software. If not, see <http://www.gnu.org/licenses/>.
*/
package org.jax.qtl.cross.gui;
import java.awt.Frame;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import org.jax.qtl.cross.Cross;
import org.jax.util.concurrent.SettableFuture;
/**
* A simple dialog for selecting from the available crosses in the
* current project.
* @author <A HREF="mailto:keith.sheppard@jax.org">Keith Sheppard</A>
*/
public class CrossSelectionDialog extends javax.swing.JDialog
{
/**
* every {@link java.io.Serializable} is supposed to have one of these
*/
private static final long serialVersionUID = -4723273972903669156L;
/**
* our logger
*/
private static final Logger LOG = Logger.getLogger(
CrossSelectionDialog.class.getName());
/**
* the future selected cross used for {@link #getSelectedCross()}
*/
private final SettableFuture<Cross> futureSelectedCross =
new SettableFuture<Cross>();
/**
* Constructor
* @param parentFrame
* the parent frame for this dialog
* @param title
* the title for the selection dialog
* @param candidateCrosses
* the crosses that the user can select from
*/
public CrossSelectionDialog(
Frame parentFrame,
String title,
Cross[] candidateCrosses)
{
this(parentFrame, title, candidateCrosses, null);
}
/**
* Constructor
* @param parentFrame
* the parent frame for this dialog
* @param title
* the title for the selection dialog
* @param candidateCrosses
* the crosses that the user can select from
* @param defaultSelection
* the cross that is initially selected
*/
public CrossSelectionDialog(
Frame parentFrame,
String title,
Cross[] candidateCrosses,
Cross defaultSelection)
{
super(parentFrame, title, true);
this.initComponents();
this.crossComboBox.setModel(new DefaultComboBoxModel(
candidateCrosses));
if(defaultSelection != null)
{
this.crossComboBox.setSelectedItem(defaultSelection);
}
}
/**
* Get the selected cross. This function will block until the user
* OKs a selection or cancels
* @return
* the selected cross or null if the user cancels selection
*/
public Cross getSelectedCross()
{
try
{
return this.futureSelectedCross.get();
}
catch(Exception ex)
{
LOG.log(Level.SEVERE,
"caught an exception waiting for future cross selection",
ex);
return null;
}
}
/**
* 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("all")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
actionPanel = new javax.swing.JPanel();
okButton = new javax.swing.JButton();
cancelButton = new javax.swing.JButton();
crossLabel = new javax.swing.JLabel();
crossComboBox = new javax.swing.JComboBox();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
okButton.setText("OK");
okButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okButtonActionPerformed(evt);
}
});
actionPanel.add(okButton);
cancelButton.setText("Cancel");
cancelButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelButtonActionPerformed(evt);
}
});
actionPanel.add(cancelButton);
crossLabel.setText("Cross:");
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(actionPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(crossLabel)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(crossComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(215, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(crossLabel)
.add(crossComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 8, Short.MAX_VALUE)
.add(actionPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
try
{
this.futureSelectedCross.set(
(Cross)this.crossComboBox.getSelectedItem());
}
catch(Exception ex)
{
LOG.log(Level.SEVERE,
"failed to set future cross value",
ex);
}
finally
{
this.dispose();
}
}//GEN-LAST:event_okButtonActionPerformed
private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
try
{
this.futureSelectedCross.set(null);
}
catch(Exception ex)
{
LOG.log(Level.SEVERE,
"failed to set future cross value",
ex);
}
finally
{
this.dispose();
}
}//GEN-LAST:event_cancelButtonActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel actionPanel;
private javax.swing.JButton cancelButton;
private javax.swing.JComboBox crossComboBox;
private javax.swing.JLabel crossLabel;
private javax.swing.JButton okButton;
// End of variables declaration//GEN-END:variables
}