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