Add menu bar.
[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
38 import net.pterodactylus.jkeytool.core.Core;
39 import net.pterodactylus.jkeytool.gui.Interface;
40 import net.pterodactylus.jkeytool.main.Main;
41 import net.pterodactylus.util.i18n.gui.I18nAction;
42 import net.pterodactylus.util.i18n.gui.I18nMenu;
43 import net.pterodactylus.util.swing.StatusBar;
44
45 /**
46  * TODO
47  *
48  * @author David Roden <droden@gmail.com>
49  */
50 public class SwingInterface implements Interface {
51
52         /** The core to control. */
53         private Core core;
54
55         /** The main frame. */
56         private JFrame mainFrame = new JFrame("jkeytool " + Main.getVersion());
57
58         /** The tab pane. */
59         private JTabbedPane tabPane = new JTabbedPane();
60
61         /** The status bar. */
62         private StatusBar statusBar = new StatusBar();
63
64         /** The “create key store” action. */
65         private Action createKeyStoreAction;
66
67         /** The “quit” action. */
68         private Action quitAction;
69
70         /** Loaded key stores and their panels. */
71         private final Map<KeyStore, KeyStorePanel> keyStores = new HashMap<KeyStore, KeyStorePanel>();
72
73         public SwingInterface() {
74                 createActions();
75                 createFrame();
76         }
77
78         //
79         // ACTIONS
80         //
81
82         private void createKeyStore() {
83         }
84
85         private void quit() {
86                 System.exit(0);
87         }
88
89         //
90         // PRIVATE METHODS
91         //
92
93         private void createActions() {
94                 createKeyStoreAction = new I18nAction("jkeytool.action.createKeyStore") {
95
96                         @SuppressWarnings("synthetic-access")
97                         public void actionPerformed(ActionEvent actionEvent) {
98                                 createKeyStore();
99                         }
100                 };
101                 quitAction = new I18nAction("jkeytool.action.quit") {
102
103                         /**
104                          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
105                          */
106                         @SuppressWarnings("synthetic-access")
107                         public void actionPerformed(ActionEvent actionEvent) {
108                                 quit();
109                         }
110                 };
111         }
112
113         private void createFrame() {
114                 mainFrame.setJMenuBar(createMenubar());
115                 mainFrame.getContentPane().add(createCenterPanel(), BorderLayout.CENTER);
116                 mainFrame.getContentPane().add(statusBar, BorderLayout.PAGE_END);
117                 mainFrame.pack();
118         }
119
120         private JComponent createCenterPanel() {
121                 JPanel centerPanel = new JPanel(new BorderLayout());
122                 centerPanel.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
123
124                 centerPanel.add(tabPane, BorderLayout.CENTER);
125
126                 return centerPanel;
127         }
128
129         private JMenuBar createMenubar() {
130                 JMenuBar menubar = new JMenuBar();
131
132                 JMenu fileMenu = new I18nMenu("jkeytool.menu.file");
133                 menubar.add(fileMenu);
134                 fileMenu.add(createKeyStoreAction);
135                 fileMenu.addSeparator();
136                 fileMenu.add(quitAction);
137
138                 return menubar;
139         }
140
141         //
142         // INTERFACE Interface
143         //
144
145         /**
146          * Sets the core to control.
147          *
148          * @param core
149          *            The core to control
150          */
151         public void setCore(Core core) {
152                 this.core = core;
153         }
154
155         /**
156          * {@inheritDoc}
157          */
158         public void start() {
159                 mainFrame.setVisible(true);
160                 statusBar.setText("jkeytool startup complete.");
161                 core.loadKeyStore(new File("client.p12"));
162         }
163
164         /**
165          * {@inheritDoc}
166          */
167         public void stop() {
168                 /* TODO */
169         }
170
171         //
172         // INTERFACE CoreListener
173         //
174
175         /**
176          * {@inheritDoc}
177          */
178         public void keyStoreCreated(KeyStore keyStore) {
179                 /* TODO */
180         }
181
182         /**
183          * {@inheritDoc}
184          */
185         public void keyStoreLoaded(File keyStoreFile, KeyStore keyStore) {
186                 KeyStorePanel keyStorePanel = new KeyStorePanel(keyStore);
187                 keyStores.put(keyStore, keyStorePanel);
188                 tabPane.addTab(keyStoreFile.getName(), keyStorePanel);
189         }
190
191         /**
192          * {@inheritDoc}
193          */
194         public void keyStoreNotCreated(String keyStoreType, Throwable reason) {
195                 /* TODO */
196         }
197
198         /**
199          * {@inheritDoc}
200          */
201         public void keyStoreNotLoaded(File keyStoreFile) {
202                 /* TODO */
203         }
204
205 }