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