2 * jSite2 - SwingInterface.java -
3 * Copyright © 2008 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package net.pterodactylus.jsite.gui;
22 import java.awt.event.ActionEvent;
24 import java.io.FileInputStream;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Locale;
33 import java.util.Properties;
34 import java.util.concurrent.Executor;
35 import java.util.concurrent.Executors;
36 import java.util.logging.Level;
37 import java.util.logging.LogRecord;
38 import java.util.logging.Logger;
40 import javax.swing.AbstractAction;
41 import javax.swing.Action;
42 import javax.swing.JOptionPane;
44 import net.pterodactylus.jsite.core.Core;
45 import net.pterodactylus.jsite.core.CoreListener;
46 import net.pterodactylus.jsite.core.Node;
47 import net.pterodactylus.jsite.core.Project;
48 import net.pterodactylus.jsite.i18n.I18n;
49 import net.pterodactylus.jsite.i18n.gui.I18nAction;
50 import net.pterodactylus.util.io.Closer;
51 import net.pterodactylus.util.logging.Logging;
52 import net.pterodactylus.util.logging.LoggingListener;
55 * The Swing user interface.
57 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
60 public class SwingInterface implements CoreListener, LoggingListener {
63 private static final Logger logger = Logging.getLogger(SwingInterface.class.getName());
65 /** The application core. */
66 private final Core core;
68 /** The configuration directory. */
69 private final String configDirectory;
71 /** The main window. */
72 private MainWindow mainWindow;
75 private Executor threadPool = Executors.newCachedThreadPool();
77 /** The logger window. */
78 private LogWindow logWindow;
80 /** The “configure” action. */
81 private I18nAction configureAction;
83 /** The “import config” action. */
84 private I18nAction importConfigAction;
86 /** The “quit” action. */
87 private I18nAction quitAction;
89 /** The “manage nodes” action. */
90 private I18nAction manageNodesAction;
92 /** The “connect to node” (simple mode) action. */
93 private I18nAction nodeConnectAction;
95 /** The “disconnect from node” (simple mode) action. */
96 private I18nAction nodeDisconnectAction;
98 /** All node menu items. */
99 private List<Action> nodeConnectActions = Collections.synchronizedList(new ArrayList<Action>());
101 /** Mapping from nodes to node connect actions. */
102 private Map<Node, Action> nodeNodeConnectActions = Collections.synchronizedMap(new HashMap<Node, Action>());
104 /** Mapping from node connect actions to nodes. */
105 private Map<Action, Node> nodeConnectActionNodes = Collections.synchronizedMap(new HashMap<Action, Node>());
107 /** All node disconnect actions. */
108 private List<Action> nodeDisconnectActions = Collections.synchronizedList(new ArrayList<Action>());
110 /** Mapping from nodes to node disconnect actions. */
111 private Map<Node, Action> nodeNodeDisconnectActions = Collections.synchronizedMap(new HashMap<Node, Action>());
113 /** Mapping from node disconnect actions to nodes. */
114 private Map<Action, Node> nodeDisconnectActionNodes = Collections.synchronizedMap(new HashMap<Action, Node>());
116 /** The node manager dialog. */
117 private ManageNodesDialog manageNodesDialog;
119 /** All lanugage menu items. */
120 private List<I18nAction> languageActions = new ArrayList<I18nAction>();
122 /** The “about” action. */
123 private I18nAction helpAboutAction;
125 /** The “add project” action. */
126 private I18nAction addProjectAction;
128 /** The “clone project” action. */
129 private I18nAction cloneProjectAction;
131 /** The “delete project” action. */
132 private I18nAction deleteProjectAction;
134 /** The “about” dialog. */
135 private AboutDialog aboutDialog;
137 /** The configuration dialog. */
138 private ConfigurationDialog configurationDialog;
140 /** The list of all defined nodes. */
141 private List<Node> nodeList;
147 /** The advanced mode. */
148 private boolean advancedMode;
150 /** Whether to antialias the GUI. */
151 private boolean antialias;
153 /** The control font. */
154 private String controlFont;
156 /** The user font. */
157 private String userFont;
160 * Creates a new swing interface.
163 * The core to operate on
164 * @param configDirectory
165 * The directory the configuration is stored in
167 public SwingInterface(Core core, String configDirectory) {
169 this.configDirectory = configDirectory;
170 I18n.setLocale(Locale.ENGLISH);
173 System.setProperty("swing.aatext", "true");
175 if (controlFont != null) {
176 System.setProperty("swing.plaf.metal.controlFont", controlFont);
178 if (userFont != null) {
179 System.setProperty("swing.plaf.metal.userFont", userFont);
183 mainWindow = new MainWindow(this);
184 mainWindow.setAdvancedMode(advancedMode);
185 logWindow = new LogWindow();
186 logWindow.setVisible(true); /* TODO - remove */
194 * Returns the core that is controlled by the Swing interface.
203 * Returns the main window of the Swing interface.
205 * @return The main window
207 MainWindow getMainWindow() {
212 * Returns whether the advanced mode is activated.
214 * @return <code>true</code> if the advanced mode is activated,
215 * <code>false</code> if the simple mode is activated
217 boolean isAdvancedMode() {
222 * Returns the “configure” action.
224 * @return The “configure” action
226 I18nAction getConfigureAction() {
227 return configureAction;
231 * Returns the “import config” action.
233 * @return The “import config” action
235 I18nAction getImportConfigAction() {
236 return importConfigAction;
240 * Returns the “quit” action.
242 * @return The “quit” action
244 I18nAction getQuitAction() {
249 * Returns the “manage nodes” action.
251 * @return The “manage nodes” action
253 I18nAction getManageNodesAction() {
254 return manageNodesAction;
258 * Returns the “connect to node” action.
260 * @return The “connect to node” action
262 I18nAction getNodeConnectAction() {
263 return nodeConnectAction;
267 * Returns all “connect node” actions.
269 * @return All “connect node” actions
271 List<Action> getNodeConnectActions() {
272 return nodeConnectActions;
276 * Returns the “disconnect from node” action.
278 * @return The “disconnect from node” action
280 I18nAction getNodeDisconnectAction() {
281 return nodeDisconnectAction;
285 * Returns all “disconnect node” actions.
287 * @return All “disconnect node” action
289 List<Action> getNodeDisconnectActions() {
290 return nodeDisconnectActions;
294 * Returns all language actions.
296 * @return All language actions
298 List<I18nAction> getLanguageActions() {
299 return languageActions;
303 * Returns the “about” action.
305 * @return The “about” action
307 I18nAction getHelpAboutAction() {
308 return helpAboutAction;
312 * Returns the “add project” action.
314 * @return The “add project” action
316 I18nAction getAddProjectAction() {
317 return addProjectAction;
321 * Returns the “clone project” action.
323 * @return The “clone project” action
325 I18nAction getCloneProjectAction() {
326 return cloneProjectAction;
330 * Returns the “delete project” action.
332 * @return The “delete project” action
334 I18nAction getDeleteProjectAction() {
335 return deleteProjectAction;
351 * Loads the configuration of the interface.
353 private void loadConfig() {
354 /* initialize default stuff. */
356 /* now read config. */
357 File configFile = new File(configDirectory, "swing-interface.properties");
358 if (!configFile.exists() || !configFile.canRead() || !configFile.isFile()) {
359 System.err.println("could not find “" + configFile.getAbsolutePath() + "”!");
362 Properties configProperties = new Properties();
363 FileInputStream configInputStream = null;
365 configInputStream = new FileInputStream(configFile);
366 configProperties.load(configInputStream);
367 } catch (IOException ioe1) {
368 System.err.println("could not load config, " + ioe1.getMessage());
370 Closer.close(configInputStream);
372 if (configProperties.containsKey("advancedMode")) {
373 advancedMode = Boolean.valueOf(configProperties.getProperty("advancedMode"));
375 if (configProperties.containsKey("antialias")) {
376 antialias = Boolean.valueOf(configProperties.getProperty("antialias"));
378 if (configProperties.containsKey("controlFont")) {
379 controlFont = configProperties.getProperty("controlFont");
381 if (configProperties.containsKey("userFont")) {
382 userFont = configProperties.getProperty("userFont");
384 if (configProperties.containsKey("language")) {
385 I18n.setLocale(new Locale(configProperties.getProperty("language")));
390 * Saves the configuration.
392 private void saveConfig() {
393 File configDirectory = new File(this.configDirectory);
394 if (!configDirectory.exists()) {
395 if (!configDirectory.mkdirs()) {
396 System.err.println("could not create “" + this.configDirectory + "”!");
400 if (!configDirectory.exists() || !configDirectory.isDirectory() || !configDirectory.canWrite()) {
401 System.err.println("can not access “" + this.configDirectory + "”!");
404 File configFile = new File(configDirectory, "swing-interface.properties");
405 Properties configProperties = new Properties();
406 configProperties.setProperty("advancedMode", String.valueOf(advancedMode));
407 configProperties.setProperty("antialias", String.valueOf(antialias));
408 if (controlFont != null) {
409 configProperties.setProperty("controlFont", controlFont);
411 if (userFont != null) {
412 configProperties.setProperty("userFont", userFont);
414 configProperties.setProperty("language", I18n.getLocale().getLanguage());
415 FileOutputStream configOutputStream = null;
417 configOutputStream = new FileOutputStream(configFile);
418 configProperties.store(configOutputStream, "configuration of swing interface");
419 } catch (IOException ioe1) {
420 System.err.println("could not save config, " + ioe1.getMessage());
422 Closer.close(configOutputStream);
427 * Initializes all actions.
429 private void initActions() {
430 configureAction = new I18nAction("mainWindow.menu.jSite.configure") {
435 @SuppressWarnings("synthetic-access")
436 public void actionPerformed(ActionEvent actionEvent) {
440 importConfigAction = new I18nAction("mainWindow.menu.jSite.importConfig") {
445 @SuppressWarnings("synthetic-access")
446 public void actionPerformed(ActionEvent actionEvent) {
450 quitAction = new I18nAction("mainWindow.menu.jSite.quit") {
455 @SuppressWarnings("synthetic-access")
456 public void actionPerformed(ActionEvent actionEvent) {
460 manageNodesAction = new I18nAction("mainWindow.menu.node.item.manageNodes") {
465 @SuppressWarnings("synthetic-access")
466 public void actionPerformed(ActionEvent actionEvent) {
470 nodeConnectAction = new I18nAction("mainWindow.menu.node.item.connect", false) {
472 @SuppressWarnings("synthetic-access")
473 public void actionPerformed(ActionEvent actionEvent) {
474 List<Node> nodes = core.getNodes();
475 if (nodes.isEmpty()) {
478 nodeConnect(nodes.get(0));
482 nodeDisconnectAction = new I18nAction("mainWindow.menu.node.item.disconnect", false) {
487 @SuppressWarnings("synthetic-access")
488 public void actionPerformed(ActionEvent e) {
489 List<Node> nodes = core.getNodes();
490 if (nodes.isEmpty()) {
493 nodeDisconnect(nodes.get(0));
496 rebuildNodeActions(core.getNodes());
497 List<Locale> availableLanguages = I18n.findAvailableLanguages();
498 for (final Locale locale: availableLanguages) {
499 I18nAction languageAction = new I18nAction("general.language." + locale.getLanguage()) {
501 @SuppressWarnings("synthetic-access")
502 public void actionPerformed(ActionEvent e) {
503 changeLanguage(locale, this);
507 if (I18n.getLocale().getLanguage().equals(locale.getLanguage())) {
508 languageAction.setEnabled(false);
510 languageActions.add(languageAction);
512 helpAboutAction = new I18nAction("mainWindow.menu.help.item.about") {
517 @SuppressWarnings("synthetic-access")
518 public void actionPerformed(ActionEvent actionEvent) {
522 addProjectAction = new I18nAction("mainWindow.button.addProject") {
527 @SuppressWarnings("synthetic-access")
528 public void actionPerformed(ActionEvent actionEvent) {
532 cloneProjectAction = new I18nAction("mainWindow.button.cloneProject") {
537 @SuppressWarnings("synthetic-access")
538 public void actionPerformed(ActionEvent actionEvent) {
542 deleteProjectAction = new I18nAction("mainWindow.button.deleteProject") {
547 @SuppressWarnings("synthetic-access")
548 public void actionPerformed(ActionEvent actionEvent) {
555 * Initializes all child dialogs.
557 private void initDialogs() {
558 manageNodesDialog = new ManageNodesDialog(this);
559 aboutDialog = new AboutDialog(this);
560 configurationDialog = new ConfigurationDialog(this);
568 * Shows the configuration dialog.
570 private void configure() {
571 configurationDialog.setAdvancedMode(advancedMode);
572 configurationDialog.setAntialias(antialias);
573 configurationDialog.setControlFont(controlFont);
574 configurationDialog.setUserFont(userFont);
575 configurationDialog.setVisible(true);
576 if (!configurationDialog.wasCancelled()) {
577 advancedMode = configurationDialog.isAdvancedMode();
578 if (!advancedMode && (nodeList.size() > 1)) {
579 JOptionPane.showMessageDialog(mainWindow, I18n.get("mainWindow.warning.multipleNodesNotAdvancedMode.message"), I18n.get("mainWindow.warning.multipleNodesNotAdvancedMode.title"), JOptionPane.WARNING_MESSAGE);
581 mainWindow.setAdvancedMode(advancedMode);
582 antialias = configurationDialog.isAntialias();
583 controlFont = configurationDialog.getControlFont();
584 userFont = configurationDialog.getUserFont();
590 * Imports old jSite configuration.
592 private void importConfig() {
599 private void quit() {
605 * Rebuilds all node connect and disconnect actions.
610 private void rebuildNodeActions(List<Node> nodes) {
611 nodeConnectActions.clear();
612 nodeNodeConnectActions.clear();
613 nodeConnectActionNodes.clear();
614 nodeDisconnectActions.clear();
615 nodeNodeDisconnectActions.clear();
616 nodeDisconnectActionNodes.clear();
617 for (Node node: nodes) {
618 Action nodeConnectAction = new AbstractAction(node.getName()) {
623 @SuppressWarnings("synthetic-access")
624 public void actionPerformed(ActionEvent e) {
625 Node node = nodeConnectActionNodes.get(this);
629 nodeConnectActions.add(nodeConnectAction);
630 nodeConnectActionNodes.put(nodeConnectAction, node);
631 nodeNodeConnectActions.put(node, nodeConnectAction);
632 Action nodeDisconnectAction = new AbstractAction(node.getName()) {
637 @SuppressWarnings("synthetic-access")
638 public void actionPerformed(ActionEvent e) {
639 Node node = nodeDisconnectActionNodes.get(this);
640 nodeDisconnect(node);
643 // nodeDisconnectActions.add(nodeDisconnectAction);
644 nodeDisconnectActionNodes.put(nodeDisconnectAction, node);
645 nodeNodeDisconnectActions.put(node, nodeDisconnectAction);
650 * Pops up the “manage nodes” dialog.
652 private void manageNodes() {
654 manageNodesDialog.setNodeList(nodeList);
655 manageNodesDialog.setVisible(true);
656 nodeList = manageNodesDialog.getNodeList();
657 rebuildNodeActions(nodeList);
658 mainWindow.refreshNodeMenuItems();
660 if (nodeList.isEmpty()) {
661 Node newNode = new Node();
662 newNode.setName(I18n.get("general.defaultNode.name"));
663 newNode.setHostname("localhost");
664 newNode.setPort(9481);
665 nodeList.add(newNode);
667 Node firstNode = nodeList.get(0);
668 EditNodeDialog editNodeDialog = manageNodesDialog.getEditNodeDialog();
669 editNodeDialog.setNodeName(firstNode.getName());
670 editNodeDialog.setNodeHostname(firstNode.getHostname());
671 editNodeDialog.setNodePort(firstNode.getPort());
672 editNodeDialog.setVisible(true);
673 if (!editNodeDialog.wasCancelled()) {
674 firstNode.setName(editNodeDialog.getNodeName());
675 firstNode.setHostname(editNodeDialog.getNodeHostname());
676 firstNode.setPort(editNodeDialog.getNodePort());
677 /* TODO - give to core. */
683 * Connects to the node.
686 * The node to connect to
688 private void nodeConnect(final Node node) {
689 threadPool.execute(new Runnable() {
694 @SuppressWarnings("synthetic-access")
696 logger.log(Level.INFO, "connecting to node “" + node.getName() + "”…");
697 core.connectToNode(node);
703 * Disconnects from the node.
706 * The node to disconnect from
708 private void nodeDisconnect(Node node) {
709 logger.log(Level.INFO, "disconnecting from node “" + node.getName() + "”…");
710 core.disconnectFromNode(node);
714 * Changes the language of the interface. This method also disables the
715 * action for the newly set language and enables all others.
719 * @param languageAction
720 * The action that triggered the change
722 private void changeLanguage(Locale newLocale, I18nAction languageAction) {
723 for (I18nAction i18nAction: languageActions) {
724 i18nAction.setEnabled(i18nAction != languageAction);
726 I18n.setLocale(newLocale);
730 * Shows the “about” dialog.
732 private void helpAbout() {
733 aboutDialog.setVisible(true);
739 private void addProject() {
740 Project project = new Project();
741 project.setName("New Project");
742 project.setDescription("");
748 private void cloneProject() {
755 private void deleteProject() {
760 // INTERFACE CoreListener
766 public void loadingProjectsDone(String directory) {
767 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.projectLoadingDone"));
773 public void loadingProjectsFailed(String directory, Throwable throwable) {
774 JOptionPane.showMessageDialog(mainWindow, I18n.get("mainWindow.error.projectLoadingFailed.message", directory), I18n.get("mainWindow.error.projectLoadingFailed.title"), JOptionPane.ERROR_MESSAGE);
780 public void savingProjectsDone(String directory) {
781 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.projectSavingDone"));
787 public void savingProjectsFailed(String directory, Throwable throwabled) {
794 public void loadingNodesDone(String directory) {
795 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.loadingNodesDone"));
801 public void loadingNodesFailed(String directory, Throwable throwable) {
808 public void savingNodesDone(String directory) {
809 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.savingNodesDone"));
815 public void savingNodesFailed(String directory, Throwable throwable) {
822 public void coreLoaded() {
823 this.nodeList = core.getNodes();
824 manageNodesDialog.setNodeList(nodeList);
825 mainWindow.setVisible(true);
826 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.coreLoaded"));
832 public void coreStopped() {
833 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.coreStopped"));
839 public void nodeConnecting(Node node) {
840 Action nodeConnectAction = nodeNodeConnectActions.get(node);
841 nodeConnectActions.remove(nodeConnectAction);
842 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.connectingToNode", node.getName(), node.getHostname(), node.getPort()));
843 mainWindow.refreshNodeMenuItems();
849 public void nodeConnected(Node node) {
850 Action nodeDisconnectAction = nodeNodeDisconnectActions.get(node);
851 nodeDisconnectActions.add(nodeDisconnectAction);
852 mainWindow.refreshNodeMenuItems();
858 public void nodeDisconnected(Node node, Throwable throwable) {
859 Action nodeConnectAction = nodeNodeConnectActions.get(node);
860 nodeConnectActions.add(nodeConnectAction);
861 Action nodeDisconnectAction = nodeNodeDisconnectActions.get(node);
862 nodeDisconnectActions.remove(nodeDisconnectAction);
863 mainWindow.refreshNodeMenuItems();
867 // INTERFACE LoggingListener
873 public void logged(LogRecord logRecord) {
874 logWindow.logged(logRecord);