Add “File -> Quit” action.
[jkeytool.git] / src / net / pterodactylus / jkeytool / gui / MainFrame.java
1 /*
2  * jkeytool - MainWindow.java -
3  * Copyright © 2009 David Roden
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 package net.pterodactylus.jkeytool.gui;
21
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.io.File;
28 import java.security.KeyStore;
29
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;
38
39 import net.pterodactylus.jkeytool.core.Core;
40 import net.pterodactylus.jkeytool.core.CoreListener;
41 import net.pterodactylus.jkeytool.main.Main;
42
43 /**
44  * The jkeytool main frame.
45  *
46  * @author David Roden <droden@gmail.com>
47  */
48 public class MainFrame implements CoreListener, WindowListener {
49
50         /** Object used for synchronization. */
51         private final Object syncObject = new Object();
52
53         /** The main frame. */
54         private final JFrame mainFrame;
55
56         /** The jkeytool core. */
57         private final Core core;
58
59         /** The “File -> Quit” action. */
60         private Action fileQuitAction;
61
62         /** The “open keystore” action. */
63         private Action openKeystoreAction;
64
65         /** The last directory when opening keystores. */
66         private File lastOpenKeystoreDirectory = null;
67
68         /**
69          * Creates a new jkeytool main frame.
70          *
71          * @param core
72          *            The core of the jkeytool application
73          */
74         public MainFrame(Core core) {
75                 this.core = core;
76                 mainFrame = new JFrame("jkeytool " + Main.getVersion());
77                 constructActions();
78                 mainFrame.setJMenuBar(constructMenuBar());
79                 mainFrame.addWindowListener(this);
80                 mainFrame.pack();
81         }
82
83         //
84         // ACTIONS
85         //
86
87         /**
88          * Shows the main frame.
89          */
90         public void show() {
91                 mainFrame.setVisible(true);
92         }
93
94         //
95         // PRIVATE METHODS
96         //
97
98         /**
99          * Constructs all actions.
100          */
101         private void constructActions() {
102                 fileQuitAction = new AbstractAction("Quit") {
103
104                         /**
105                          * {@inheritDoc}
106                          */
107                         @SuppressWarnings("synthetic-access")
108                         public void actionPerformed(ActionEvent actionEvent) {
109                                 actionFileQuit();
110                         }
111                 };
112                 fileQuitAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_Q);
113                 fileQuitAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_DOWN_MASK));
114
115                 openKeystoreAction = new AbstractAction("Open Keystore") {
116
117                         @SuppressWarnings("synthetic-access")
118                         public void actionPerformed(ActionEvent actionEvent) {
119                                 actionKeystoreOpen();
120                         }
121                 };
122                 openKeystoreAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_O);
123                 openKeystoreAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK));
124         }
125
126         /**
127          * Creates the menu bar.
128          *
129          * @return The menu bar
130          */
131         private JMenuBar constructMenuBar() {
132                 JMenuBar menuBar = new JMenuBar();
133
134                 menuBar.add(constructFileMenu());
135                 menuBar.add(constructKeystoreMenu());
136
137                 return menuBar;
138         }
139
140         /**
141          * Creates the “File” menu.
142          *
143          * @return The “File” menu
144          */
145         private JMenu constructFileMenu() {
146                 JMenu fileMenu = new JMenu("File");
147
148                 fileMenu.add(fileQuitAction);
149
150                 return fileMenu;
151         }
152
153         /**
154          * Creates the “Key Store” menu.
155          *
156          * @return The “Key Store” menu
157          */
158         private JMenu constructKeystoreMenu() {
159                 JMenu keystoreMenu = new JMenu("Key Store");
160
161                 keystoreMenu.add(constructNewKeystoreTypeMenu());
162                 keystoreMenu.add(openKeystoreAction);
163
164                 return keystoreMenu;
165         }
166
167         /**
168          * Creates a new menu containing all the types for new key stores.
169          *
170          * @return A menu containing all new key store types
171          */
172         private JMenu constructNewKeystoreTypeMenu() {
173                 JMenu keystoreTypeMenu = new JMenu("New Key Store");
174                 keystoreTypeMenu.setMnemonic(KeyEvent.VK_N);
175
176                 for (final String keystoreType : new String[] { "JKS", "PKCS12" }) {
177                         Action keystoreTypeAction = new AbstractAction(keystoreType) {
178
179                                 /**
180                                  * {@inheritDoc}
181                                  */
182                                 @SuppressWarnings("synthetic-access")
183                                 public void actionPerformed(ActionEvent actionEvent) {
184                                         actionNewKeystore(keystoreType);
185                                 }
186                         };
187                         keystoreTypeMenu.add(keystoreTypeAction);
188                 }
189
190                 return keystoreTypeMenu;
191         }
192
193         //
194         // PRIVATE ACTIONS
195         //
196
197         /**
198          * Quits the program.
199          */
200         private void actionFileQuit() {
201                 /* TODO - ask for confirmation. */
202                 System.exit(0);
203         }
204
205         /**
206          * Shows a file selection dialog and loads a keystore from a file.
207          */
208         private void actionKeystoreOpen() {
209                 File directory;
210                 synchronized (syncObject) {
211                         if (lastOpenKeystoreDirectory == null) {
212                                 lastOpenKeystoreDirectory = new File(".");
213                         }
214                         directory = lastOpenKeystoreDirectory;
215                 }
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) {
221                         return;
222                 }
223                 core.loadKeyStore(fileChooser.getSelectedFile());
224                 synchronized (syncObject) {
225                         lastOpenKeystoreDirectory = fileChooser.getCurrentDirectory();
226                 }
227         }
228
229         /**
230          * Tells the core to create a new key store.
231          *
232          * @see Core#createKeyStore(String)
233          * @param keystoreType
234          *            The type of the key store
235          */
236         private void actionNewKeystore(String keystoreType) {
237                 core.createKeyStore(keystoreType);
238         }
239
240         //
241         // INTERFACE CoreListener
242         //
243
244         /**
245          * {@inheritDoc}
246          */
247         public void keyStoreCreated(KeyStore keyStore) {
248                 /* TODO */
249         }
250
251         /**
252          * {@inheritDoc}
253          */
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);
256         }
257
258         /**
259          * {@inheritDoc}
260          */
261         public void keyStoreLoaded(File keyStoreFile, KeyStore keyStore) {
262                 /* TODO - create keystore frame. */
263         }
264
265         /**
266          * {@inheritDoc}
267          */
268         public void keyStoreNotLoaded(File keyStoreFile) {
269                 JOptionPane.showMessageDialog(mainFrame, "Could not load keystore from “" + keyStoreFile.getName() + "”.", "Could not load keystore", JOptionPane.ERROR_MESSAGE);
270         }
271
272         //
273         // INTERFACE WindowListener
274         //
275
276         /**
277          * {@inheritDoc}
278          */
279         public void windowActivated(WindowEvent e) {
280                 /* ignore. */
281         }
282
283         /**
284          * {@inheritDoc}
285          */
286         public void windowClosed(WindowEvent e) {
287                 /* ignore. */
288         }
289
290         /**
291          * {@inheritDoc}
292          */
293         public void windowClosing(WindowEvent e) {
294                 System.exit(0);
295         }
296
297         /**
298          * {@inheritDoc}
299          */
300         public void windowDeactivated(WindowEvent e) {
301                 /* ignore. */
302         }
303
304         /**
305          * {@inheritDoc}
306          */
307         public void windowDeiconified(WindowEvent e) {
308                 /* ignore. */
309         }
310
311         /**
312          * {@inheritDoc}
313          */
314         public void windowIconified(WindowEvent e) {
315                 /* ignore. */
316         }
317
318         /**
319          * {@inheritDoc}
320          */
321         public void windowOpened(WindowEvent e) {
322                 /* ignore. */
323         }
324
325 }