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