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);
84 * Shows the main frame.
87 mainFrame.setVisible(true);
95 * Constructs all actions.
97 private void constructActions() {
98 openKeystoreAction = new AbstractAction("Open Keystore") {
100 @SuppressWarnings("synthetic-access")
101 public void actionPerformed(ActionEvent actionEvent) {
105 openKeystoreAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_O);
106 openKeystoreAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK));
110 * Creates the menu bar.
112 * @return The menu bar
114 private JMenuBar constructMenuBar() {
115 JMenuBar menuBar = new JMenuBar();
117 menuBar.add(constructFileMenu());
123 * Creates the “File” menu.
125 * @return The “File” menu
127 private JMenu constructFileMenu() {
128 JMenu fileMenu = new JMenu("File");
130 fileMenu.add(openKeystoreAction);
136 * Shows a file selection dialog and loads a keystore from a file.
138 private void openKeystore() {
140 synchronized (syncObject) {
141 if (lastOpenKeystoreDirectory == null) {
142 lastOpenKeystoreDirectory = new File(".");
144 directory = lastOpenKeystoreDirectory;
146 JFileChooser fileChooser = new JFileChooser(directory);
147 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
148 fileChooser.setMultiSelectionEnabled(false);
149 int returnedOption = fileChooser.showOpenDialog(mainFrame);
150 if (returnedOption != JFileChooser.APPROVE_OPTION) {
153 core.loadKeyStore(fileChooser.getSelectedFile());
154 synchronized (syncObject) {
155 lastOpenKeystoreDirectory = fileChooser.getCurrentDirectory();
160 // INTERFACE CoreListener
166 public void keyStoreLoaded(File keyStoreFile, KeyStore keyStore) {
167 /* TODO - create keystore frame. */
173 public void keyStoreNotLoaded(File keyStoreFile) {
174 JOptionPane.showMessageDialog(mainFrame, "Could not load keystore from “" + keyStoreFile.getName() + "”.", "Could not load keystore", JOptionPane.ERROR_MESSAGE);
178 // INTERFACE WindowListener
184 public void windowActivated(WindowEvent e) {
191 public void windowClosed(WindowEvent e) {
198 public void windowClosing(WindowEvent e) {
205 public void windowDeactivated(WindowEvent e) {
212 public void windowDeiconified(WindowEvent e) {
219 public void windowIconified(WindowEvent e) {
226 public void windowOpened(WindowEvent e) {