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;
27 import java.beans.PropertyVetoException;
29 import java.security.KeyStore;
31 import javax.swing.AbstractAction;
32 import javax.swing.Action;
33 import javax.swing.JDesktopPane;
34 import javax.swing.JFileChooser;
35 import javax.swing.JFrame;
36 import javax.swing.JInternalFrame;
37 import javax.swing.JMenu;
38 import javax.swing.JMenuBar;
39 import javax.swing.JOptionPane;
40 import javax.swing.KeyStroke;
42 import net.pterodactylus.jkeytool.core.Core;
43 import net.pterodactylus.jkeytool.core.CoreListener;
44 import net.pterodactylus.jkeytool.main.Main;
47 * The jkeytool main frame.
49 * @author David Roden <droden@gmail.com>
51 public class MainFrame implements CoreListener, WindowListener {
53 /** Object used for synchronization. */
54 private final Object syncObject = new Object();
56 /** The main frame. */
57 private final JFrame mainFrame;
59 /** The main frame’s desktop. */
60 private final JDesktopPane desktop;
62 /** The jkeytool core. */
63 private final Core core;
65 /** The “File -> Quit” action. */
66 private Action fileQuitAction;
68 /** The “open keystore” action. */
69 private Action openKeystoreAction;
71 /** The last directory when opening keystores. */
72 private File lastOpenKeystoreDirectory = null;
75 * Creates a new jkeytool main frame.
78 * The core of the jkeytool application
80 public MainFrame(Core core) {
82 mainFrame = new JFrame("jkeytool " + Main.getVersion());
84 mainFrame.setJMenuBar(constructMenuBar());
85 mainFrame.addWindowListener(this);
86 desktop = new JDesktopPane();
87 mainFrame.setContentPane(desktop);
96 * Shows the main frame.
99 mainFrame.setVisible(true);
107 * Constructs all actions.
109 private void constructActions() {
110 fileQuitAction = new AbstractAction("Quit") {
115 @SuppressWarnings("synthetic-access")
116 public void actionPerformed(ActionEvent actionEvent) {
120 fileQuitAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_Q);
121 fileQuitAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_DOWN_MASK));
123 openKeystoreAction = new AbstractAction("Open Keystore") {
125 @SuppressWarnings("synthetic-access")
126 public void actionPerformed(ActionEvent actionEvent) {
127 actionKeystoreOpen();
130 openKeystoreAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_O);
131 openKeystoreAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK));
135 * Creates the menu bar.
137 * @return The menu bar
139 private JMenuBar constructMenuBar() {
140 JMenuBar menuBar = new JMenuBar();
142 menuBar.add(constructFileMenu());
143 menuBar.add(constructKeystoreMenu());
149 * Creates the “File” menu.
151 * @return The “File” menu
153 private JMenu constructFileMenu() {
154 JMenu fileMenu = new JMenu("File");
156 fileMenu.add(fileQuitAction);
162 * Creates the “Key Store” menu.
164 * @return The “Key Store” menu
166 private JMenu constructKeystoreMenu() {
167 JMenu keystoreMenu = new JMenu("Key Store");
169 keystoreMenu.add(constructNewKeystoreTypeMenu());
170 keystoreMenu.add(openKeystoreAction);
176 * Creates a new menu containing all the types for new key stores.
178 * @return A menu containing all new key store types
180 private JMenu constructNewKeystoreTypeMenu() {
181 JMenu keystoreTypeMenu = new JMenu("New Key Store");
182 keystoreTypeMenu.setMnemonic(KeyEvent.VK_N);
184 for (final String keystoreType : new String[] { "JKS", "PKCS12" }) {
185 Action keystoreTypeAction = new AbstractAction(keystoreType) {
190 @SuppressWarnings("synthetic-access")
191 public void actionPerformed(ActionEvent actionEvent) {
192 actionNewKeystore(keystoreType);
195 keystoreTypeMenu.add(keystoreTypeAction);
198 return keystoreTypeMenu;
201 private void constructKeyStoreWindow(KeyStore keyStore) {
202 JInternalFrame internalFrame = new JInternalFrame("Key Store (" + keyStore.getType() + ")", true, true, true, true);
203 internalFrame.getContentPane().add(new ListKeyStorePanel(keyStore));
204 internalFrame.setBounds(10, 10, 200, 200);
205 desktop.add(internalFrame);
206 internalFrame.setVisible(true);
208 internalFrame.setSelected(true);
209 } catch (PropertyVetoException pve1) {
210 pve1.printStackTrace();
221 private void actionFileQuit() {
222 /* TODO - ask for confirmation. */
227 * Shows a file selection dialog and loads a keystore from a file.
229 private void actionKeystoreOpen() {
231 synchronized (syncObject) {
232 if (lastOpenKeystoreDirectory == null) {
233 lastOpenKeystoreDirectory = new File(".");
235 directory = lastOpenKeystoreDirectory;
237 JFileChooser fileChooser = new JFileChooser(directory);
238 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
239 fileChooser.setMultiSelectionEnabled(false);
240 int returnedOption = fileChooser.showOpenDialog(mainFrame);
241 if (returnedOption != JFileChooser.APPROVE_OPTION) {
244 core.loadKeyStore(fileChooser.getSelectedFile());
245 synchronized (syncObject) {
246 lastOpenKeystoreDirectory = fileChooser.getCurrentDirectory();
251 * Tells the core to create a new key store.
253 * @see Core#createKeyStore(String)
254 * @param keystoreType
255 * The type of the key store
257 private void actionNewKeystore(String keystoreType) {
258 core.createKeyStore(keystoreType);
262 // INTERFACE CoreListener
268 public void keyStoreCreated(KeyStore keyStore) {
269 constructKeyStoreWindow(keyStore);
275 public void keyStoreNotCreated(String keyStoreType, Throwable reason) {
276 JOptionPane.showMessageDialog(mainFrame, "Could not create a key store of type “" + keyStoreType + "”.", "Could Not Create Key Store", JOptionPane.ERROR_MESSAGE);
282 public void keyStoreLoaded(File keyStoreFile, KeyStore keyStore) {
283 constructKeyStoreWindow(keyStore);
289 public void keyStoreNotLoaded(File keyStoreFile) {
290 JOptionPane.showMessageDialog(mainFrame, "Could not load key store from “" + keyStoreFile.getName() + "”.", "Could Not Load Key Store", JOptionPane.ERROR_MESSAGE);
294 // INTERFACE WindowListener
300 public void windowActivated(WindowEvent e) {
307 public void windowClosed(WindowEvent e) {
314 public void windowClosing(WindowEvent e) {
321 public void windowDeactivated(WindowEvent e) {
328 public void windowDeiconified(WindowEvent e) {
335 public void windowIconified(WindowEvent e) {
342 public void windowOpened(WindowEvent e) {