/* Copyright (c) 2009 The Regents of the University of California. All rights reserved. Permission is hereby granted, without written agreement and without license or royalty fees, to use, copy, modify, and distribute this software and its documentation for any purpose, provided that the above copyright notice and the following two paragraphs appear in all copies of this software. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.. */ /* * DeviceDialog.java * * Created on Nov 26, 2009, 3:48:04 PM */ package org.clothocad.tool.spectacles.ui.frames; import javax.swing.JOptionPane; /** * DeviceDialog allows the user to select which devices * are displayed when importing Eugene files. * @author Richard */ public class DeviceDialog extends javax.swing.JDialog { /** Creates new form DeviceDialog */ public DeviceDialog(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); _wsFrame = (WorkspaceFrame) parent; _numDevices = 0; } /** * Takes an array of strings as an Object array and * presents a dialog with the list of device names. * May also take a list of indices for devices that * already have open tabs. * @param deviceNames the array of strings as Objects. * @param openDevices list of indices; may be null. */ public void chooseDevices(Object[] deviceNames, int[] openDevices) { deviceList.setListData(deviceNames); _numDevices = deviceNames.length; if (_numDevices == _wsFrame.getMainJTPTabCount() || _wsFrame.getMainJTPTabCount() < 1) { selectAll(); } else if (openDevices != null) { deviceList.setSelectedIndices(openDevices); } setLocationRelativeTo(_wsFrame); setVisible(true); } /** * Opens all selected tabs in the parent WorkspaceFrame. */ private void filterDevices() { for (int i = 0; i < _numDevices; i++) { if (deviceList.isSelectedIndex(i)) { _wsFrame.openTab((String) deviceList.getModel().getElementAt(i)); } else { _wsFrame.closeTab((String) deviceList.getModel().getElementAt(i)); } } } /** * Selects all items in the device list. */ private void selectAll() { deviceList.setSelectionInterval(0, _numDevices); } /** * De-selects all items in the device list. */ private void selectNone() { deviceList.setSelectedIndices(_emptyIntArray); } /** 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() { deviceListScrollPane = new javax.swing.JScrollPane(); deviceList = new javax.swing.JList(); selectAllButton = new javax.swing.JButton(); selectNoneButton = new javax.swing.JButton(); doneButton = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle(org.openide.util.NbBundle.getMessage(DeviceDialog.class, "DeviceDialog.title")); // NOI18N setAlwaysOnTop(true); setResizable(false); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosed(java.awt.event.WindowEvent evt) { formWindowClosed(evt); } }); deviceListScrollPane.setViewportView(deviceList); selectAllButton.setMnemonic('a'); selectAllButton.setText(org.openide.util.NbBundle.getMessage(DeviceDialog.class, "DeviceDialog.selectAllButton.text")); // NOI18N selectAllButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { selectAllButtonActionPerformed(evt); } }); selectNoneButton.setMnemonic('n'); selectNoneButton.setText(org.openide.util.NbBundle.getMessage(DeviceDialog.class, "DeviceDialog.selectNoneButton.text")); // NOI18N selectNoneButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { selectNoneButtonActionPerformed(evt); } }); doneButton.setMnemonic('d'); doneButton.setText(org.openide.util.NbBundle.getMessage(DeviceDialog.class, "DeviceDialog.doneButton.text")); // NOI18N doneButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { doneButtonActionPerformed(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() .addComponent(deviceListScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(doneButton, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE) .addComponent(selectNoneButton, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE) .addComponent(selectAllButton, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(deviceListScrollPane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(selectAllButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(selectNoneButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 256, Short.MAX_VALUE) .addComponent(doneButton))) .addContainerGap()) ); pack(); }// </editor-fold>//GEN-END:initComponents private void doneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doneButtonActionPerformed if (deviceList.getSelectedIndices().length > 25) { JOptionPane.showMessageDialog(this, "Maximum number of open tabs allowed is 25.", "Warning", JOptionPane.WARNING_MESSAGE); return; } filterDevices(); dispose(); }//GEN-LAST:event_doneButtonActionPerformed private void selectAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectAllButtonActionPerformed selectAll(); }//GEN-LAST:event_selectAllButtonActionPerformed private void selectNoneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectNoneButtonActionPerformed selectNone(); }//GEN-LAST:event_selectNoneButtonActionPerformed private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed if ( _wsFrame.getMainJTPTabCount() < 1) { _wsFrame.openTab((String) deviceList.getModel().getElementAt(0)); } }//GEN-LAST:event_formWindowClosed /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { DeviceDialog dialog = new DeviceDialog(new javax.swing.JFrame(), true); dialog.addWindowListener(new java.awt.event.WindowAdapter() { @Override public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0); } }); dialog.setVisible(true); } }); } private WorkspaceFrame _wsFrame; private int _numDevices; private static int[] _emptyIntArray = new int[0]; // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JList deviceList; private javax.swing.JScrollPane deviceListScrollPane; private javax.swing.JButton doneButton; private javax.swing.JButton selectAllButton; private javax.swing.JButton selectNoneButton; // End of variables declaration//GEN-END:variables }