use panel instead of box for add project button
[jSite2.git] / src / net / pterodactylus / jsite / gui / MainWindow.java
index 6708f5e..2a50313 100644 (file)
 package net.pterodactylus.jsite.gui;
 
 import java.awt.BorderLayout;
+import java.awt.Component;
 import java.awt.Container;
 import java.awt.Dimension;
+import java.awt.GridBagConstraints;
+import java.awt.GridBagLayout;
+import java.awt.Insets;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
 import java.awt.event.WindowListener;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Timer;
 import java.util.TimerTask;
+import java.util.logging.Logger;
 
-import javax.swing.Action;
 import javax.swing.Box;
-import javax.swing.BoxLayout;
+import javax.swing.Icon;
 import javax.swing.JButton;
 import javax.swing.JFrame;
+import javax.swing.JMenu;
 import javax.swing.JMenuBar;
-import javax.swing.JMenuItem;
+import javax.swing.JOptionPane;
 import javax.swing.JPanel;
-import javax.swing.JScrollPane;
 import javax.swing.JTabbedPane;
-import javax.swing.JTable;
 import javax.swing.JToolBar;
 import javax.swing.SwingConstants;
 import javax.swing.border.EmptyBorder;
 
-import net.pterodactylus.jsite.core.Project;
+import net.pterodactylus.jsite.core.Node;
 import net.pterodactylus.jsite.i18n.I18n;
 import net.pterodactylus.jsite.i18n.I18nable;
 import net.pterodactylus.jsite.i18n.gui.I18nAction;
 import net.pterodactylus.jsite.i18n.gui.I18nMenu;
 import net.pterodactylus.jsite.main.Version;
