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;
/**
*
* @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);
}
//
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. */
+ }
+
}