Show key store type in key store panel.
[jkeytool.git] / src / net / pterodactylus / jkeytool / gui / swing / SwingInterface.java
1 /*
2  * jkeytool - SwingInterface.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.swing;
21
22 import java.awt.BorderLayout;
23 import java.awt.event.ActionEvent;
24 import java.io.File;
25 import java.security.KeyStore;
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import javax.swing.Action;
30 import javax.swing.BorderFactory;
31 import javax.swing.JComponent;
32 import javax.swing.JFrame;
33 import javax.swing.JMenu;
34 import javax.swing.JMenuBar;
35 import javax.swing.JPanel;
36 import javax.swing.JTabbedPane;
37 import javax.swing.JToolBar;
38
39 import net.pterodactylus.jkeytool.core.Core;
40 import net.pterodactylus.jkeytool.gui.Interface;
41 import net.pterodactylus.jkeytool.main.Main;
42 import net.pterodactylus.util.i18n.I18n;
43 import net.pterodactylus.util.i18n.gui.I18nAction;
44 import net.pterodactylus.util.i18n.gui.I18nMenu;
45 import net.pterodactylus.util.swing.StatusBar;
46
47 /**
48  * TODO
49  *
50  * @author David Roden <droden@gmail.com>
51  */
52 public class SwingInterface implements Interface {
53
54         /** The core to control. */
55         private Core core;
56
57         /** The I18n container. */
58         private I18n i18n = new I18n("jkeytool", SwingInterface.class);
59
60         /** The main frame. */
61         private JFrame mainFrame = new JFrame("jkeytool " + Main.getVersion());
62
63         /** The tab pane. */
64         private JTabbedPane tabPane = new JTabbedPane();
65
66         /** The status bar. */
67         private StatusBar statusBar = new StatusBar();
68
69         /** The “create key store” action. */
70         private Action createKeyStoreAction;
71
72         /** The “quit” action. */
73         private Action quitAction;
74
75         /** Loaded key stores and their panels. */
76         private final Map<KeyStore, KeyStorePanel> keyStores = new HashMap<KeyStore, KeyStorePanel>();
77
78         /**
79          * Creates a new Swing interface.
80          */
81         public SwingInterface() {
82                 createActions();
83                 createFrame();
84         }
85
86         //
87         // ACTIONS
88         //
89
90         /**
91          * Creates a new key store.
92          */
93         private void createKeyStore() {
94                 /* TODO */
95         }
96
97         /**
98          * Exits the application.
99          */
100         private void quit() {
101                 System.exit(0);
102         }
103
104         //
105         // PRIVATE METHODS
106         //
107
108         /**
109          * Creates all used actions.
110          */
111         private void createActions() {
112                 createKeyStoreAction = new I18nAction(i18n, "jkeytool.action.createKeyStore") {
113
114                         @SuppressWarnings("synthetic-access")
115                         public void actionPerformed(ActionEvent actionEvent) {
116                                 createKeyStore();
117                         }
118                 };
119                 quitAction = new I18nAction(i18n, "jkeytool.action.quit") {
120
121                         /**
122                          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
123                          */
124                         @SuppressWarnings("synthetic-access")
125                         public void actionPerformed(ActionEvent actionEvent) {
126                                 quit();
127                         }
128                 };
129         }
130
131         /**
132          * Creates the application main frame.
133          */
134         private void createFrame() {
135                 mainFrame.setJMenuBar(createMenubar());
136                 mainFrame.getContentPane().add(createToolbar(), BorderLayout.PAGE_START);
137                 mainFrame.getContentPane().add(createCenterPanel(), BorderLayout.CENTER);
138                 mainFrame.getContentPane().add(statusBar, BorderLayout.PAGE_END);
139                 mainFrame.pack();
140         }
141
142         /**
143          * Creates the central panel of the frame.
144          *
145          * @return The central panel of the frame
146          */
147         private JComponent createCenterPanel() {
148                 JPanel centerPanel = new JPanel(new BorderLayout());
149                 centerPanel.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
150
151                 centerPanel.add(tabPane, BorderLayout.CENTER);
152
153                 return centerPanel;
154         }
155
156         /**
157          * Creates the menu bar of the frame.
158          *
159          * @return The menu bar of the frame
160          */
161         private JMenuBar createMenubar() {
162                 JMenuBar menubar = new JMenuBar();
163
164                 JMenu fileMenu = new I18nMenu(i18n, "jkeytool.menu.file");
165                 menubar.add(fileMenu);
166                 fileMenu.add(createKeyStoreAction);
167                 fileMenu.addSeparator();
168                 fileMenu.add(quitAction);
169
170                 return menubar;
171         }
172
173         /**
174          * Creates the tool bar of the frame.
175          *
176          * @return The tool bar of the frame
177          */
178         private JToolBar createToolbar() {
179                 JToolBar toolbar = new JToolBar();
180
181                 toolbar.add(createKeyStoreAction);
182                 toolbar.add(quitAction);
183
184                 return toolbar;
185         }
186
187         //
188         // INTERFACE Interface
189         //
190
191         /**
192          * Sets the core to control.
193          *
194          * @param core
195          *            The core to control
196          */
197         public void setCore(Core core) {
198                 this.core = core;
199         }
200
201         /**
202          * {@inheritDoc}
203          */
204         public void start() {
205                 mainFrame.setVisible(true);
206                 statusBar.setText("jkeytool startup complete.");
207                 core.loadKeyStore(new File("client.p12"));
208         }
209
210         /**
211          * {@inheritDoc}
212          */
213         public void stop() {
214                 /* TODO */
215         }
216
217         //
218         // INTERFACE CoreListener
219         //
220
221         /**
222          * {@inheritDoc}
223          */
224         public void keyStoreCreated(KeyStore keyStore) {
225                 /* TODO */
226         }
227
228         /**
229          * {@inheritDoc}
230          */
231         public void keyStoreLoaded(File keyStoreFile, KeyStore keyStore) {
232                 KeyStorePanel keyStorePanel = new KeyStorePanel(i18n, keyStore);
233                 keyStores.put(keyStore, keyStorePanel);
234                 tabPane.addTab(keyStoreFile.getName(), keyStorePanel);
235         }
236
237         /**
238          * {@inheritDoc}
239          */
240         public void keyStoreNotCreated(String keyStoreType, Throwable reason) {
241                 /* TODO */
242         }
243
244         /**
245          * {@inheritDoc}
246          */
247         public void keyStoreNotLoaded(File keyStoreFile) {
248                 /* TODO */
249         }
250
251 }