Add window listener to main frame.
[jkeytool.git] / src / net / pterodactylus / jkeytool / gui / swing / SwingInterface.java
index 4d287d9..7e3e300 100644 (file)
 
 package net.pterodactylus.jkeytool.gui.swing;
 
+import java.awt.BorderLayout;
+import java.awt.event.ActionEvent;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
 import java.io.File;
 import java.security.KeyStore;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.swing.Action;
+import javax.swing.BorderFactory;
+import javax.swing.JComponent;
+import javax.swing.JFrame;
+import javax.swing.JMenu;
+import javax.swing.JMenuBar;
+import javax.swing.JPanel;
+import javax.swing.JTabbedPane;
+import javax.swing.JToolBar;
 
 import net.pterodactylus.jkeytool.core.Core;
 import net.pterodactylus.jkeytool.gui.Interface;
+import net.pterodactylus.jkeytool.main.Main;
+import net.pterodactylus.util.i18n.I18n;
+import net.pterodactylus.util.i18n.gui.I18nAction;
+import net.pterodactylus.util.i18n.gui.I18nMenu;
+import net.pterodactylus.util.swing.StatusBar;
 
 /**
  * TODO
@@ -35,6 +56,147 @@ public class SwingInterface implements Interface {
        /** The core to control. */
        private Core core;
 
+       /** The I18n container. */
+       private I18n i18n = new I18n("jkeytool", SwingInterface.class);
+
+       /** The main frame. */
+       private JFrame mainFrame = new JFrame("jkeytool " + Main.getVersion());
+
+       /** The tab pane. */
+       private JTabbedPane tabPane = new JTabbedPane();
+
+       /** The status bar. */
+       private StatusBar statusBar = new StatusBar();
+
+       /** The “create key store” action. */
+       private Action createKeyStoreAction;
+
+       /** The “quit” action. */
+       private Action quitAction;
+
+       /** Loaded key stores and their panels. */
+       private final Map<KeyStore, KeyStorePanel> keyStores = new HashMap<KeyStore, KeyStorePanel>();
+
+       /**
+        * Creates a new Swing interface.
+        */
+       public SwingInterface() {
+               createActions();
+               createFrame();
+       }
+
+       //
+       // ACTIONS
+       //
+
+       /**
+        * Creates a new key store.
+        */
+       private void createKeyStore() {
+               /* TODO */
+       }
+
+       /**
+        * Exits the application.
+        */
+       private void quit() {
+               System.exit(0);
+       }
+
+       //
+       // PRIVATE METHODS
+       //
+
+       /**
+        * Creates all used actions.
+        */
+       private void createActions() {
+               createKeyStoreAction = new I18nAction(i18n, "jkeytool.action.createKeyStore") {
+
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent actionEvent) {
+                               createKeyStore();
+                       }
+               };
+               quitAction = new I18nAction(i18n, "jkeytool.action.quit") {
+
+                       /**
+                        * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
+                        */
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent actionEvent) {
+                               quit();
+                       }
+               };
+       }
+
+       /**
+        * Creates the application main frame.
+        */
+       private void createFrame() {
+               mainFrame.setJMenuBar(createMenubar());
+               mainFrame.getContentPane().add(createToolbar(), BorderLayout.PAGE_START);
+               mainFrame.getContentPane().add(createCenterPanel(), BorderLayout.CENTER);
+               mainFrame.getContentPane().add(statusBar, BorderLayout.PAGE_END);
+               mainFrame.pack();
+               mainFrame.addWindowListener(new WindowAdapter() {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @Override
+                       @SuppressWarnings("synthetic-access")
+                       public void windowClosing(WindowEvent windowEvent) {
+                               quit();
+                       }
+               });
+       }
+
+       /**
+        * Creates the central panel of the frame.
+        *
+        * @return The central panel of the frame
+        */
+       private JComponent createCenterPanel() {
+               JPanel centerPanel = new JPanel(new BorderLayout());
+               centerPanel.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
+
+               centerPanel.add(tabPane, BorderLayout.CENTER);
+
+               return centerPanel;
+       }
+
+       /**
+        * Creates the menu bar of the frame.
+        *
+        * @return The menu bar of the frame
+        */
+       private JMenuBar createMenubar() {
+               JMenuBar menubar = new JMenuBar();
+
+               JMenu fileMenu = new I18nMenu(i18n, "jkeytool.menu.file");
+               menubar.add(fileMenu);
+               fileMenu.add(createKeyStoreAction);
+               fileMenu.addSeparator();
+               fileMenu.add(quitAction);
+
+               return menubar;
+       }
+
+       /**
+        * Creates the tool bar of the frame.
+        *
+        * @return The tool bar of the frame
+        */
+       private JToolBar createToolbar() {
+               JToolBar toolbar = new JToolBar();
+
+               toolbar.add(createKeyStoreAction);
+               toolbar.add(quitAction);
+
+               return toolbar;
+       }
+
        //
        // INTERFACE Interface
        //
@@ -53,6 +215,15 @@ public class SwingInterface implements Interface {
         * {@inheritDoc}
         */
        public void start() {
+               mainFrame.setVisible(true);
+               statusBar.setText("jkeytool startup complete.");
+               core.loadKeyStore(new File("client.p12"));
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void stop() {
                /* TODO */
        }
 
@@ -71,7 +242,9 @@ public class SwingInterface implements Interface {
         * {@inheritDoc}
         */
        public void keyStoreLoaded(File keyStoreFile, KeyStore keyStore) {
-               /* TODO */
+               KeyStorePanel keyStorePanel = new KeyStorePanel(i18n, keyStore);
+               keyStores.put(keyStore, keyStorePanel);
+               tabPane.addTab(keyStoreFile.getName(), keyStorePanel);
        }
 
        /**