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 “open keystore” action. */
60 private Action openKeystoreAction;
62 /** The last directory when opening keystores. */
63 private File lastOpenKeystoreDirectory = null;
66 * Creates a new jkeytool main frame.
69 * The core of the jkeytool application
71 public MainFrame(Core core) {
73 mainFrame = new JFrame("jkeytool " + Main.getVersion());
75 mainFrame.setJMenuBar(constructMenuBar());
76 mainFrame.addWindowListener(this);
85 * Shows the main frame.
88 mainFrame.setVisible(true);
96 * Constructs all actions.
98 private void constructActions() {
99 openKeystoreAction = new AbstractAction("Open Keystore") {
101 @SuppressWarnings("synthetic-access")
102 public void actionPerformed(ActionEvent actionEvent) {
106 openKeystoreAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_O);
107 openKeystoreAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK));
111 * Creates the menu bar.
113 * @return The menu bar
115 private JMenuBar constructMenuBar() {
116 JMenuBar menuBar = new JMenuBar();
118 menuBar.add(constructFileMenu());
124 * Creates the “File” menu.
126 * @return The “File” menu
128 private JMenu constructFileMenu() {
129 JMenu fileMenu = new JMenu("File");
131 fileMenu.add(openKeystoreAction);
137 * Shows a file selection dialog and loads a keystore from a file.
139 private void openKeystore() {
141 synchronized (syncObject) {
142 if (lastOpenKeystoreDirectory == null) {
143 lastOpenKeystoreDirectory = new File(".");
145 directory = lastOpenKeystoreDirectory;
147 JFileChooser fileChooser = new JFileChooser(directory);
148 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
149 fileChooser.setMultiSelectionEnabled(false);
150 int returnedOption = fileChooser.showOpenDialog(mainFrame);
151 if (returnedOption != JFileChooser.APPROVE_OPTION) {
154 core.loadKeyStore(fileChooser.getSelectedFile());
155 synchronized (syncObject) {
156 lastOpenKeystoreDirectory = fileChooser.getCurrentDirectory();
161 // INTERFACE CoreListener
167 public void keyStoreCreated(KeyStore keyStore) {
174 public void keyStoreNotCreated(String keyStoreType, Throwable reason) {
175 JOptionPane.showMessageDialog(mainFrame, "Could not create a key store of type “" + keyStoreType + "”.", "Could Not Create Key Store", JOptionPane.ERROR_MESSAGE);
181 public void keyStoreLoaded(File keyStoreFile, KeyStore keyStore) {
182 /* TODO - create keystore frame. */
188 public void keyStoreNotLoaded(File keyStoreFile) {
189 JOptionPane.showMessageDialog(mainFrame, "Could not load keystore from “" + keyStoreFile.getName() + "”.", "Could not load keystore", JOptionPane.ERROR_MESSAGE);
193 // INTERFACE WindowListener
199 public void windowActivated(WindowEvent e) {
206 public void windowClosed(WindowEvent e) {
213 public void windowClosing(WindowEvent e) {
220 public void windowDeactivated(WindowEvent e) {
227 public void windowDeiconified(WindowEvent e) {
234 public void windowIconified(WindowEvent e) {
241 public void windowOpened(WindowEvent e) {