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