From cb532ce01960cf8aaf236233df78d747292fdb7e Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 19 Jan 2009 21:58:18 +0100 Subject: [PATCH] Add basic menu and keystore loading functionality. --- src/net/pterodactylus/jkeytool/gui/MainFrame.java | 180 +++++++++++++++++++++- src/net/pterodactylus/jkeytool/main/Main.java | 5 +- 2 files changed, 182 insertions(+), 3 deletions(-) diff --git a/src/net/pterodactylus/jkeytool/gui/MainFrame.java b/src/net/pterodactylus/jkeytool/gui/MainFrame.java index 749a801..b6bda71 100644 --- a/src/net/pterodactylus/jkeytool/gui/MainFrame.java +++ b/src/net/pterodactylus/jkeytool/gui/MainFrame.java @@ -19,8 +19,25 @@ 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 <droden@gmail.com> */ -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. */ + } + } diff --git a/src/net/pterodactylus/jkeytool/main/Main.java b/src/net/pterodactylus/jkeytool/main/Main.java index 91b8eb5..22b4ec4 100644 --- a/src/net/pterodactylus/jkeytool/main/Main.java +++ b/src/net/pterodactylus/jkeytool/main/Main.java @@ -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(); } -- 2.7.4