+ //
+ // PRIVATE METHODS
+ //
+
+ /**
+ * 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) {
+ actionKeystoreOpen();
+ }
+ };
+ 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());
+ menuBar.add(constructKeystoreMenu());
+
+ return menuBar;
+ }
+
+ /**
+ * Creates the “File” menu.
+ *
+ * @return The “File” menu
+ */
+ private JMenu constructFileMenu() {
+ JMenu fileMenu = new JMenu("File");
+
+ 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 actionKeystoreOpen() {
+ 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();
+ }
+ }
+
+ /**
+ * 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
+ //
+
+ /**
+ * {@inheritDoc}
+ */
+ public void keyStoreCreated(KeyStore keyStore) {
+ constructKeyStoreWindow(keyStore);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void keyStoreNotCreated(String keyStoreType, Throwable reason) {
+ JOptionPane.showMessageDialog(mainFrame, "Could not create a key store of type “" + keyStoreType + "”.", "Could Not Create Key Store", JOptionPane.ERROR_MESSAGE);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void keyStoreLoaded(File keyStoreFile, KeyStore keyStore) {
+ constructKeyStoreWindow(keyStore);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void keyStoreNotLoaded(File keyStoreFile) {
+ JOptionPane.showMessageDialog(mainFrame, "Could not load key store from “" + keyStoreFile.getName() + "”.", "Could Not Load Key Store", 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. */
+ }
+