Open and create key stores in an internal frame.
[jkeytool.git] / src / net / pterodactylus / jkeytool / gui / MainFrame.java
index cfbccee..cd6052a 100644 (file)
@@ -24,13 +24,16 @@ import java.awt.event.InputEvent;
 import java.awt.event.KeyEvent;
 import java.awt.event.WindowEvent;
 import java.awt.event.WindowListener;
+import java.beans.PropertyVetoException;
 import java.io.File;
 import java.security.KeyStore;
 
 import javax.swing.AbstractAction;
 import javax.swing.Action;
+import javax.swing.JDesktopPane;
 import javax.swing.JFileChooser;
 import javax.swing.JFrame;
+import javax.swing.JInternalFrame;
 import javax.swing.JMenu;
 import javax.swing.JMenuBar;
 import javax.swing.JOptionPane;
@@ -53,9 +56,15 @@ public class MainFrame implements CoreListener, WindowListener {
        /** The main frame. */
        private final JFrame mainFrame;
 
+       /** The main frame’s desktop. */
+       private final JDesktopPane desktop;
+
        /** The jkeytool core. */
        private final Core core;
 
+       /** The “File -> Quit” action. */
+       private Action fileQuitAction;
+
        /** The “open keystore” action. */
        private Action openKeystoreAction;
 
@@ -74,6 +83,8 @@ public class MainFrame implements CoreListener, WindowListener {
                constructActions();
                mainFrame.setJMenuBar(constructMenuBar());
                mainFrame.addWindowListener(this);
+               desktop = new JDesktopPane();
+               mainFrame.setContentPane(desktop);
                mainFrame.pack();
        }
 
@@ -96,11 +107,24 @@ public class MainFrame implements CoreListener, WindowListener {
         * Constructs all actions.
         */
        private void constructActions() {
+               fileQuitAction = new AbstractAction("Quit") {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent actionEvent) {
+                               actionFileQuit();
+                       }
+               };
+               fileQuitAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_Q);
+               fileQuitAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_DOWN_MASK));
+
                openKeystoreAction = new AbstractAction("Open Keystore") {
 
                        @SuppressWarnings("synthetic-access")
                        public void actionPerformed(ActionEvent actionEvent) {
-                               openKeystore();
+                               actionKeystoreOpen();
                        }
                };
                openKeystoreAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_O);
@@ -116,6 +140,7 @@ public class MainFrame implements CoreListener, WindowListener {
                JMenuBar menuBar = new JMenuBar();
 
                menuBar.add(constructFileMenu());
+               menuBar.add(constructKeystoreMenu());
 
                return menuBar;
        }
@@ -128,15 +153,80 @@ public class MainFrame implements CoreListener, WindowListener {
        private JMenu constructFileMenu() {
                JMenu fileMenu = new JMenu("File");
 
-               fileMenu.add(openKeystoreAction);
+               fileMenu.add(fileQuitAction);
 
                return fileMenu;
        }
 
        /**
+        * Creates the “Key Store” menu.
+        *
+        * @return The “Key Store” menu
+        */
+       private JMenu constructKeystoreMenu() {
+               JMenu keystoreMenu = new JMenu("Key Store");
+
+               keystoreMenu.add(constructNewKeystoreTypeMenu());
+               keystoreMenu.add(openKeystoreAction);
+
+               return keystoreMenu;
+       }
+
+       /**
+        * Creates a new menu containing all the types for new key stores.
+        *
+        * @return A menu containing all new key store types
+        */
+       private JMenu constructNewKeystoreTypeMenu() {
+               JMenu keystoreTypeMenu = new JMenu("New Key Store");
+               keystoreTypeMenu.setMnemonic(KeyEvent.VK_N);
+
+               for (final String keystoreType : new String[] { "JKS", "PKCS12" }) {
+                       Action keystoreTypeAction = new AbstractAction(keystoreType) {
+
+                               /**
+                                * {@inheritDoc}
+                                */
+                               @SuppressWarnings("synthetic-access")
+                               public void actionPerformed(ActionEvent actionEvent) {
+                                       actionNewKeystore(keystoreType);
+                               }
+                       };
+                       keystoreTypeMenu.add(keystoreTypeAction);
+               }
+
+               return keystoreTypeMenu;
+       }
+
+       private void constructKeyStoreWindow(KeyStore keyStore) {
+               JInternalFrame internalFrame = new JInternalFrame("Key Store (" + keyStore.getType() + ")", true, true, true, true);
+               internalFrame.getContentPane().add(new ListKeyStorePanel(keyStore));
+               internalFrame.setBounds(10, 10, 200, 200);
+               desktop.add(internalFrame);
+               internalFrame.setVisible(true);
+               try {
+                       internalFrame.setSelected(true);
+               } catch (PropertyVetoException pve1) {
+                       pve1.printStackTrace();
+               }
+       }
+
+       //
+       // PRIVATE ACTIONS
+       //
+
+       /**
+        * Quits the program.
+        */
+       private void actionFileQuit() {
+               /* TODO - ask for confirmation. */
+               System.exit(0);
+       }
+
+       /**
         * Shows a file selection dialog and loads a keystore from a file.
         */
-       private void openKeystore() {
+       private void actionKeystoreOpen() {
                File directory;
                synchronized (syncObject) {
                        if (lastOpenKeystoreDirectory == null) {
@@ -157,6 +247,17 @@ public class MainFrame implements CoreListener, WindowListener {
                }
        }
 
+       /**
+        * Tells the core to create a new key store.
+        *
+        * @see Core#createKeyStore(String)
+        * @param keystoreType
+        *            The type of the key store
+        */
+       private void actionNewKeystore(String keystoreType) {
+               core.createKeyStore(keystoreType);
+       }
+
        //
        // INTERFACE CoreListener
        //
@@ -165,7 +266,7 @@ public class MainFrame implements CoreListener, WindowListener {
         * {@inheritDoc}
         */
        public void keyStoreCreated(KeyStore keyStore) {
-               /* TODO */
+               constructKeyStoreWindow(keyStore);
        }
 
        /**
@@ -179,14 +280,14 @@ public class MainFrame implements CoreListener, WindowListener {
         * {@inheritDoc}
         */
        public void keyStoreLoaded(File keyStoreFile, KeyStore keyStore) {
-               /* TODO - create keystore frame. */
+               constructKeyStoreWindow(keyStore);
        }
 
        /**
         * {@inheritDoc}
         */
        public void keyStoreNotLoaded(File keyStoreFile) {
-               JOptionPane.showMessageDialog(mainFrame, "Could not load keystore from “" + keyStoreFile.getName() + "”.", "Could not load keystore", JOptionPane.ERROR_MESSAGE);
+               JOptionPane.showMessageDialog(mainFrame, "Could not load key store from “" + keyStoreFile.getName() + "”.", "Could Not Load Key Store", JOptionPane.ERROR_MESSAGE);
        }
 
        //