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.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
+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;
/** 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));
/** 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 project overview panel. */
private Box projectOverviewPanel;
+ /** 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.
* <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. */
}
/**
//
/**
- * 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));
+ nodeMenu.add(newNodeMenu);
+ NodeLabel nodeLabel = new NodeLabel(swingInterface, node, onlineIcon, offlineIcon, errorIcon);
+ nodeLabels.put(node, nodeLabel);
+ statusBar.addSideComponent(nodeLabel);
+ }
+
+ /**
+ * 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));
}
/**
* 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");
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(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);
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);
JToolBar toolBar = new JToolBar(I18n.get("mainWindow.toolbar.name"));
toolBar.add(swingInterface.getManageNodesAction());
toolBar.addSeparator();
- toolBar.add(swingInterface.getNodeConnectAction());
- toolBar.add(swingInterface.getNodeDisconnectAction());
+ toolBar.add(swingInterface.getQuitAction());
super.getContentPane().add(toolBar, BorderLayout.PAGE_START);
super.getContentPane().add(contentPane, BorderLayout.CENTER);
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();
jSiteMenu.updateI18n();
nodeMenu.updateI18n();
languageMenu.updateI18n();
+ for (Node node: swingInterface.getNodes()) {
+ swingInterface.getNodeConnectAction(node).updateI18n();
+ swingInterface.getNodeDisconnectAction(node).updateI18n();
+ }
for (I18nAction languageAction: swingInterface.getLanguageActions()) {
languageAction.updateI18n();
}
((ProjectPanel) tabComponent).updateI18n();
}
}
- refreshNodeMenuItems();
SwingUtils.repackCentered(this);
}
package net.pterodactylus.jsite.gui;
import java.awt.event.ActionEvent;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
-import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
* @author David ‘Bombe’ Roden <bombe@freenetproject.org>
* @version $Id$
*/
-public class SwingInterface implements CoreListener, LoggingListener {
+public class SwingInterface implements CoreListener, LoggingListener, PropertyChangeListener {
/** The logger. */
private static final Logger logger = Logging.getLogger(SwingInterface.class.getName());
/** The “manage nodes” action. */
private I18nAction manageNodesAction;
- /** The “connect to node” (simple mode) action. */
- private I18nAction nodeConnectAction;
-
- /** The “disconnect from node” (simple mode) action. */
- private I18nAction nodeDisconnectAction;
-
/** All node menu items. */
- private List<Action> nodeConnectActions = Collections.synchronizedList(new ArrayList<Action>());
-
- /** Mapping from nodes to node connect actions. */
- private Map<Node, Action> nodeNodeConnectActions = Collections.synchronizedMap(new HashMap<Node, Action>());
-
- /** Mapping from node connect actions to nodes. */
- private Map<Action, Node> nodeConnectActionNodes = Collections.synchronizedMap(new HashMap<Action, Node>());
+ private Map<Node, I18nAction> nodeConnectActions = Collections.synchronizedMap(new HashMap<Node, I18nAction>());
/** All node disconnect actions. */
- private List<Action> nodeDisconnectActions = Collections.synchronizedList(new ArrayList<Action>());
-
- /** Mapping from nodes to node disconnect actions. */
- private Map<Node, Action> nodeNodeDisconnectActions = Collections.synchronizedMap(new HashMap<Node, Action>());
-
- /** Mapping from node disconnect actions to nodes. */
- private Map<Action, Node> nodeDisconnectActionNodes = Collections.synchronizedMap(new HashMap<Action, Node>());
+ private Map<Node, I18nAction> nodeDisconnectActions = Collections.synchronizedMap(new HashMap<Node, I18nAction>());
/** The node manager dialog. */
private ManageNodesDialog manageNodesDialog;
}
/**
- * Returns the “connect to node” action.
+ * Returns the “connect to node” action for the given node.
*
+ * @param node
+ * The node go get the “connect” action for
* @return The “connect to node” action
*/
- I18nAction getNodeConnectAction() {
- return nodeConnectAction;
+ I18nAction getNodeConnectAction(Node node) {
+ return nodeConnectActions.get(node);
}
/**
- * Returns all “connect node” actions.
- *
- * @return All “connect node” actions
- */
- List<Action> getNodeConnectActions() {
- return nodeConnectActions;
- }
-
- /**
- * Returns the “disconnect from node” action.
+ * Returns the “disconnect from node” action for the given node.
*
+ * @param node
+ * The node go get the “disconnect” action for
* @return The “disconnect from node” action
*/
- I18nAction getNodeDisconnectAction() {
- return nodeDisconnectAction;
- }
-
- /**
- * Returns all “disconnect node” actions.
- *
- * @return All “disconnect node” action
- */
- List<Action> getNodeDisconnectActions() {
- return nodeDisconnectActions;
+ I18nAction getNodeDisconnectAction(Node node) {
+ return nodeDisconnectActions.get(node);
}
/**
return deleteProjectAction;
}
+ /**
+ * Returns all currently configured nodes.
+ *
+ * @return All configured nodes
+ */
+ List<Node> getNodes() {
+ return nodeList;
+ }
+
//
// ACTIONS
//
manageNodes();
}
};
- nodeConnectAction = new I18nAction("mainWindow.menu.node.item.connect", false) {
-
- @SuppressWarnings("synthetic-access")
- public void actionPerformed(ActionEvent actionEvent) {
- List<Node> nodes = core.getNodes();
- if (nodes.isEmpty()) {
- return;
- }
- nodeConnect(nodes.get(0));
- }
-
- };
- nodeDisconnectAction = new I18nAction("mainWindow.menu.node.item.disconnect", false) {
-
- /**
- * {@inheritDoc}
- */
- @SuppressWarnings("synthetic-access")
- public void actionPerformed(ActionEvent e) {
- List<Node> nodes = core.getNodes();
- if (nodes.isEmpty()) {
- return;
- }
- nodeDisconnect(nodes.get(0));
- }
- };
- rebuildNodeActions(core.getNodes());
List<Locale> availableLanguages = I18n.findAvailableLanguages();
for (final Locale locale: availableLanguages) {
I18nAction languageAction = new I18nAction("general.language." + locale.getLanguage()) {
}
/**
- * Rebuilds all node connect and disconnect actions.
- *
- * @param nodes
- * The list of nodes
- */
- private void rebuildNodeActions(List<Node> nodes) {
- logger.fine("rebuilding node actions…");
- nodeConnectActions.clear();
- nodeNodeConnectActions.clear();
- nodeConnectActionNodes.clear();
- nodeDisconnectActions.clear();
- nodeNodeDisconnectActions.clear();
- nodeDisconnectActionNodes.clear();
- for (Node node: nodes) {
- logger.finer("adding node “" + node + "” to menus");
- Action nodeConnectAction = new AbstractAction(node.getName()) {
-
- /**
- * {@inheritDoc}
- */
- @SuppressWarnings("synthetic-access")
- public void actionPerformed(ActionEvent e) {
- Node node = nodeConnectActionNodes.get(this);
- nodeConnect(node);
- }
- };
- nodeConnectActions.add(nodeConnectAction);
- nodeConnectActionNodes.put(nodeConnectAction, node);
- nodeNodeConnectActions.put(node, nodeConnectAction);
- Action nodeDisconnectAction = new AbstractAction(node.getName()) {
-
- /**
- * {@inheritDoc}
- */
- @SuppressWarnings("synthetic-access")
- public void actionPerformed(ActionEvent e) {
- Node node = nodeDisconnectActionNodes.get(this);
- nodeDisconnect(node);
- }
- };
- // nodeDisconnectActions.add(nodeDisconnectAction);
- nodeDisconnectActionNodes.put(nodeDisconnectAction, node);
- nodeNodeDisconnectActions.put(node, nodeDisconnectAction);
- }
- }
-
- /**
* Pops up the “manage nodes” dialog.
*/
private void manageNodes() {
manageNodesDialog.setNodeList(nodeList);
manageNodesDialog.setVisible(true);
nodeList = manageNodesDialog.getNodeList();
- rebuildNodeActions(nodeList);
- mainWindow.refreshNodeMenuItems();
+ /* TODO - notify main window of changes */
} else {
if (nodeList.isEmpty()) {
Node newNode = new Node();
/**
* {@inheritDoc}
*/
- public void nodeAdded(Node node) {
+ public void nodeAdded(final Node node) {
logger.log(Level.INFO, "node added: " + node);
nodeList.add(node);
+ node.addPropertyChangeListener(this);
logger.log(Level.FINE, "nodeList.size(): " + nodeList.size());
manageNodesDialog.setNodeList(nodeList);
- rebuildNodeActions(nodeList);
- mainWindow.refreshNodeMenuItems();
+ nodeConnectActions.put(node, new I18nAction("mainWindow.menu.connect") {
+
+ /**
+ * {@inheritDoc}
+ */
+ @SuppressWarnings("synthetic-access")
+ public void actionPerformed(ActionEvent e) {
+ nodeConnect(node);
+ }
+ });
+ nodeDisconnectActions.put(node, new I18nAction("mainWindow.menu.disconnect") {
+
+ /**
+ * {@inheritDoc}
+ */
+ @SuppressWarnings("synthetic-access")
+ public void actionPerformed(ActionEvent e) {
+ nodeDisconnect(node);
+ }
+ });
+ nodeDisconnectActions.get(node).setEnabled(false);
+ mainWindow.addNode(node);
}
/**
public void nodeRemoved(Node node) {
logger.log(Level.INFO, "node removed: " + node);
nodeList.remove(node);
- rebuildNodeActions(nodeList);
- mainWindow.refreshNodeMenuItems();
+ node.removePropertyChangeListener(this);
+ nodeConnectActions.remove(node);
+ nodeDisconnectActions.remove(node);
+ mainWindow.removeNode(node);
}
/**
* {@inheritDoc}
*/
public void nodeConnecting(Node node) {
- Action nodeConnectAction = nodeNodeConnectActions.get(node);
- nodeConnectActions.remove(nodeConnectAction);
+ nodeConnectActions.get(node).setEnabled(false);
mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.connectingToNode", node.getName(), node.getHostname(), node.getPort()));
- mainWindow.refreshNodeMenuItems();
}
/**
* {@inheritDoc}
*/
public void nodeConnected(Node node) {
- Action nodeDisconnectAction = nodeNodeDisconnectActions.get(node);
- nodeDisconnectActions.add(nodeDisconnectAction);
+ nodeDisconnectActions.get(node).setEnabled(true);
mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.connectedToNode", node.getName(), node.getHostname(), node.getPort()));
- mainWindow.refreshNodeMenuItems();
}
/**
* {@inheritDoc}
*/
public void nodeConnectionFailed(Node node, Throwable cause) {
- Action nodeConnectAction = nodeNodeConnectActions.get(node);
- nodeConnectActions.add(nodeConnectAction);
+ nodeConnectActions.get(node).setEnabled(true);
mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.connectionToNodeFailed", node.getName(), node.getHostname(), node.getPort(), (cause != null) ? cause.getMessage() : "no reason given"));
JOptionPane.showMessageDialog(mainWindow, I18n.get("mainWindow.error.nodeConnectionFailed.message", node.getName(), node.getHostname(), node.getPort(), (cause != null) ? cause.getMessage() : "no reason given"), I18n.get("mainWindow.error.nodeConnectionFailed.title"), JOptionPane.ERROR_MESSAGE);
- mainWindow.refreshNodeMenuItems();
}
/**
* {@inheritDoc}
*/
public void nodeDisconnected(Node node, Throwable throwable) {
- Action nodeConnectAction = nodeNodeConnectActions.get(node);
- nodeConnectActions.add(nodeConnectAction);
- Action nodeDisconnectAction = nodeNodeDisconnectActions.get(node);
- nodeDisconnectActions.remove(nodeDisconnectAction);
+ nodeDisconnectActions.get(node).setEnabled(false);
+ nodeConnectActions.get(node).setEnabled(true);
mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.disconnectedFromNode", node.getName(), node.getHostname(), node.getPort()));
- mainWindow.refreshNodeMenuItems();
}
/**
logWindow.logged(logRecord);
}
+ //
+ // INTERFACE PropertyChangeListener
+ //
+
+ /**
+ * {@inheritDoc}
+ */
+ public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
+ if (propertyChangeEvent.getSource() instanceof Node) {
+ Node changedNode = (Node) propertyChangeEvent.getSource();
+ nodeConnectActions.get(changedNode).putValue(Action.NAME, changedNode.getName());
+ nodeDisconnectActions.get(changedNode).putValue(Action.NAME, changedNode.getName());
+ }
+ }
+
}