2 * jkeytool - MainWindow.java -
3 * Copyright © 2009 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package net.pterodactylus.jkeytool.gui;
22 import java.awt.event.ActionEvent;
23 import java.awt.event.InputEvent;
24 import java.awt.event.KeyEvent;
25 import java.awt.event.WindowEvent;
26 import java.awt.event.WindowListener;
28 import java.security.KeyStore;
30 import javax.swing.AbstractAction;
31 import javax.swing.Action;
32 import javax.swing.JFileChooser;
33 import javax.swing.JFrame;
34 import javax.swing.JMenu;
35 import javax.swing.JMenuBar;
36 import javax.swing.JOptionPane;
37 import javax.swing.KeyStroke;
39 import net.pterodactylus.jkeytool.core.Core;
40 import net.pterodactylus.jkeytool.core.CoreListener;
41 import net.pterodactylus.jkeytool.main.Main;
44 * The jkeytool main frame.
46 * @author David Roden <droden@gmail.com>
48 public class MainFrame implements CoreListener, WindowListener {
50 /** Object used for synchronization. */
51 private final Object syncObject = new Object();
53 /** The main frame. */
54 private final JFrame mainFrame;
56 /** The jkeytool core. */
57 private final Core core;
59 /** The “File -> Quit” action. */
60 private Action fileQuitAction;
62 /** The “open keystore” action. */
63 private Action openKeystoreAction;
65 /** The last directory when opening keystores. */
66 private File lastOpenKeystoreDirectory = null;
69 * Creates a new jkeytool main frame.
72 * The core of the jkeytool application
74 public MainFrame(Core core) {
76 mainFrame = new JFrame("jkeytool " + Main.getVersion());
78 mainFrame.setJMenuBar(constructMenuBar());
79 mainFrame.addWindowListener(this);
88 * Shows the main frame.
91 mainFrame.setVisible(true);
99 * Constructs all actions.
101 private void constructActions() {
102 fileQuitAction = new AbstractAction("Quit") {
107 @SuppressWarnings("synthetic-access")
108 public void actionPerformed(ActionEvent actionEvent) {
112 fileQuitAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_Q);
113 fileQuitAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_DOWN_MASK));
115 openKeystoreAction = new AbstractAction("Open Keystore") {
117 @SuppressWarnings("synthetic-access")
118 public void actionPerformed(ActionEvent actionEvent) {
119 actionKeystoreOpen();
122 openKeystoreAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_O);
123 openKeystoreAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK));
127 * Creates the menu bar.
129 * @return The menu bar
131 private JMenuBar constructMenuBar() {
132 JMenuBar menuBar = new JMenuBar();
134 menuBar.add(constructFileMenu());
135 menuBar.add(constructKeystoreMenu());
141 * Creates the “File” menu.
143 * @return The “File” menu
145 private JMenu constructFileMenu() {
146 JMenu fileMenu = new JMenu("File");
148 fileMenu.add(fileQuitAction);
154 * Creates the “Key Store” menu.
156 * @return The “Key Store” menu
158 private JMenu constructKeystoreMenu() {
159 JMenu keystoreMenu = new JMenu("Key Store");
161 keystoreMenu.add(constructNewKeystoreTypeMenu());
162 keystoreMenu.add(openKeystoreAction);
168 * Creates a new menu containing all the types for new key stores.
170 * @return A menu containing all new key store types
172 private JMenu constructNewKeystoreTypeMenu() {
173 JMenu keystoreTypeMenu = new JMenu("New Key Store");
174 keystoreTypeMenu.setMnemonic(KeyEvent.VK_N);
176 for (final String keystoreType : new String[] { "JKS", "PKCS12" }) {
177 Action keystoreTypeAction = new AbstractAction(keystoreType) {
182 @SuppressWarnings("synthetic-access")
183 public void actionPerformed(ActionEvent actionEvent) {
184 actionNewKeystore(keystoreType);
187 keystoreTypeMenu.add(keystoreTypeAction);
190 return keystoreTypeMenu;
200 private void actionFileQuit() {
201 /* TODO - ask for confirmation. */
206 * Shows a file selection dialog and loads a keystore from a file.
208 private void actionKeystoreOpen() {
210 synchronized (syncObject) {
211 if (lastOpenKeystoreDirectory == null) {
212 lastOpenKeystoreDirectory = new File(".");
214 directory = lastOpenKeystoreDirectory;
216 JFileChooser fileChooser = new JFileChooser(directory);
217 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
218 fileChooser.setMultiSelectionEnabled(false);
219 int returnedOption = fileChooser.showOpenDialog(mainFrame);
220 if (returnedOption != JFileChooser.APPROVE_OPTION) {
223 core.loadKeyStore(fileChooser.getSelectedFile());
224 synchronized (syncObject) {
225 lastOpenKeystoreDirectory = fileChooser.getCurrentDirectory();
230 * Tells the core to create a new key store.
232 * @see Core#createKeyStore(String)
233 * @param keystoreType
234 * The type of the key store
236 private void actionNewKeystore(String keystoreType) {
237 core.createKeyStore(keystoreType);
241 // INTERFACE CoreListener
247 public void keyStoreCreated(KeyStore keyStore) {
254 public void keyStoreNotCreated(String keyStoreType, Throwable reason) {
255 JOptionPane.showMessageDialog(mainFrame, "Could not create a key store of type “" + keyStoreType + "”.", "Could Not Create Key Store", JOptionPane.ERROR_MESSAGE);
261 public void keyStoreLoaded(File keyStoreFile, KeyStore keyStore) {
262 /* TODO - create keystore frame. */
268 public void keyStoreNotLoaded(File keyStoreFile) {
269 JOptionPane.showMessageDialog(mainFrame, "Could not load keystore from “" + keyStoreFile.getName() + "”.", "Could not load keystore", JOptionPane.ERROR_MESSAGE);
273 // INTERFACE WindowListener
279 public void windowActivated(WindowEvent e) {
286 public void windowClosed(WindowEvent e) {
293 public void windowClosing(WindowEvent e) {
300 public void windowDeactivated(WindowEvent e) {
307 public void windowDeiconified(WindowEvent e) {
314 public void windowIconified(WindowEvent e) {
321 public void windowOpened(WindowEvent e) {