/* * MyInternalFrame.java * * * ==================================================== Professional Data Security (PDS) http://crypto.brettlee.com ==================================================== Copyright (c) 2009-2011, Brett Lee All rights reserved. Portions Copyright (C) 1995-2008, Sun Microsystems, Inc. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the ORGANIZATION nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ============================================================================= */ package com.brettlee.crypto; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.ArrayList; import java.util.Comparator; import java.util.Date; import javax.swing.GroupLayout; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.RowSorter; import javax.swing.SortOrder; import javax.swing.border.TitledBorder; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; import javax.swing.table.TableRowSorter; class KeySelectorDialog extends JDialog { /** * */ private final CreateFileDialog createFileDialog; final JButton jButtonSelect = new JButton("Select"); public KeySelectorDialog(CreateFileDialog createFileDialog, JFrame frame, boolean modal, CryptoFile cryptoFile) { super(frame, modal); this.createFileDialog = createFileDialog; if ( ! ( initComponents( cryptoFile ))) { return; } pack(); setLocationRelativeTo(frame); setTitle("Professional Data Security (PDS) - Select Key from KeyStore"); setVisible(true); } boolean initComponents ( final CryptoFile cryptoFile ) { final JTable table; String ksElements[][] = null; // System.out.println("Selecting key from KeyStore..."); ///////////////////////////////////////////////////////////// // Build the Dialog JPanel contentPane = new JPanel(); GroupLayout layout = new GroupLayout(contentPane); contentPane.setLayout(layout); layout.setAutoCreateGaps(true); layout.setAutoCreateContainerGaps(true); add(contentPane); FlowLayout flowLayoutLeft = new FlowLayout(); flowLayoutLeft.setAlignment(FlowLayout.LEFT); JPanel p1 = new JPanel(); JPanel p2 = new JPanel( flowLayoutLeft ); JPanel p3 = new JPanel(); layout.setHorizontalGroup( layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(p1) .addComponent(p2) .addComponent(p3) ) ); layout.setVerticalGroup( layout.createSequentialGroup() .addComponent(p1) .addComponent(p2) .addComponent(p3) ); ///////////////////////////////////////////////////////////// // p1 - Title JLabel jlabel = new JLabel("Select Key from KeyStore"); p1.add(jlabel); // p2 - KeyStore name JPanel p2SubPanel = new JPanel(new FlowLayout()); p2.add(p2SubPanel); p2.setBorder(new TitledBorder("Select a Key")); // Need to get list of keys from KeyStore try { ksElements = new CryptoKeyStore().getKeyStore( cryptoFile.getKsName(), cryptoFile.getKsPass() ); } catch (Exception ex) { // System.out.println("Error getting KeyStore: " + ex); } if ( ksElements == null ) { // System.out.println("No keys in the store, or invalid passphrase provided."); StateObject.setFileChooserCancel(true); JOptionPane.showMessageDialog( this.createFileDialog.mGui.mainFrame, "Unable to recover any Keys from this KeyStore." + this.createFileDialog.mGui.newline + this.createFileDialog.mGui.newline + "Try again with different credentials or" + this.createFileDialog.mGui.newline + "select a different KeyStore.", "No Keys Recovered", JOptionPane.WARNING_MESSAGE); return false; } // System.out.println("Listing keys in Gui.java"); // if ( ksElements.length > 0 ) { // for (int count = 0, total = ksElements.length; count < total; ++count ) { // System.out.print("Created: " + ksElements[count][0] + "\t\t" ); // System.out.println("Alias: " + ksElements[count][1]); // } // } String col [] = {"Created", "Key Alias"}; DefaultTableModel model = new DefaultTableModel(ksElements,col); table = new JTable( model ) { public boolean isCellEditable(int row, int column) { return false; } public Dimension getPreferredScrollableViewportSize() { Dimension size = super.getPreferredScrollableViewportSize(); return new Dimension(size.width, Math.min(getPreferredSize().height, 110)); } public void valueChanged(ListSelectionEvent e) { super.valueChanged( e ); jButtonSelect.setEnabled(true); } }; table.setAutoResizeMode ( JTable.AUTO_RESIZE_ALL_COLUMNS ); table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); table.setRowSelectionAllowed(true); table.setAutoCreateRowSorter(true); table.setUpdateSelectionOnSort(true); TableRowSorter sorter = new TableRowSorter(table.getModel()); table.setRowSorter(sorter); Comparator dateComparator = new Comparator() { public int compare(String s1, String s2) { try { Date d1 = new Date(s1); Date d2 = new Date(s2); return d1.compareTo(d2); } catch (Exception e) { System.out.println("Error comparing dates in JTable"); return 0; } } }; sorter.setComparator(0, dateComparator); java.util.List sortKeys = new ArrayList(); sortKeys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING)); sorter.setSortKeys(sortKeys); sorter.sort(); // Add ability to 2x-click one of the rows table.getSelectionModel().addListSelectionListener(new ListSelectionListener() { // class SharedListSelectionHandler implements ListSelectionListener { // public void valueChanged(ListSelectionEvent e) { // ListSelectionModel lsm = (ListSelectionModel)e.getSource(); // } // } @Override public void valueChanged(ListSelectionEvent arg0) { } } ); MouseListener mouseListener = new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { // (Code duplicated below...) // System.out.println("Getting selected key"); int selected = table.getSelectedRow(); // System.out.println("Row selected was: " + selected); // Set the Key Alias in the new CryptoFile object // System.out.println("table.getValueAt: " + table.getValueAt(selected,1).toString()); cryptoFile.setKeyAlias(table.getValueAt(selected,1).toString()); // System.out.println("Key Alias: " + cryptoFile.getKeyAlias() ); KeySelectorDialog.this.createFileDialog.keyAliasNameField.setText( cryptoFile.getKeyAlias() ); KeySelectorDialog.this.createFileDialog.keyAliasNameField.repaint(); // System.out.println("Closing Key Selector Dialog"); setVisible(false); dispose(); } } }; table.addMouseListener(mouseListener); JScrollPane scrollPane = new JScrollPane(table); p2SubPanel.add(scrollPane); // p3 - Select & Cancel Buttons jButtonSelect.setMnemonic(KeyEvent.VK_S); Gui.enterPressesWhenFocused(jButtonSelect); jButtonSelect.setEnabled(false); jButtonSelect.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { // System.out.println("Getting selected key"); int selected = table.getSelectedRow(); // System.out.println("Row selected was: " + selected); // Set the Key Alias in the new CryptoFile object // System.out.println("table.getValueAt: " + table.getValueAt(selected,1).toString()); cryptoFile.setKeyAlias(table.getValueAt(selected,1).toString()); // System.out.println("Key Alias: " + cryptoFile.getKeyAlias() ); KeySelectorDialog.this.createFileDialog.keyAliasNameField.setText( cryptoFile.getKeyAlias() ); KeySelectorDialog.this.createFileDialog.keyAliasNameField.repaint(); // System.out.println("Closing Key Selector Dialog"); setVisible(false); dispose(); } }); p3.add(jButtonSelect); JButton jButtonCancel = new JButton("Cancel"); jButtonCancel.setMnemonic(KeyEvent.VK_C); Gui.enterPressesWhenFocused(jButtonCancel); jButtonCancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // System.out.println("Canceling..."); StateObject.setFileChooserCancel(true); setVisible(false); dispose(); } }); p3.add(jButtonCancel); return true; } }