implement node addition and removal events
[jSite2.git] / src / net / pterodactylus / jsite / gui / MainWindow.java
index ed2b1db..4eb7ab7 100644 (file)
@@ -24,12 +24,15 @@ import java.awt.Container;
 import java.awt.Dimension;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
+import java.awt.event.WindowListener;
 
+import javax.swing.Action;
 import javax.swing.Box;
 import javax.swing.BoxLayout;
 import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.JMenuBar;
+import javax.swing.JMenuItem;
 import javax.swing.JPanel;
 import javax.swing.JTabbedPane;
 import javax.swing.JToolBar;
@@ -47,11 +50,11 @@ import net.pterodactylus.util.swing.SwingUtils;
 
 /**
  * Defines the main window of the application.
- * 
+ *
  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
  * @version $Id$
  */
-public class MainWindow extends JFrame implements I18nable {
+public class MainWindow extends JFrame implements WindowListener, I18nable {
 
        /** The swing interface that receives all actions. */
        private final SwingInterface swingInterface;
@@ -68,19 +71,34 @@ public class MainWindow extends JFrame implements I18nable {
        /** The node menu. */
        private I18nMenu nodeMenu;
 
+       /** The “connect” (advanced mode) menu. */
+       private I18nMenu connectMenu;
+
+       /** The “connect” (simple mode) menu. */
+       private JMenuItem connectMenuItem;
+
+       /** The “disconnect” (advanced mode) menu. */
+       private I18nMenu disconnectMenu;
+
+       /** The “diconnect” (simple mode) menu item. */
+       private JMenuItem disconnectMenuItem;
+
        /** The language menu. */
        private I18nMenu languageMenu;
 
        /** The about menu. */
-       private I18nMenu aboutMenu;
-       
+       private I18nMenu helpMenu;
+
        /** The tabbed project pane. */
        private JTabbedPane projectPane;
 
+       /** The project overview panel. */
+       private Box projectOverviewPanel;
+
        /**
         * Creates a new main window that redirects all actions to the given swing
         * interface.
-        * 
+        *
         * @param swingInterface
         *            The swing interface to receive all actions
         */
@@ -92,7 +110,7 @@ public class MainWindow extends JFrame implements I18nable {
                pack();
                SwingUtils.center(this);
                I18n.registerI18nable(this);
-               setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+               addWindowListener(this);
        }
 
        //
@@ -101,7 +119,7 @@ public class MainWindow extends JFrame implements I18nable {
 
        /**
         * Sets the text of the status bar.
-        * 
+        *
         * @param text
         *            The text of the status bar
         */
@@ -110,6 +128,20 @@ public class MainWindow extends JFrame implements I18nable {
        }
 
        /**
+        * Sets whether the advanced mode is activated.
+        *
+        * @param advancedMode
+        *            <code>true</code> if the advanced mode is activated,
+        *            <code>false</code> if the simple mode is activated
+        */
+       public void setAdvancedMode(boolean advancedMode) {
+               connectMenu.setVisible(advancedMode);
+               connectMenuItem.setVisible(!advancedMode);
+               disconnectMenu.setVisible(advancedMode);
+               disconnectMenuItem.setVisible(!advancedMode);
+       }
+
+       /**
         * {@inheritDoc}
         */
        @Override
@@ -117,12 +149,44 @@ public class MainWindow extends JFrame implements I18nable {
                return contentPane;
        }
 
-       
+       /**
+        * Returns the currently selected project.
+        *
+        * @return The currently selected project
+        */
        public Project getSelectedProject() {
                return null;
        }
 
        //
+       // ACTIONS
+       //
+
+       /**
+        * Refreshes the menu items in the “connect” and “disconnect” menus.
+        */
+       void refreshNodeMenuItems() {
+               connectMenu.removeAll();
+               for (Action nodeConnectAction: swingInterface.getNodeConnectActions()) {
+                       connectMenu.add(nodeConnectAction);
+               }
+               if (connectMenu.getMenuComponentCount() == 0) {
+                       JMenuItem noNodeAvailableItem = new JMenuItem(I18n.get("mainWindow.menu.connectNoNodeAvailable.name"));
+                       noNodeAvailableItem.setEnabled(false);
+                       connectMenu.add(noNodeAvailableItem);
+               }
+               disconnectMenu.removeAll();
+               for (Action nodeDisconnectAction: swingInterface.getNodeDisconnectActions()) {
+                       disconnectMenu.add(nodeDisconnectAction);
+               }
+               if (disconnectMenu.getMenuComponentCount() == 0) {
+                       JMenuItem noNodeAvailableItem = new JMenuItem(I18n.get("mainWindow.menu.disconnectNoNodeAvailable.name"));
+                       noNodeAvailableItem.setEnabled(false);
+                       disconnectMenu.add(noNodeAvailableItem);
+               }
+       }
+
+       //
        // PRIVATE METHODS
        //
 
@@ -141,26 +205,35 @@ public class MainWindow extends JFrame implements I18nable {
                jSiteMenu.addSeparator();
                jSiteMenu.add(new FixedJMenuItem(swingInterface.getQuitAction()));
 
+               connectMenu = new I18nMenu("mainWindow.menu.connect");
+               disconnectMenu = new I18nMenu("mainWindow.menu.disconnect");
+
                nodeMenu = new I18nMenu("mainWindow.menu.node");
                menuBar.add(nodeMenu);
 
                nodeMenu.add(new FixedJMenuItem(swingInterface.getManageNodesAction()));
                nodeMenu.addSeparator();
-               nodeMenu.add(new FixedJMenuItem(swingInterface.getNodeConnectAction()));
-               nodeMenu.add(new FixedJMenuItem(swingInterface.getNodeDisconnectAction()));
+               nodeMenu.add(connectMenuItem = new FixedJMenuItem(swingInterface.getNodeConnectAction()));
+               nodeMenu.add(connectMenu);
+               nodeMenu.add(disconnectMenuItem = new FixedJMenuItem(swingInterface.getNodeDisconnectAction()));
+               nodeMenu.add(disconnectMenu);
+               refreshNodeMenuItems();
 
                languageMenu = new I18nMenu("mainWindow.menu.language");
                menuBar.add(languageMenu);
 
                for (I18nAction languageAction: swingInterface.getLanguageActions()) {
-                       languageMenu.add(languageAction);
+                       languageMenu.add(new FixedJMenuItem(languageAction));
                }
 
-               menuBar.add(new JPanel());
-               aboutMenu = new I18nMenu("mainWindow.menu.help");
-               menuBar.add(aboutMenu);
+               JPanel spacerPanel = new JPanel();
+               spacerPanel.setOpaque(false);
+               menuBar.add(spacerPanel);
 
-               aboutMenu.add(new FixedJMenuItem(swingInterface.getHelpAboutAction()));
+               helpMenu = new I18nMenu("mainWindow.menu.help");
+               menuBar.add(helpMenu);
+
+               helpMenu.add(new FixedJMenuItem(swingInterface.getHelpAboutAction()));
 
                setJMenuBar(menuBar);
 
@@ -207,8 +280,9 @@ public class MainWindow extends JFrame implements I18nable {
                projectPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
                upperPanel.add(projectPane, BorderLayout.CENTER);
 
-               Box projectOverviewPanel = new Box(BoxLayout.PAGE_AXIS);
-               projectPane.add(I18n.get("mainWindow.pane.overview.title"), projectOverviewPanel);
+               projectOverviewPanel = new Box(BoxLayout.PAGE_AXIS);
+               projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
+               projectPane.add(projectOverviewPanel);
                projectOverviewPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
                projectOverviewPanel.add(Box.createVerticalGlue());
                JButton addProjectButton = new JButton(swingInterface.getAddProjectAction());
@@ -228,13 +302,85 @@ public class MainWindow extends JFrame implements I18nable {
         * {@inheritDoc}
         */
        public void updateI18n() {
+               swingInterface.getConfigureAction().updateI18n();
+               swingInterface.getImportConfigAction().updateI18n();
+               swingInterface.getQuitAction().updateI18n();
                swingInterface.getManageNodesAction().updateI18n();
                swingInterface.getNodeConnectAction().updateI18n();
+               connectMenu.updateI18n();
                swingInterface.getNodeDisconnectAction().updateI18n();
+               disconnectMenu.updateI18n();
+               swingInterface.getAddProjectAction().updateI18n();
+               swingInterface.getCloneProjectAction().updateI18n();
+               swingInterface.getDeleteProjectAction().updateI18n();
+               swingInterface.getHelpAboutAction().updateI18n();
+               jSiteMenu.updateI18n();
                nodeMenu.updateI18n();
                languageMenu.updateI18n();
+               for (I18nAction languageAction: swingInterface.getLanguageActions()) {
+                       languageAction.updateI18n();
+               }
+               helpMenu.updateI18n();
                getJMenuBar().revalidate();
+               projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
+               for (int componentIndex = 0; componentIndex < projectPane.getTabCount(); componentIndex++) {
+                       projectPane.setTitleAt(componentIndex, projectPane.getComponentAt(componentIndex).getName());
+               }
+               refreshNodeMenuItems();
                SwingUtils.repackCentered(this);
        }
 
+       //
+       // INTERFACE WindowListener
+       //
+       
+       /**
+        * {@inheritDoc}
+        */
+       public void windowActivated(WindowEvent e) {
+               /* do nothing. */
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void windowClosed(WindowEvent e) {
+               /* do nothing. */
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void windowClosing(WindowEvent e) {
+               swingInterface.getQuitAction().actionPerformed(null);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void windowDeactivated(WindowEvent e) {
+               /* do nothing. */
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void windowDeiconified(WindowEvent e) {
+               /* do nothing. */
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void windowIconified(WindowEvent e) {
+               /* do nothing. */
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void windowOpened(WindowEvent e) {
+               /* do nothing. */
+       }
+
 }