import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
+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;
/** 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;
}
/**
+ * 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
}
//
+ // ACTIONS
+ //
+
+ /**
+ * Refreshes the menu items in the “connect” and “disconnect” menus.
+ */
+ void refreshNodeMenuItems() {
+ connectMenu.removeAll();
+ for (Action nodeConnectAction: swingInterface.getNodeConnectActions()) {
+ connectMenu.add(nodeConnectAction);
+ }
+ disconnectMenu.removeAll();
+ for (Action nodeDisconnectAction: swingInterface.getNodeDisconnectActions()) {
+ disconnectMenu.add(nodeDisconnectAction);
+ }
+ }
+
+ //
// PRIVATE METHODS
//
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);
languageMenu = new I18nMenu("mainWindow.menu.language");
menuBar.add(languageMenu);
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();
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
import java.util.Locale;
+import java.util.Map;
import java.util.Properties;
-
+import java.util.concurrent.Executor;
+import java.util.concurrent.Executors;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+import java.util.logging.Logger;
+
+import javax.swing.AbstractAction;
+import javax.swing.Action;
import javax.swing.JOptionPane;
import net.pterodactylus.jsite.core.Core;
import net.pterodactylus.jsite.i18n.I18n;
import net.pterodactylus.jsite.i18n.gui.I18nAction;
import net.pterodactylus.util.io.Closer;
+import net.pterodactylus.util.logging.Logging;
+import net.pterodactylus.util.logging.LoggingListener;
/**
* The Swing user interface.
- *
+ *
* @author David ‘Bombe’ Roden <bombe@freenetproject.org>
* @version $Id$
*/
-public class SwingInterface implements CoreListener {
+public class SwingInterface implements CoreListener, LoggingListener {
+
+ /** The logger. */
+ private static final Logger logger = Logging.getLogger(SwingInterface.class.getName());
/** The application core. */
private final Core core;
/** The main window. */
private MainWindow mainWindow;
+ /** Thread pool. */
+ private Executor threadPool = Executors.newCachedThreadPool();
+
+ /** The logger window. */
+ private LogWindow logWindow;
+
/** The “configure” action. */
private I18nAction configureAction;
/** The “manage nodes” action. */
private I18nAction manageNodesAction;
- /** The “connect to node” action. */
+ /** The “connect to node” (simple mode) action. */
private I18nAction nodeConnectAction;
- /** The “disconnect from node” action. */
+ /** 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>());
+
+ /** 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>());
+
/** The node manager dialog. */
private ManageNodesDialog manageNodesDialog;
/**
* Creates a new swing interface.
- *
+ *
* @param core
* The core to operate on
* @param configDirectory
}
initActions();
initDialogs();
+ mainWindow = new MainWindow(this);
+ mainWindow.setAdvancedMode(advancedMode);
+ logWindow = new LogWindow();
+ logWindow.setVisible(true); /* TODO - remove */
}
//
/**
* Returns the core that is controlled by the Swing interface.
- *
+ *
* @return The core
*/
Core getCore() {
/**
* Returns the main window of the Swing interface.
- *
+ *
* @return The main window
*/
MainWindow getMainWindow() {
}
/**
+ * Returns whether the advanced mode is activated.
+ *
+ * @return <code>true</code> if the advanced mode is activated,
+ * <code>false</code> if the simple mode is activated
+ */
+ boolean isAdvancedMode() {
+ return advancedMode;
+ }
+
+ /**
* Returns the “configure” action.
- *
+ *
* @return The “configure” action
*/
I18nAction getConfigureAction() {
/**
* Returns the “import config” action.
- *
+ *
* @return The “import config” action
*/
I18nAction getImportConfigAction() {
/**
* Returns the “quit” action.
- *
+ *
* @return The “quit” action
*/
I18nAction getQuitAction() {
/**
* Returns the “manage nodes” action.
- *
+ *
* @return The “manage nodes” action
*/
I18nAction getManageNodesAction() {
/**
* Returns the “connect to node” action.
- *
+ *
* @return The “connect to node” action
*/
I18nAction getNodeConnectAction() {
}
/**
+ * Returns all “connect node” actions.
+ *
+ * @return All “connect node” actions
+ */
+ List<Action> getNodeConnectActions() {
+ return nodeConnectActions;
+ }
+
+ /**
* Returns the “disconnect from node” action.
- *
+ *
* @return The “disconnect from node” action
*/
I18nAction getNodeDisconnectAction() {
}
/**
+ * Returns all “disconnect node” actions.
+ *
+ * @return All “disconnect node” action
+ */
+ List<Action> getNodeDisconnectActions() {
+ return nodeDisconnectActions;
+ }
+
+ /**
* Returns all language actions.
- *
+ *
* @return All language actions
*/
List<I18nAction> getLanguageActions() {
/**
* Returns the “about” action.
- *
+ *
* @return The “about” action
*/
I18nAction getHelpAboutAction() {
/**
* Returns the “add project” action.
- *
+ *
* @return The “add project” action
*/
I18nAction getAddProjectAction() {
/**
* Returns the “clone project” action.
- *
+ *
* @return The “clone project” action
*/
I18nAction getCloneProjectAction() {
/**
* Returns the “delete project” action.
- *
+ *
* @return The “delete project” action
*/
I18nAction getDeleteProjectAction() {
// SERVICE METHODS
//
- /**
- * Starts the interface.
- */
- public void start() {
- mainWindow = new MainWindow(this);
- }
-
//
// PRIVATE METHODS
//
@SuppressWarnings("synthetic-access")
public void actionPerformed(ActionEvent actionEvent) {
- nodeConnect();
+ List<Node> nodes = core.getNodes();
+ if (nodes.isEmpty()) {
+ return;
+ }
+ nodeConnect(nodes.get(0));
}
};
*/
@SuppressWarnings("synthetic-access")
public void actionPerformed(ActionEvent e) {
- nodeDisconnect();
+ 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()) {
if (!advancedMode && (nodeList.size() > 1)) {
JOptionPane.showMessageDialog(mainWindow, I18n.get("mainWindow.warning.multipleNodesNotAdvancedMode.message"), I18n.get("mainWindow.warning.multipleNodesNotAdvancedMode.title"), JOptionPane.WARNING_MESSAGE);
}
+ mainWindow.setAdvancedMode(advancedMode);
antialias = configurationDialog.isAntialias();
controlFont = configurationDialog.getControlFont();
userFont = configurationDialog.getUserFont();
}
/**
+ * Rebuilds all node connect and disconnect actions.
+ *
+ * @param nodes
+ * The list of nodes
+ */
+ private void rebuildNodeActions(List<Node> nodes) {
+ nodeConnectActions.clear();
+ nodeNodeConnectActions.clear();
+ nodeConnectActionNodes.clear();
+ nodeDisconnectActions.clear();
+ nodeNodeDisconnectActions.clear();
+ nodeDisconnectActionNodes.clear();
+ for (Node node: nodes) {
+ 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();
} else {
if (nodeList.isEmpty()) {
Node newNode = new Node();
/**
* Connects to the node.
+ *
+ * @param node
+ * The node to connect to
*/
- private void nodeConnect() {
- /* TODO */
+ private void nodeConnect(final Node node) {
+ threadPool.execute(new Runnable() {
+
+ /**
+ * {@inheritDoc}
+ */
+ @SuppressWarnings("synthetic-access")
+ public void run() {
+ logger.log(Level.INFO, "connecting to node “" + node.getName() + "”…");
+ core.connectToNode(node);
+ }
+ });
}
/**
* Disconnects from the node.
+ *
+ * @param node
+ * The node to disconnect from
*/
- private void nodeDisconnect() {
- /* TODO */
+ private void nodeDisconnect(Node node) {
+ logger.log(Level.INFO, "disconnecting from node “" + node.getName() + "”…");
+ core.disconnectFromNode(node);
}
/**
* Changes the language of the interface. This method also disables the
* action for the newly set language and enables all others.
- *
+ *
* @param newLocale
* The new language
* @param languageAction
// INTERFACE CoreListener
//
-
/**
* {@inheritDoc}
*/
/**
* {@inheritDoc}
*/
+ public void nodeConnecting(Node node) {
+ Action nodeConnectAction = nodeNodeConnectActions.get(node);
+ nodeConnectActions.remove(nodeConnectAction);
+ mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.connectingToNode", node.getName(), node.getHostname(), node.getPort()));
+ mainWindow.refreshNodeMenuItems();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public void nodeConnected(Node node) {
- /* TODO */
+ Action nodeDisconnectAction = nodeNodeDisconnectActions.get(node);
+ nodeDisconnectActions.add(nodeDisconnectAction);
+ mainWindow.refreshNodeMenuItems();
}
/**
* {@inheritDoc}
*/
- public void nodeConnecting(Node node) {
- /* TODO */
+ public void nodeDisconnected(Node node, Throwable throwable) {
+ Action nodeConnectAction = nodeNodeConnectActions.get(node);
+ nodeConnectActions.add(nodeConnectAction);
+ Action nodeDisconnectAction = nodeNodeDisconnectActions.get(node);
+ nodeDisconnectActions.remove(nodeDisconnectAction);
+ mainWindow.refreshNodeMenuItems();
}
+ //
+ // INTERFACE LoggingListener
+ //
+
/**
* {@inheritDoc}
*/
- public void nodeDisconnected(Node node) {
- /* TODO */
+ public void logged(LogRecord logRecord) {
+ logWindow.logged(logRecord);
}
}