/* * OptionsDialog.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.BorderLayout; import java.awt.Component; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.io.File; import java.util.ArrayList; import java.util.Vector; import javax.swing.BorderFactory; import javax.swing.GroupLayout; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTabbedPane; import javax.swing.JTable; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.RowSorter; import javax.swing.SortOrder; import javax.swing.border.TitledBorder; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import javax.swing.event.ListSelectionEvent; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableColumnModel; import javax.swing.table.TableModel; import javax.swing.table.TableRowSorter; /** * OptionsDialog - Tools -> Options "Dialog" * */ class OptionsDialog { /** * */ private final Gui mGui; final JPanel optionsPanel = new JPanel(); JDialog optionsDialog ; JTable table; JTextField defaultFileLocation; JTextField defaultKeyStoreLocation; DefaultTableModel model; String keyStoreDirPath; public OptionsDialog (Gui theGui) { ///////////////////////////////////////////////////////////// // Build the Dialog this.mGui = theGui; optionsDialog = new JDialog(mGui.mainFrame, "Options", true); optionsDialog.getContentPane().add(optionsPanel); JTabbedPane jTabbedPane = (JTabbedPane) new MyTabbedPane(); jTabbedPane.setBorder(BorderFactory.createEtchedBorder()); optionsPanel.add(jTabbedPane); // Create the panels - add one per tab // // Default directories JPanel defaultSaveLocationPanel = createDefaultSaveLocationPanel(); jTabbedPane.addTab("Default File Locations", defaultSaveLocationPanel); // KeyStore Search Path MyPanel defaultKeyStoreSearchPathPanel = new MyPanel(); jTabbedPane.addTab("KeyStore Search Path", defaultKeyStoreSearchPathPanel ); // Display the window. optionsDialog.pack(); optionsDialog.setLocationRelativeTo(mGui.mainFrame); optionsDialog.setVisible(true); } public class MyTabbedPane extends JTabbedPane implements ChangeListener { // constructor public MyTabbedPane() { addChangeListener(this); } // Listener for when the selected tab changes // Notify if unsaved changes public void stateChanged(ChangeEvent e) { int paneIndex = getSelectedIndex(); // System.out.println("Pane selected: " + paneIndex); // Default directories pane selected if ( paneIndex == 0 ) { int lastSelectedIndex = 1; // System.out.println("Added: " + mGui.stateObject.getKeyStoreDirAdded() ); // System.out.println("Removed: " + mGui.stateObject.getKeyStoreDirRemoved() ); // Check for changes on pane 0 if ( mGui.stateObject.getKeyStoreDirAdded() || mGui.stateObject.getKeyStoreDirRemoved() ) { int n = JOptionPane.showOptionDialog( frame, "There are changes pending. Continue?" + mGui.newline + mGui.newline, "Changes Pending", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, new String[] {"Yes", "No"}, "Yes"); if ( n == JOptionPane.YES_OPTION ) lastSelectedIndex = paneIndex; else setSelectedIndex(lastSelectedIndex); } } // KeyStore Search Path pane selected if ( paneIndex == 1 ) { int lastSelectedIndex = 0; if ( ! StateObject.pathToDisplay( defaultFileLocation.getText() ).equals( mGui.stateObject.getDefaultEncryptedFilePath() )) { // System.out.println( mGui.stateObject.pathToDisplay( defaultFileLocation.getText()) ); // System.out.println( mGui.stateObject.getDefaultEncryptedFilePath() ); mGui.stateObject.setNewDefaultFilePath(true); } else { mGui.stateObject.setNewDefaultFilePath(false); } if ( ! StateObject.pathToDisplay( defaultKeyStoreLocation.getText() ).equals( mGui.stateObject.getDefaultKeyStoreDir() )) { mGui.stateObject.setNewDefaultKeyStorePath(true); // System.out.println( mGui.stateObject.pathToDisplay( defaultKeyStoreLocation.getText()) ); // System.out.println( mGui.stateObject.getDefaultKeyStoreDir() ); } else { mGui.stateObject.setNewDefaultKeyStorePath(false); } // Check for changes on pane 0 if ( mGui.stateObject.getNewDefaultFilePath() || mGui.stateObject.getNewDefaultKeyStorePath() ) { int n = JOptionPane.showOptionDialog( frame, "There are changes pending. Continue?" + mGui.newline + mGui.newline, "Changes Pending", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, new String[] {"Yes", "No"}, "Yes"); if ( n == JOptionPane.YES_OPTION ) lastSelectedIndex = paneIndex; else setSelectedIndex(lastSelectedIndex); } } } } ///////////////////////////////////////////////////////////// // Default Location for Files Panel public JPanel createDefaultSaveLocationPanel() { JPanel panel = new JPanel(); GroupLayout layout = new GroupLayout(panel); panel.setLayout(layout); layout.setAutoCreateGaps(true); layout.setAutoCreateContainerGaps(true); // Accessed in inner class final JButton jButtonOk = new JButton("Ok"); final JButton jButtonApply = new JButton("Apply"); jButtonOk.setEnabled(false); jButtonApply.setEnabled(false); FlowLayout flowLayoutLeft = new FlowLayout(); flowLayoutLeft.setAlignment(FlowLayout.LEFT); JPanel p1 = new JPanel(); JPanel p2 = new JPanel( flowLayoutLeft ); JPanel p3 = new JPanel( flowLayoutLeft ); JPanel p4 = new JPanel( flowLayoutLeft ); layout.setHorizontalGroup( layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(p1) .addComponent(p2) .addComponent(p3) .addComponent(p4) ) ); layout.setVerticalGroup( layout.createSequentialGroup() .addComponent(p1) .addComponent(p2) .addComponent(p3) .addComponent(p4) ); ///////////////////////////////////////////////////////////// // p1 - Title JLabel jlabel = new JLabel("Default Directories"); p1.add(jlabel); // p2 - Default Directories for PDS Files JPanel p2SubPanel = new JPanel(flowLayoutLeft); p2.add(p2SubPanel); JPanel p2SubPanel2 = new JPanel(new GridLayout(2,2)); p2SubPanel.add(p2SubPanel2); p2.setBorder(new TitledBorder("PDS Files")); p2SubPanel2.add(new JLabel( "Default Directory for Encrypted Files: "), BorderLayout.WEST); defaultFileLocation = new JTextField( 40 ); defaultFileLocation.setText(mGui.stateObject.getDefaultEncryptedFilePath()); defaultFileLocation.setCaretPosition(0); defaultFileLocation.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { defaultFileLocation.setText( StateObject.pathToDisplay( defaultFileLocation.getText() )); defaultFileLocation.repaint(); jButtonOk.setEnabled(true); jButtonApply.setEnabled(true); } }); p2SubPanel2.add(defaultFileLocation, BorderLayout.WEST); JButton iconOpen = new JButton(Gui.createImageIcon("images/folder.gif", 28)); iconOpen.setFocusable(false); Gui.enterPressesWhenFocused(iconOpen); iconOpen.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { if ( openMenuItemActionPerformed(evt, 1) ) { jButtonOk.setEnabled(true); jButtonApply.setEnabled(true); defaultFileLocation.setCaretPosition(0); } } }); iconOpen.setToolTipText("Open"); p2SubPanel.add(iconOpen, BorderLayout.EAST); // p3 - Default Directories for KeyStores JPanel p3SubPanel = new JPanel(flowLayoutLeft); p3.add(p3SubPanel); JPanel p3SubPanel2 = new JPanel(new GridLayout(2,2)); p3SubPanel.add(p3SubPanel2); p3.setBorder(new TitledBorder("KeyStore Files")); p3SubPanel2.add(new JLabel( "Default Directory for KeyStores: "), BorderLayout.WEST); defaultKeyStoreLocation = new JTextField( 40 ); defaultKeyStoreLocation.setText(mGui.stateObject.getDefaultKeyStoreDir()); defaultKeyStoreLocation.setCaretPosition(0); defaultKeyStoreLocation.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { defaultKeyStoreLocation.setText( StateObject.pathToDisplay( defaultKeyStoreLocation.getText() )); defaultKeyStoreLocation.repaint(); jButtonOk.setEnabled(true); jButtonApply.setEnabled(true); } }); p3SubPanel2.add(defaultKeyStoreLocation, BorderLayout.WEST); JButton iconOpen2 = new JButton(Gui.createImageIcon("images/folder.gif", 28)); iconOpen2.setFocusable(false); Gui.enterPressesWhenFocused(iconOpen2); iconOpen2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { if ( openMenuItemActionPerformed(evt, 2) ) { jButtonOk.setEnabled(true); jButtonApply.setEnabled(true); defaultKeyStoreLocation.setCaretPosition(0); } } }); iconOpen2.setToolTipText("Open"); p3SubPanel.add(iconOpen2, BorderLayout.EAST); // p4 - Buttons - OK, Cancel & Apply // // JButtonOk jButtonApply // Defined earlier - accessed later in a subclass jButtonOk.setMnemonic(KeyEvent.VK_O); Gui.enterPressesWhenFocused(jButtonOk); jButtonOk.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { new OkAndApply( false ); // Reset tab settings - both tabs //mGui.stateObject.setNewDefaultFilePath(false); //mGui.stateObject.setNewDefaultKeyStorePath(false); mGui.stateObject.setKeyStoreDirRemoved(false); mGui.stateObject.setKeyStoreDirAdded(false); optionsDialog.dispose(); } }); p4.add(jButtonOk); JButton jButtonCancel = new JButton("Cancel"); jButtonCancel.setMnemonic(KeyEvent.VK_C); Gui.enterPressesWhenFocused(jButtonCancel); jButtonCancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { // System.out.println("Canceling..."); // Reset tab settings - both tabs mGui.stateObject.setNewDefaultFilePath(false); mGui.stateObject.setNewDefaultKeyStorePath(false); mGui.stateObject.setKeyStoreDirRemoved(false); mGui.stateObject.setKeyStoreDirAdded(false); optionsDialog.dispose(); } }); p4.add(jButtonCancel); // JButtonApply jButtonApply // Defined earlier - accessed later in a subclass Gui.enterPressesWhenFocused(jButtonApply); jButtonApply.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { new OkAndApply( true ); jButtonApply.setEnabled(false); } }); p4.add(jButtonApply); ///////////////////////////////////////////////////////////// // Finish up return panel; } // Used for both OK and Apply Button selections // class OkAndApply { public OkAndApply( boolean addToModel ) { // Default Location for Encrypted Files // String dirName = ( StateObject.pathToDisplay( defaultFileLocation.getText() )); try { if ( ! new java.io.File( dirName ).exists() ) { JFrame frame = new JFrame(); int n = JOptionPane.showOptionDialog( frame, "The Encrypted Files directory does not exist." + mGui.newline + mGui.newline + dirName + mGui.newline + mGui.newline + "Would you like to create it?" + mGui.newline + mGui.newline, "Directory does not exist", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, new String[] {"Yes", "No"}, "Yes"); if ( n != 0 ) { // System.out.println("Canceling..."); return; } else { // System.out.println("Creating the directory..."); boolean success = (new File( dirName )).mkdirs(); if (success) { // System.out.println("Directory: " + dirName + " created"); mGui.stateObject.setDefaultEncryptedFilePath( dirName ); } else { JOptionPane.showMessageDialog( frame, "Unable to Create the Directory:" + mGui.newline + dirName + mGui.newline + mGui.newline + "Please check the permissions or the path" + mGui.newline + "and try again.", "Unable to Create the Directory", JOptionPane.ERROR_MESSAGE); return; } } } } catch (Exception e) { System.out.println("Unable to get fully pathed filename: " + e); return; } mGui.stateObject.setDefaultEncryptedFilePath( StateObject.pathToDisplay( defaultFileLocation.getText() )); // Default Location for KeyStore Files // keyStoreDirPath = ( StateObject.pathToDisplay( defaultKeyStoreLocation.getText() )); try { if ( ! new java.io.File( keyStoreDirPath ).exists() ) { JFrame frame = new JFrame(); int n = JOptionPane.showOptionDialog( frame, "The KeyStore directory does not exist." + mGui.newline + mGui.newline + keyStoreDirPath + mGui.newline + mGui.newline + "Would you like to create it?" + mGui.newline + mGui.newline, "Directory does not exist", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, new String[] {"Yes", "No"}, "Yes"); if ( n != 0 ) { // System.out.println("Canceling..."); return; } else { // System.out.println("Creating the directory..."); boolean success = (new File( keyStoreDirPath )).mkdirs(); if (success) { // System.out.println("Directory: " + keyStoreDirPath + " created"); } else { JOptionPane.showMessageDialog( frame, "Unable to Create the Directory:" + mGui.newline + keyStoreDirPath + mGui.newline + mGui.newline + "Please check the permissions or the path" + mGui.newline + "and try again.", "Unable to Create the Directory", JOptionPane.ERROR_MESSAGE); return; } } } } catch (Exception e) { System.out.println("Unable to get fully pathed filename: " + e); return; } // Redefine the new Default KeyStore Location mGui.stateObject.setDefaultKeyStoreDir( StateObject.pathToDisplay( defaultKeyStoreLocation.getText() )); // Ensure the Default KeyStore Path is in the KSP // mGui.stateObject.addPathToKeyStoreSearchPath( keyStoreDirPath ); // Reset tab settings mGui.stateObject.setNewDefaultFilePath(false); mGui.stateObject.setNewDefaultKeyStorePath(false); // Save mGui.stateObject.saveState(); if ( addToModel ) { // Since the dialog remains open, update the model in // in case the other tab is selected. // System.out.println("Before: " + keyStoreDirPath); keyStoreDirPath = StateObject.pathToDisplay( keyStoreDirPath ); // System.out.println("After: " + keyStoreDirPath); // Check to see if it is in the model first int i = model.getRowCount(); boolean match = false; while ( i > 0 ) { --i; // System.out.println ( (String) model.getValueAt(i, 0) ); if ( keyStoreDirPath.equals( model.getValueAt(i, 0) ) ) { // System.out.println("Duplicate"); match = true; } } if (! match ) { // System.out.println ("Adding to model: " + keyStoreDirPath ); model.insertRow(model.getRowCount(), new Object[] { StateObject.pathToDisplay( keyStoreDirPath ) }); } } } } ///////////////////////////////////////////////////////////// // KeyStore Search Path Panel /* * There are two different actions: * - Add/Remove a Directory * - Ok/Apply Changes * * The Add/Remove Directory sequence Adds or Removes a Directory * from the current model. Duplicates are not allowed into the * mode. The StateObject instance is not changed. * * The Ok/Apply sequence sends the model (after conversion to a * Vector) to the StateObject instance to be set as the * new KeyStore Search Path (checks for duplicate and format * conversions are performed on that end, as is saving state). * * The model contains the system specific path (i.e. C:\Windows). * The StateObject instance contains the PDS-DOS path (i.e. /C:/Windows). * */ public class MyPanel extends JPanel { // constructor public MyPanel() { go(); } // called from listener when selected tab changes public void redrawPanel() { // System.out.println("Redrawing the panel"); table.setModel(model); table.revalidate(); table.setFillsViewportHeight(true); table.getPreferredScrollableViewportSize(); table.repaint(); revalidate(); repaint(); optionsDialog.setSize(optionsDialog.getPreferredSize()); optionsDialog.repaint(); } public void go() { final JButton jButtonOk = new JButton("Ok"); final JButton jButtonApply = new JButton("Apply"); final JButton jButtonRemove = new JButton("Remove Directory"); ///////////////////////////////////////////////////////////// // Build the Dialog GroupLayout layout = new GroupLayout(this); setLayout(layout); layout.setAutoCreateGaps(true); layout.setAutoCreateContainerGaps(true); FlowLayout flowLayoutLeft = new FlowLayout(); flowLayoutLeft.setAlignment(FlowLayout.LEFT); JPanel p1 = new JPanel(); JPanel p2 = new JPanel(flowLayoutLeft); JPanel p3 = new JPanel(); JPanel p4 = new JPanel(); JPanel p5 = new JPanel(); layout.setHorizontalGroup( layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(p1) .addComponent(p2) .addComponent(p3) .addComponent(p4) .addComponent(p5) ) ); layout.setVerticalGroup( layout.createSequentialGroup() .addComponent(p1) .addComponent(p2) .addComponent(p3) .addComponent(p4) .addComponent(p5) ); ///////////////////////////////////////////////////////////// // p1 - Title JLabel jlabel = new JLabel("KeyStore Search Path"); p1.add(jlabel); // p2 - Paths JTextArea textArea = new JTextArea(); textArea.setEditable(false); textArea.setOpaque(false); textArea.append("Each encrypted file contains the path to the KeyStore that was last used." + mGui.newline); textArea.append("When decrypting file, if the KeyStore specified in the file is found, it will be used." + mGui.newline); textArea.append("If that KeyStore is not found, a search for the KeyStore will be made in the directories below."); p2.add(textArea); // p2 - Paths JPanel p3SubPanel = new JPanel(new FlowLayout()); p3.add(p3SubPanel); p3.setBorder(new TitledBorder("Directories PDS will search for KeyStores")); Vector searchPaths = mGui.stateObject.getKeyStoreSearchPath(); model = new DefaultTableModel(); model.addColumn("Directories to be Searched", searchPaths); 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 ); jButtonRemove.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); java.util.List sortKeys = new ArrayList(); sortKeys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING)); sorter.setSortKeys(sortKeys); sorter.sort(); JScrollPane scrollPane = new JScrollPane(table); TableColumnModel colModel = table.getColumnModel(); for(int j = 0; j < colModel.getColumnCount(); j++) colModel.getColumn(j).setCellRenderer(new RowRenderer()); p3SubPanel.add(scrollPane); // p4 - Buttons - Add & Remove Directory JButton jButtonAdd = new JButton("Add New Directory"); jButtonAdd.setMnemonic(KeyEvent.VK_N); Gui.enterPressesWhenFocused(jButtonAdd); jButtonAdd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { // System.out.println("Adding KeyStore Search Dir"); if ( openMenuItemActionPerformed(evt, 3) ) { mGui.stateObject.setKeyStoreDirAdded(true); jButtonOk.setEnabled(true); jButtonApply.setEnabled(true); redrawPanel(); } } }); p4.add(jButtonAdd); // jButton created earlier, as listener on another object enables this button jButtonRemove.setMnemonic(KeyEvent.VK_R); Gui.enterPressesWhenFocused(jButtonRemove); jButtonRemove.setEnabled(false); jButtonRemove.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // System.out.println("Deleting KeyStore Search Dir"); int selected = table.getSelectedRow(); // System.out.println ("Row:Value: " + selected + ":" + (String) table.getValueAt(selected, 0) ); String pathToDelete = (String) table.getValueAt(selected, 0); // System.out.println("Lines in Model before: " + model.getRowCount()); // int i = model.getRowCount(); // while ( i > 0 ) { // System.out.println ( (String) model.getValueAt(--i, 0) ); // } // Update the model // System.out.println("Updating the model."); // Check to see if it is in the model first int j = model.getRowCount(); int row = -1; while ( j > 0 ) { --j; // System.out.println ( "Comparing: " + pathToDelete + " : " + (String) model.getValueAt(j, 0) ); if ( pathToDelete.equals( model.getValueAt(j, 0) ) ) { // System.out.println("Match"); row = j; } } if ( row >= 0 ) { // System.out.println("Removing row: " + row); model.removeRow(row); } else { System.out.println("Error: Path to delete not found in model: " + pathToDelete); } // System.out.println("Lines in Model after: " + model.getRowCount()); // int k = model.getRowCount(); // while ( k > 0 ) { // System.out.println ( (String) model.getValueAt(--k, 0) ); // } mGui.stateObject.setKeyStoreDirRemoved(true); jButtonRemove.setEnabled(false); jButtonOk.setEnabled(true); jButtonApply.setEnabled(true); redrawPanel(); } }); p4.add(jButtonRemove); // p5 - Buttons - OK, Cancel & Apply // // jButton created earlier as listener on another object enables this button jButtonOk.setMnemonic(KeyEvent.VK_O); Gui.enterPressesWhenFocused(jButtonOk); jButtonOk.setEnabled(false); jButtonOk.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { mGui.stateObject.setKeyStoreSearchPath( modelToStringVector( model ) ); // Reset this tab mGui.stateObject.setKeyStoreDirAdded(false); mGui.stateObject.setKeyStoreDirRemoved(false); // Reset the other tab mGui.stateObject.setNewDefaultFilePath(false); mGui.stateObject.setNewDefaultKeyStorePath(false); mGui.stateObject.saveState(); optionsDialog.dispose(); } }); p5.add(jButtonOk); 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..."); // Reset this tab mGui.stateObject.setKeyStoreDirAdded(false); mGui.stateObject.setKeyStoreDirRemoved(false); // Reset the other tab mGui.stateObject.setNewDefaultFilePath(false); mGui.stateObject.setNewDefaultKeyStorePath(false); optionsDialog.dispose(); } }); p5.add(jButtonCancel); // jButton created earlier as listener on another // object enables this button Gui.enterPressesWhenFocused(jButtonApply); jButtonApply.setMnemonic(KeyEvent.VK_A); jButtonApply.setEnabled(false); jButtonApply.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { mGui.stateObject.setKeyStoreSearchPath( modelToStringVector( model ) ); mGui.stateObject.setKeyStoreDirAdded(false); mGui.stateObject.setKeyStoreDirRemoved(false); mGui.stateObject.saveState(); jButtonApply.setEnabled(false); } }); p5.add(jButtonApply); } } Vector modelToStringVector( DefaultTableModel model ) { Vector stringVector = new Vector(); int i = model.getRowCount(); while ( i > 0 ) { --i; // System.out.println ("Adding: " + (String) model.getValueAt(i, 0) ); stringVector.add( (String) model.getValueAt(i, 0) ); } return stringVector; } // RowRenderer from: http://www.javalobby.org/java/forums/t84905.html // class RowRenderer extends DefaultTableCellRenderer { public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); setToolTipText((String)table.getValueAt(row, 0)); return this; } } //////////////////////////////////////////////////////// /** * Listeners for the OptionsDialog inner class * */ //////////////////////////////////////////////////////// // JFileChooser for a Directory // JFrame chooserFrame = new JFrame(); JFrame frame = new JFrame(); private boolean openMenuItemActionPerformed(ActionEvent evt, int action) { JFileChooser fileChooser = new JFileChooser ( mGui.stateObject.getDefaultEncryptedFilePath() ); if ( action != 1 ) { fileChooser = new JFileChooser ( mGui.stateObject.getDefaultKeyStoreDir() ); } fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); fileChooser.setDialogTitle( "Directory" ); fileChooser.setApproveButtonText("Select"); int returnVal = fileChooser.showOpenDialog(chooserFrame); String dirName; if ( returnVal == 0 ) { if ( action == 1 ) { // System.out.print("Encrypted File Default Path" + newline); // Get Absolute Path: dirName = ( StateObject.pathToDisplay( fileChooser.getSelectedFile().getPath() + File.separator )); // System.out.println("Default Dir: " + dirName); defaultFileLocation.setText( dirName ); defaultFileLocation.setEnabled(true); defaultFileLocation.repaint(); return true; } else if ( action == 2 ) { // System.out.print("KeyStore Default Path" + newline); // Get Absolute Path: dirName = ( StateObject.pathToDisplay( fileChooser.getSelectedFile().getPath() + File.separator )); // System.out.println("Default Dir: " + dirName); defaultKeyStoreLocation.setText( dirName ); defaultKeyStoreLocation.setEnabled(true); defaultKeyStoreLocation.repaint(); return true; } else if ( action == 3 ) { // System.out.print("Paths in the KeyStore Search Path" + newline); // Get Absolute Path: String newKeyStorePath = ( StateObject.pathToDisplay( fileChooser.getSelectedFile().getPath() + File.separator )); // System.out.print("Dir to add: " + newKeyStorePath + newline); // System.out.println("Lines in Model before: " + model.getRowCount()); // Check to see if it is in the model first int i = model.getRowCount(); boolean match = false; while ( i > 0 ) { --i; // System.out.println ( (String) model.getValueAt(i, 0) ); if ( newKeyStorePath.equals( model.getValueAt(i, 0) ) ) { // System.out.println("Duplicate"); match = true; } } try { if ( ! new java.io.File( newKeyStorePath ).exists() ) { JFrame frame = new JFrame(); int n = JOptionPane.showOptionDialog( frame, "The KeyStore directory does not exist." + mGui.newline + mGui.newline + newKeyStorePath + mGui.newline + mGui.newline + "Would you like to create it?" + mGui.newline + mGui.newline, "Directory does not exist", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, new String[] {"Yes", "No"}, "Yes"); if ( n != 0 ) { // System.out.println("Canceling..."); return true; } else { // System.out.println("Creating the directory..."); boolean success = (new File( newKeyStorePath )).mkdirs(); if (! success) { JOptionPane.showMessageDialog( frame, "Unable to Create the Directory:" + mGui.newline + newKeyStorePath + mGui.newline + mGui.newline + "Please check the permissions or the path" + mGui.newline + "and try again.", "Unable to Create the Directory", JOptionPane.ERROR_MESSAGE); return false; } } } } catch (Exception e) { System.out.println("Unable to get fully pathed filename: " + e); return false; } // Add it, if not already there if ( ! match ) { // System.out.println("Adding: " + newKeyStorePath ); model.insertRow(model.getRowCount(), new Object[] { newKeyStorePath }); } // System.out.println("Lines in Model after: " + model.getRowCount()); // i = model.getRowCount(); // while ( i > 0 ) { // System.out.println ( (String) model.getValueAt(--i, 0) ); // } table.setModel(model); table.revalidate(); table.setFillsViewportHeight(true); table.getPreferredScrollableViewportSize(); table.repaint(); return true; } else { System.out.print("Unknown action requested: " + action + mGui.newline); return false; } } else if ( returnVal == 1 ) { // System.out.print("Canceling..." + mGui.newline); return false; } else { System.out.print("Error: " + returnVal + mGui.newline); return false; } } }