Add basic menu and keystore loading functionality.
[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 “open keystore” action. */
60         private Action openKeystoreAction;
61
62         /** The last directory when opening keystores. */
63         private File lastOpenKeystoreDirectory = null;
64
65         /**
66          * Creates a new jkeytool main frame.
67          *
68          * @param core
69          *            The core of the jkeytool application
70          */
71         public MainFrame(Core core) {
72                 this.core = core;
73                 mainFrame = new JFrame("jkeytool " + Main.getVersion());
74                 constructActions();
75                 mainFrame.setJMenuBar(constructMenuBar());
76                 mainFrame.addWindowListener(this);
77         }
78
79         //
80         // ACTIONS
81         //
82
83         /**
84          * Shows the main frame.
85          */
86         public void show() {
87                 mainFrame.setVisible(true);
88         }
89
90         //
91         // PRIVATE METHODS
92         //
93
94         /**
95          * Constructs all actions.
96          */
97         private void constructActions() {
98                 openKeystoreAction = new AbstractAction("Open Keystore") {
99
100                         @SuppressWarnings("synthetic-access")
101                         public void actionPerformed(ActionEvent actionEvent) {
102                                 openKeystore();
103                         }
104                 };
105                 openKeystoreAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_O);
106                 openKeystoreAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK));
107         }
108
109         /**
110          * Creates the menu bar.
111          *
112          * @return The menu bar
113          */
114         private JMenuBar constructMenuBar() {
115                 JMenuBar menuBar = new JMenuBar();
116
117                 menuBar.add(constructFileMenu());
118
119                 return menuBar;
120         }
121
122         /**
123          * Creates the “File” menu.
124          *
125          * @return The “File” menu
126          */
127         private JMenu constructFileMenu() {
128                 JMenu fileMenu = new JMenu("File");
129
130                 fileMenu.add(openKeystoreAction);
131
132                 return fileMenu;
133         }
134
135         /**
136          * Shows a file selection dialog and loads a keystore from a file.
137          */
138         private void openKeystore() {
139                 File directory;
140                 synchronized (syncObject) {
141                         if (lastOpenKeystoreDirectory == null) {
142                                 lastOpenKeystoreDirectory = new File(".");
143                         }
144                         directory = lastOpenKeystoreDirectory;
145                 }
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) {
151                         return;
152                 }
153                 core.loadKeyStore(fileChooser.getSelectedFile());
154                 synchronized (syncObject) {
155                         lastOpenKeystoreDirectory = fileChooser.getCurrentDirectory();
156                 }
157         }
158
159         //
160         // INTERFACE CoreListener
161         //
162
163         /**
164          * {@inheritDoc}
165          */
166         public void keyStoreLoaded(File keyStoreFile, KeyStore keyStore) {
167                 /* TODO - create keystore frame. */
168         }
169
170         /**
171          * {@inheritDoc}
172          */
173         public void keyStoreNotLoaded(File keyStoreFile) {
174                 JOptionPane.showMessageDialog(mainFrame, "Could not load keystore from “" + keyStoreFile.getName() + "”.", "Could not load keystore", JOptionPane.ERROR_MESSAGE);
175         }
176
177         //
178         // INTERFACE WindowListener
179         //
180
181         /**
182          * {@inheritDoc}
183          */
184         public void windowActivated(WindowEvent e) {
185                 /* ignore. */
186         }
187
188         /**
189          * {@inheritDoc}
190          */
191         public void windowClosed(WindowEvent e) {
192                 /* ignore. */
193         }
194
195         /**
196          * {@inheritDoc}
197          */
198         public void windowClosing(WindowEvent e) {
199                 System.exit(0);
200         }
201
202         /**
203          * {@inheritDoc}
204          */
205         public void windowDeactivated(WindowEvent e) {
206                 /* ignore. */
207         }
208
209         /**
210          * {@inheritDoc}
211          */
212         public void windowDeiconified(WindowEvent e) {
213                 /* ignore. */
214         }
215
216         /**
217          * {@inheritDoc}
218          */
219         public void windowIconified(WindowEvent e) {
220                 /* ignore. */
221         }
222
223         /**
224          * {@inheritDoc}
225          */
226         public void windowOpened(WindowEvent e) {
227                 /* ignore. */
228         }
229
230 }