Add basic menu and keystore loading functionality.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 19 Jan 2009 20:58:18 +0000 (21:58 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 19 Jan 2009 20:58:18 +0000 (21:58 +0100)
src/net/pterodactylus/jkeytool/gui/MainFrame.java
src/net/pterodactylus/jkeytool/main/Main.java

index 749a801..b6bda71 100644 (file)
 
 package net.pterodactylus.jkeytool.gui;
 
+import java.awt.event.ActionEvent;
+import java.awt.event.InputEvent;
+import java.awt.event.KeyEvent;
+import java.awt.event.WindowEvent;
+import java.awt.event.WindowListener;
+import java.io.File;
+import java.security.KeyStore;
+
+import javax.swing.AbstractAction;
+import javax.swing.Action;
+import javax.swing.JFileChooser;
 import javax.swing.JFrame;
+import javax.swing.JMenu;
+import javax.swing.JMenuBar;
+import javax.swing.JOptionPane;
+import javax.swing.KeyStroke;
 
+import net.pterodactylus.jkeytool.core.Core;
+import net.pterodactylus.jkeytool.core.CoreListener;
 import net.pterodactylus.jkeytool.main.Main;
 
 /**
@@ -28,16 +45,35 @@ import net.pterodactylus.jkeytool.main.Main;
  *
  * @author David Roden &lt;droden@gmail.com&gt;
  */
-public class MainFrame {
+public class MainFrame implements CoreListener, WindowListener {
+
+       /** Object used for synchronization. */
+       private final Object syncObject = new Object();
 
        /** The main frame. */
        private final JFrame mainFrame;
 
+       /** The jkeytool core. */
+       private final Core core;
+
+       /** The “open keystore” action. */
+       private Action openKeystoreAction;
+
+       /** The last directory when opening keystores. */
+       private File lastOpenKeystoreDirectory = null;
+
        /**
         * Creates a new jkeytool main frame.
+        *
+        * @param core
+        *            The core of the jkeytool application
         */
-       public MainFrame() {
+       public MainFrame(Core core) {
+               this.core = core;
                mainFrame = new JFrame("jkeytool " + Main.getVersion());
+               constructActions();
+               mainFrame.setJMenuBar(constructMenuBar());
+               mainFrame.addWindowListener(this);
        }
 
        //
@@ -51,4 +87,144 @@ public class MainFrame {
                mainFrame.setVisible(true);
        }
 
+       //
+       // PRIVATE METHODS
+       //
+
+       /**
+        * Constructs all actions.
+        */
+       private void constructActions() {
+               openKeystoreAction = new AbstractAction("Open Keystore") {
+
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent actionEvent) {
+                               openKeystore();
+                       }
+               };
+               openKeystoreAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_O);
+               openKeystoreAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK));
+       }
+
+       /**
+        * Creates the menu bar.
+        *
+        * @return The menu bar
+        */
+       private JMenuBar constructMenuBar() {
+               JMenuBar menuBar = new JMenuBar();
+
+               menuBar.add(constructFileMenu());
+
+               return menuBar;
+       }
+
+       /**
+        * Creates the “File” menu.
+        *
+        * @return The “File” menu
+        */
+       private JMenu constructFileMenu() {
+               JMenu fileMenu = new JMenu("File");
+
+               fileMenu.add(openKeystoreAction);
+
+               return fileMenu;
+       }
+
+       /**
+        * Shows a file selection dialog and loads a keystore from a file.
+        */
+       private void openKeystore() {
+               File directory;
+               synchronized (syncObject) {
+                       if (lastOpenKeystoreDirectory == null) {
+                               lastOpenKeystoreDirectory = new File(".");
+                       }
+                       directory = lastOpenKeystoreDirectory;
+               }
+               JFileChooser fileChooser = new JFileChooser(directory);
+               fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
+               fileChooser.setMultiSelectionEnabled(false);
+               int returnedOption = fileChooser.showOpenDialog(mainFrame);
+               if (returnedOption != JFileChooser.APPROVE_OPTION) {
+                       return;
+               }
+               core.loadKeyStore(fileChooser.getSelectedFile());
+               synchronized (syncObject) {
+                       lastOpenKeystoreDirectory = fileChooser.getCurrentDirectory();
+               }
+       }
+
+       //
+       // INTERFACE CoreListener
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       public void keyStoreLoaded(File keyStoreFile, KeyStore keyStore) {
+               /* TODO - create keystore frame. */
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void keyStoreNotLoaded(File keyStoreFile) {
+               JOptionPane.showMessageDialog(mainFrame, "Could not load keystore from “" + keyStoreFile.getName() + "”.", "Could not load keystore", JOptionPane.ERROR_MESSAGE);
+       }
+
+       //
+       // INTERFACE WindowListener
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       public void windowActivated(WindowEvent e) {
+               /* ignore. */
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void windowClosed(WindowEvent e) {
+               /* ignore. */
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void windowClosing(WindowEvent e) {
+               System.exit(0);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void windowDeactivated(WindowEvent e) {
+               /* ignore. */
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void windowDeiconified(WindowEvent e) {
+               /* ignore. */
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void windowIconified(WindowEvent e) {
+               /* ignore. */
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void windowOpened(WindowEvent e) {
+               /* ignore. */
+       }
+
 }
index 91b8eb5..22b4ec4 100644 (file)
@@ -19,6 +19,7 @@
 
 package net.pterodactylus.jkeytool.main;
 
+import net.pterodactylus.jkeytool.core.Core;
 import net.pterodactylus.jkeytool.gui.MainFrame;
 import de.ina.util.version.Version;
 
@@ -48,7 +49,9 @@ public class Main {
         *            The command-line arguments
         */
        public static void main(String[] arguments) {
-               MainFrame mainFrame = new MainFrame();
+               Core core = new Core();
+               MainFrame mainFrame = new MainFrame(core);
+               core.addCoreListener(mainFrame);
                mainFrame.show();
        }