+import net.pterodactylus.jsite.project.Project;
+import net.pterodactylus.util.image.IconLoader;
+import net.pterodactylus.util.logging.Logging;
 import net.pterodactylus.util.swing.StatusBar;
 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 WindowListener, I18nable {
+public class MainWindow extends JFrame implements WindowListener, I18nable, PropertyChangeListener {
+
+       /** Logger. */
+       @SuppressWarnings("unused")
+       private static final Logger logger = Logging.getLogger(MainWindow.class.getName());
 
        /** The swing interface that receives all actions. */
        private final SwingInterface swingInterface;
@@ -75,6 +88,15 @@ public class MainWindow extends JFrame implements WindowListener, I18nable {
        /** Delay (in seconds) after which to clear status bar. */
        private int statusBarClearDelay = 5000;
 
+       /** The icon for offline nodes. */
+       private Icon offlineIcon;
+
+       /** The icon for online nodes. */
+       private Icon onlineIcon;
+
+       /** The icon for error nodes. */
+       private Icon errorIcon;
+
        /** The content pane. */
        private JPanel contentPane = new JPanel(new BorderLayout(12, 12));
 
@@ -84,18 +106,6 @@ public class MainWindow extends JFrame implements WindowListener, 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;
 
@@ -106,15 +116,18 @@ public class MainWindow extends JFrame implements WindowListener, I18nable {
        private JTabbedPane projectPane;
 
        /** The project overview panel. */
-       private Box projectOverviewPanel;
+       private JPanel projectOverviewPanel;
 
-       /** The request table. */
-       private JTable requestTable;
+       /** Maps from node to menus. */
+       private final Map<Node, JMenu> nodeMenus = new HashMap<Node, JMenu>();
+
+       /** Maps from nodes to node panels. */
+       private final Map<Node, NodeLabel> nodeLabels = new HashMap<Node, NodeLabel>();
 
        /**
         * Creates a new main window that redirects all actions to the given swing
         * interface.
-        * 
+        *
         * @param swingInterface
         *            The swing interface to receive all actions
         */
@@ -127,6 +140,7 @@ public class MainWindow extends JFrame implements WindowListener, I18nable {
                SwingUtils.center(this);
                I18n.registerI18nable(this);
                addWindowListener(this);
+               setIconImage(IconLoader.loadImage("/jSite-frame-icon.png"));
        }
 
        //
@@ -135,7 +149,7 @@ public class MainWindow extends JFrame implements WindowListener, I18nable {
 
        /**
         * Sets the text of the status bar.
-        * 
+        *
         * @param text
         *            The text of the status bar
         */
@@ -160,7 +174,7 @@ public class MainWindow extends JFrame implements WindowListener, I18nable {
 
        /**
         * Returns the status bar clear delay (in milliseconds).
-        * 
+        *
         * @return The status bar clear delay
         */
        public int getStatusBarClearDelay() {
@@ -169,7 +183,7 @@ public class MainWindow extends JFrame implements WindowListener, I18nable {
 
        /**
         * Sets the status bar clear delay (in milliseconds).
-        * 
+        *
         * @param statusBarClearDelay
         *            The status bar clear delay
         */
@@ -179,16 +193,13 @@ public class MainWindow extends JFrame implements WindowListener, 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);
+               /* doesn’t do anything right now. */
        }
 
        /**
@@ -201,50 +212,160 @@ public class MainWindow extends JFrame implements WindowListener, I18nable {
 
        /**
         * Returns the currently selected project.
-        * 
+        *
         * @return The currently selected project
         */
        public Project getSelectedProject() {
                return null;
        }
 
+       /**
+        * Sets the given node to the “online” state.
+        *
+        * @param node
+        *            The node to set online
+        */
+       public void setOnline(Node node) {
+               nodeLabels.get(node).setOnline();
+       }
+
+       /**
+        * Sets the given node to the “offline” state in the status bar.
+        *
+        * @param node
+        *            The node to set offline
+        */
+       public void setOffline(Node node) {
+               nodeLabels.get(node).setOffline();
+       }
+
+       /**
+        * Sets the given node to the “error” state in the status bar.
+        *
+        * @param node
+        *            The node to set the error state for
+        */
+       public void setError(Node node) {
+               nodeLabels.get(node).setError();
+       }
+
        //
        // ACTIONS
        //
 
        /**
-        * Refreshes the menu items in the “connect” and “disconnect” menus.
+        * Adds a node to the menu.
+        *
+        * @param node
+        *            The node to add
         */
-       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);
-               }
+       void addNode(Node node) {
+               JMenu newNodeMenu = new JMenu(node.getName());
+               nodeMenus.put(node, newNodeMenu);
+               newNodeMenu.add(swingInterface.getNodeConnectAction(node));
+               newNodeMenu.add(swingInterface.getNodeDisconnectAction(node));
+               newNodeMenu.addSeparator();
+               newNodeMenu.add(swingInterface.getNodeEditAction(node));
+               newNodeMenu.add(swingInterface.getNodeDeleteAction(node));
+               nodeMenu.add(newNodeMenu);
+               NodeLabel nodeLabel = new NodeLabel(swingInterface, node, onlineIcon, offlineIcon, errorIcon);
+               nodeLabels.put(node, nodeLabel);
+               statusBar.addSideComponent(nodeLabel);
+               node.addPropertyChangeListener(this);
+       }
+
+       /**
+        * Removes a node from the menu.
+        *
+        * @param node
+        *            The node to remove
+        */
+       void removeNode(Node node) {
+               nodeMenu.remove(nodeMenus.remove(node));
+               statusBar.removeSideComponent(nodeLabels.remove(node));
+               node.removePropertyChangeListener(this);
        }
 
        /**
         * Adds a project to the project pane.
-        * 
+        *
         * @param project
         *            The project to add
+        * @param switchToProject
+        *            <code>true</code> to switch to the new panel,
+        *            <code>false</code> to not change the current panel
         */
-       void addProject(Project project) {
+       void addProject(Project project, boolean switchToProject) {
                ProjectPanel projectPanel = new ProjectPanel(swingInterface, project);
+               int newTabIndex = projectPane.getTabCount();
                projectPane.add(project.getName(), projectPanel);
+               projectPane.setToolTipTextAt(newTabIndex, project.getDescription());
+               project.addPropertyChangeListener(this);
+               if (switchToProject) {
+                       projectPane.setSelectedIndex(newTabIndex);
+                       while (project.getBasePath().length() == 0) {
+                               JOptionPane.showMessageDialog(this, I18n.get("mainWindow.information.changeProjectBasePath.message"), I18n.get("mainWindow.information.changeProjectBasePath.title"), JOptionPane.INFORMATION_MESSAGE);
+                               projectPanel.changeBasePath();
+                       }
+               }
+       }
+
+       /**
+        * Removes the pane containing the given project.
+        *
+        * @param project
+        *            The project whose pane to remove
+        */
+       void removeProject(Project project) {
+               int projectIndex = getProjectIndex(project);
+               projectPane.remove(projectIndex);
+       }
+
+       /**
+        * @param project
+        */
+       void projectInsertStarted(Project project) {
+               int projectIndex = getProjectIndex(project);
+               if (projectIndex == -1) {
+                       return;
+               }
+               projectPane.setTitleAt(projectIndex, I18n.get("projectPanel.title.starting", project.getName()));
+       }
+
+       /**
+        * @param project
+        * @param totalBlocks
+        * @param requiredBlocks
+        * @param successfulBlocks
+        * @param failedBlocks
+        * @param fatallyFailedBlocks
+        * @param finalizedTotal
+        */
+       void projectInsertProgressed(Project project, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean finalizedTotal) {
+               int projectIndex = getProjectIndex(project);
+               if (projectIndex == -1) {
+                       return;
+               }
+               projectPane.setTitleAt(projectIndex, I18n.get("projectPanel.title.progress", project.getName(), requiredBlocks / (double) successfulBlocks));
+       }
+
+       /**
+        * @param project
+        */
+       void projectInsertGeneratedURI(Project project) {
+               /* TODO - update panel. */
+       }
+
+       /**
+        * @param project
+        * @param success
+        */
+       void projectInsertFinished(Project project, boolean success) {
+               int projectIndex = getProjectIndex(project);
+               if (projectIndex == -1) {
+                       return;
+               }
+               projectPane.setTitleAt(projectIndex, project.getName());
        }
 
        //
@@ -252,9 +373,34 @@ public class MainWindow extends JFrame implements WindowListener, I18nable {
        //
 
        /**
+        * Returns the index of the project panel that contains the given project.
+        *
+        * @param project
+        *            The wanted project
+        * @return The index of {@link #projectPane}’s tab that contains the given
+        *         project, or <code>-1</code> if the project can not be found
+        */
+       private int getProjectIndex(Project project) {
+               int tabCount = projectPane.getTabCount();
+               for (int tabIndex = 1; tabIndex < tabCount; tabIndex++) {
+                       Component tabComponent = projectPane.getComponentAt(tabIndex);
+                       if (tabComponent instanceof ProjectPanel) {
+                               if (((ProjectPanel) tabComponent).getProject() == project) {
+                                       return tabIndex;
+                               }
+                       }
+               }
+               return -1;
+       }
+
+       /**
         * Initializes the window by creating all its components.
         */
        private void initWindow() {
+               onlineIcon = IconLoader.loadIcon("/node-online.png");
+               offlineIcon = IconLoader.loadIcon("/node-offline.png");
+               errorIcon = IconLoader.loadIcon("/node-error.png");
+
                JMenuBar menuBar = new JMenuBar();
 
                jSiteMenu = new I18nMenu("mainWindow.menu.jSite");
@@ -266,19 +412,11 @@ public class MainWindow extends JFrame implements WindowListener, 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.add(new FixedJMenuItem(swingInterface.getAddNodeAction()));
                nodeMenu.addSeparator();
-               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);
@@ -287,9 +425,7 @@ public class MainWindow extends JFrame implements WindowListener, I18nable {
                        languageMenu.add(new FixedJMenuItem(languageAction));
                }
 
-               JPanel spacerPanel = new JPanel();
-               spacerPanel.setOpaque(false);
-               menuBar.add(spacerPanel);
+               menuBar.add(Box.createHorizontalGlue());
 
                helpMenu = new I18nMenu("mainWindow.menu.help");
                menuBar.add(helpMenu);
@@ -299,10 +435,10 @@ public class MainWindow extends JFrame implements WindowListener, I18nable {
                setJMenuBar(menuBar);
 
                JToolBar toolBar = new JToolBar(I18n.get("mainWindow.toolbar.name"));
-               toolBar.add(swingInterface.getManageNodesAction());
+               toolBar.add(swingInterface.getConfigureAction());
+               toolBar.add(swingInterface.getQuitAction());
                toolBar.addSeparator();
-               toolBar.add(swingInterface.getNodeConnectAction());
-               toolBar.add(swingInterface.getNodeDisconnectAction());
+               toolBar.add(swingInterface.getAddNodeAction());
                super.getContentPane().add(toolBar, BorderLayout.PAGE_START);
 
                super.getContentPane().add(contentPane, BorderLayout.CENTER);
@@ -327,35 +463,17 @@ public class MainWindow extends JFrame implements WindowListener, I18nable {
         */
        private void initComponents() {
                super.getContentPane().add(statusBar, BorderLayout.PAGE_END);
-
-               /*
-                * the main window consists of two panels which are vertically oriented.
-                * the upper panel contains of a tabbed pane, the lower panel consists
-                * of a table that lists the running requests.
-                */
-
-               JPanel upperPanel = new JPanel(new BorderLayout(12, 12));
-               getContentPane().add(upperPanel, BorderLayout.PAGE_START);
                contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
 
                projectPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
-               upperPanel.add(projectPane, BorderLayout.CENTER);
+               contentPane.add(projectPane, BorderLayout.CENTER);
 
-               projectOverviewPanel = new Box(BoxLayout.PAGE_AXIS);
+               projectOverviewPanel = new JPanel(new GridBagLayout());
                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());
-               addProjectButton.setAlignmentX(0.5f);
-               projectOverviewPanel.add(addProjectButton);
-               projectOverviewPanel.add(Box.createVerticalGlue());
-
-               requestTable = new JTable(swingInterface.getRequestTableModel());
-               getContentPane().add(new JScrollPane(requestTable), BorderLayout.CENTER);
-
-// JPanel lowerPanel = new JPanel(new BorderLayout(12, 12));
-// getContentPane().add(lowerPanel, BorderLayout.CENTER);
+               projectOverviewPanel.add(addProjectButton, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
        }
 
        //
@@ -369,29 +487,34 @@ public class MainWindow extends JFrame implements WindowListener, I18nable {
                swingInterface.getConfigureAction().updateI18n();
                swingInterface.getImportConfigAction().updateI18n();
                swingInterface.getQuitAction().updateI18n();
-               swingInterface.getManageNodesAction().updateI18n();
-               swingInterface.getNodeConnectAction().updateI18n();
-               connectMenu.updateI18n();
-               swingInterface.getNodeDisconnectAction().updateI18n();
-               disconnectMenu.updateI18n();
+               swingInterface.getAddNodeAction().updateI18n();
                swingInterface.getAddProjectAction().updateI18n();
-               swingInterface.getCloneProjectAction().updateI18n();
-               swingInterface.getDeleteProjectAction().updateI18n();
                swingInterface.getHelpAboutAction().updateI18n();
                jSiteMenu.updateI18n();
                nodeMenu.updateI18n();
                languageMenu.updateI18n();
+               for (Node node: swingInterface.getNodes()) {
+                       swingInterface.getNodeConnectAction(node).updateI18n();
+                       swingInterface.getNodeDisconnectAction(node).updateI18n();
+                       swingInterface.getNodeEditAction(node).updateI18n();
+                       swingInterface.getNodeDeleteAction(node).updateI18n();
+               }
+               for (Project project: swingInterface.getProjects()) {
+                       swingInterface.getCloneProjectAction(project).updateI18n();
+                       swingInterface.getDeleteProjectAction(project).updateI18n();
+               }
                for (I18nAction languageAction: swingInterface.getLanguageActions()) {
                        languageAction.updateI18n();
                }
                helpMenu.updateI18n();
                getJMenuBar().revalidate();
-               projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
+               projectPane.setTitleAt(0, I18n.get("mainWindow.pane.overview.title"));
                for (int componentIndex = 0; componentIndex < projectPane.getTabCount(); componentIndex++) {
-                       projectPane.setTitleAt(componentIndex, projectPane.getComponentAt(componentIndex).getName());
+                       Component tabComponent = projectPane.getComponentAt(componentIndex);
+                       if (tabComponent instanceof ProjectPanel) {
+                               ((ProjectPanel) tabComponent).updateI18n();
+                       }
                }
-               refreshNodeMenuItems();
-               SwingUtils.repackCentered(this);
        }
 
        //
@@ -447,4 +570,33 @@ public class MainWindow extends JFrame implements WindowListener, I18nable {
                /* do nothing. */
        }
 
+       //
+       // INTERFACE PropertyChangeListener
+       //
+
+       /**
+        * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
+        */
+       public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
+               Object eventSource = propertyChangeEvent.getSource();
+               String propertyName = propertyChangeEvent.getPropertyName();
+               if (eventSource instanceof Project) {
+                       /* if a project was changed, update the tab title and tooltip. */
+                       if (Project.PROPERTY_NAME.equals(propertyName) || Project.PROPERTY_DESCRIPTION.equals(propertyName)) {
+                               Project project = (Project) eventSource;
+                               int projectIndex = getProjectIndex(project);
+                               if (projectIndex != -1) {
+                                       projectPane.setTitleAt(projectIndex, project.getName());
+                                       projectPane.setToolTipTextAt(projectIndex, project.getDescription());
+                                       projectPane.repaint();
+                               }
+                       }
+               } else if (eventSource instanceof Node) {
+                       if (propertyName.equals(Node.PROPERTY_NAME)) {
+                               Node changedNode = (Node) eventSource;
+                               nodeMenus.get(changedNode).setText(changedNode.getName());
+                       }
+               }
+       }
+
 }