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