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;
43 import javax.swing.UIManager;
44 import javax.swing.UnsupportedLookAndFeelException;
46 import net.pterodactylus.jsite.core.Core;
47 import net.pterodactylus.jsite.core.CoreListener;
48 import net.pterodactylus.jsite.core.Node;
49 import net.pterodactylus.jsite.core.Project;
50 import net.pterodactylus.jsite.core.Request;
51 import net.pterodactylus.jsite.i18n.I18n;
52 import net.pterodactylus.jsite.i18n.gui.I18nAction;
53 import net.pterodactylus.util.image.IconLoader;
54 import net.pterodactylus.util.io.Closer;
55 import net.pterodactylus.util.logging.Logging;
56 import net.pterodactylus.util.logging.LoggingListener;
59 * The Swing user interface.
61 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
64 public class SwingInterface implements CoreListener, LoggingListener {
67 private static final Logger logger = Logging.getLogger(SwingInterface.class.getName());
69 /** The application core. */
70 private final Core core;
72 /** The configuration directory. */
73 private final String configDirectory;
75 /** The main window. */
76 private MainWindow mainWindow;
79 private Executor threadPool = Executors.newCachedThreadPool();
81 /** The logger window. */
82 private LogWindow logWindow;
84 /** The “configure” action. */
85 private I18nAction configureAction;
87 /** The “import config” action. */
88 private I18nAction importConfigAction;
90 /** The “quit” action. */
91 private I18nAction quitAction;
93 /** The “manage nodes” action. */
94 private I18nAction manageNodesAction;
96 /** The “connect to node” (simple mode) action. */
97 private I18nAction nodeConnectAction;
99 /** The “disconnect from node” (simple mode) action. */
100 private I18nAction nodeDisconnectAction;
102 /** All node menu items. */
103 private List<Action> nodeConnectActions = Collections.synchronizedList(new ArrayList<Action>());
105 /** Mapping from nodes to node connect actions. */
106 private Map<Node, Action> nodeNodeConnectActions = Collections.synchronizedMap(new HashMap<Node, Action>());
108 /** Mapping from node connect actions to nodes. */
109 private Map<Action, Node> nodeConnectActionNodes = Collections.synchronizedMap(new HashMap<Action, Node>());
111 /** All node disconnect actions. */
112 private List<Action> nodeDisconnectActions = Collections.synchronizedList(new ArrayList<Action>());
114 /** Mapping from nodes to node disconnect actions. */
115 private Map<Node, Action> nodeNodeDisconnectActions = Collections.synchronizedMap(new HashMap<Node, Action>());
117 /** Mapping from node disconnect actions to nodes. */
118 private Map<Action, Node> nodeDisconnectActionNodes = Collections.synchronizedMap(new HashMap<Action, Node>());
120 /** The node manager dialog. */
121 private ManageNodesDialog manageNodesDialog;
123 /** All lanugage menu items. */
124 private List<I18nAction> languageActions = new ArrayList<I18nAction>();
126 /** The “about” action. */
127 private I18nAction helpAboutAction;
129 /** The “add project” action. */
130 private I18nAction addProjectAction;
132 /** The “clone project” action. */
133 private I18nAction cloneProjectAction;
135 /** The “delete project” action. */
136 private I18nAction deleteProjectAction;
138 /** The “about” dialog. */
139 private AboutDialog aboutDialog;
141 /** The configuration dialog. */
142 private ConfigurationDialog configurationDialog;
144 /** The list of all defined nodes. */
145 private List<Node> nodeList = Collections.synchronizedList(new ArrayList<Node>());
151 /** The advanced mode. */
152 private boolean advancedMode;
154 /** Whether to antialias the GUI. */
155 private boolean antialias;
157 /** The control font. */
158 private String controlFont;
160 /** The user font. */
161 private String userFont;
163 /** The class name of the look and feel. */
164 private String lookAndFeel;
167 * Creates a new swing interface.
170 * The core to operate on
171 * @param configDirectory
172 * The directory the configuration is stored in
174 public SwingInterface(Core core, String configDirectory) {
176 this.configDirectory = configDirectory;
177 I18n.setLocale(Locale.ENGLISH);
179 if (lookAndFeel != null) {
181 UIManager.setLookAndFeel(lookAndFeel);
182 } catch (ClassNotFoundException cnfe1) {
183 logger.log(Level.WARNING, "could not load look and feel", cnfe1);
184 } catch (InstantiationException ie1) {
185 logger.log(Level.WARNING, "could not load look and feel", ie1);
186 } catch (IllegalAccessException iae1) {
187 logger.log(Level.WARNING, "could not load look and feel", iae1);
188 } catch (UnsupportedLookAndFeelException ulafe1) {
189 logger.log(Level.WARNING, "could not load look and feel", ulafe1);
193 System.setProperty("swing.aatext", "true");
195 if (controlFont != null) {
196 System.setProperty("swing.plaf.metal.controlFont", controlFont);
198 if (userFont != null) {
199 System.setProperty("swing.plaf.metal.userFont", userFont);
203 mainWindow = new MainWindow(this);
204 mainWindow.setAdvancedMode(advancedMode);
205 logWindow = new LogWindow();
213 * Returns the core that is controlled by the Swing interface.
222 * Returns the main window of the Swing interface.
224 * @return The main window
226 MainWindow getMainWindow() {
231 * Returns whether the advanced mode is activated.
233 * @return <code>true</code> if the advanced mode is activated,
234 * <code>false</code> if the simple mode is activated
236 boolean isAdvancedMode() {
241 * Returns the “configure” action.
243 * @return The “configure” action
245 I18nAction getConfigureAction() {
246 return configureAction;
250 * Returns the “import config” action.
252 * @return The “import config” action
254 I18nAction getImportConfigAction() {
255 return importConfigAction;
259 * Returns the “quit” action.
261 * @return The “quit” action
263 I18nAction getQuitAction() {
268 * Returns the “manage nodes” action.
270 * @return The “manage nodes” action
272 I18nAction getManageNodesAction() {
273 return manageNodesAction;
277 * Returns the “connect to node” action.
279 * @return The “connect to node” action
281 I18nAction getNodeConnectAction() {
282 return nodeConnectAction;
286 * Returns all “connect node” actions.
288 * @return All “connect node” actions
290 List<Action> getNodeConnectActions() {
291 return nodeConnectActions;
295 * Returns the “disconnect from node” action.
297 * @return The “disconnect from node” action
299 I18nAction getNodeDisconnectAction() {
300 return nodeDisconnectAction;
304 * Returns all “disconnect node” actions.
306 * @return All “disconnect node” action
308 List<Action> getNodeDisconnectActions() {
309 return nodeDisconnectActions;
313 * Returns all language actions.
315 * @return All language actions
317 List<I18nAction> getLanguageActions() {
318 return languageActions;
322 * Returns the “about” action.
324 * @return The “about” action
326 I18nAction getHelpAboutAction() {
327 return helpAboutAction;
331 * Returns the “add project” action.
333 * @return The “add project” action
335 I18nAction getAddProjectAction() {
336 return addProjectAction;
340 * Returns the “clone project” action.
342 * @return The “clone project” action
344 I18nAction getCloneProjectAction() {
345 return cloneProjectAction;
349 * Returns the “delete project” action.
351 * @return The “delete project” action
353 I18nAction getDeleteProjectAction() {
354 return deleteProjectAction;
370 * Loads the configuration of the interface.
372 private void loadConfig() {
373 /* initialize default stuff. */
375 /* now read config. */
376 File configFile = new File(configDirectory, "swing-interface.properties");
377 if (!configFile.exists() || !configFile.canRead() || !configFile.isFile()) {
378 System.err.println("could not find “" + configFile.getAbsolutePath() + "”!");
381 Properties configProperties = new Properties();
382 FileInputStream configInputStream = null;
384 configInputStream = new FileInputStream(configFile);
385 configProperties.load(configInputStream);
386 } catch (IOException ioe1) {
387 System.err.println("could not load config, " + ioe1.getMessage());
389 Closer.close(configInputStream);
391 if (configProperties.containsKey("advancedMode")) {
392 advancedMode = Boolean.valueOf(configProperties.getProperty("advancedMode"));
394 if (configProperties.containsKey("antialias")) {
395 antialias = Boolean.valueOf(configProperties.getProperty("antialias"));
397 if (configProperties.containsKey("controlFont")) {
398 controlFont = configProperties.getProperty("controlFont");
400 if (configProperties.containsKey("userFont")) {
401 userFont = configProperties.getProperty("userFont");
403 if (configProperties.containsKey("lookAndFeel")) {
404 lookAndFeel = configProperties.getProperty("lookAndFeel");
406 if (configProperties.containsKey("language")) {
407 I18n.setLocale(new Locale(configProperties.getProperty("language")));
412 * Saves the configuration.
414 private void saveConfig() {
415 File configDirectory = new File(this.configDirectory);
416 if (!configDirectory.exists()) {
417 if (!configDirectory.mkdirs()) {
418 System.err.println("could not create “" + this.configDirectory + "”!");
422 if (!configDirectory.exists() || !configDirectory.isDirectory() || !configDirectory.canWrite()) {
423 System.err.println("can not access “" + this.configDirectory + "”!");
426 File configFile = new File(configDirectory, "swing-interface.properties");
427 Properties configProperties = new Properties();
428 configProperties.setProperty("advancedMode", String.valueOf(advancedMode));
429 configProperties.setProperty("antialias", String.valueOf(antialias));
430 if (controlFont != null) {
431 configProperties.setProperty("controlFont", controlFont);
433 if (userFont != null) {
434 configProperties.setProperty("userFont", userFont);
436 if (lookAndFeel != null) {
437 configProperties.setProperty("lookAndFeel", lookAndFeel);
439 configProperties.setProperty("language", I18n.getLocale().getLanguage());
440 FileOutputStream configOutputStream = null;
442 configOutputStream = new FileOutputStream(configFile);
443 configProperties.store(configOutputStream, "configuration of swing interface");
444 } catch (IOException ioe1) {
445 System.err.println("could not save config, " + ioe1.getMessage());
447 Closer.close(configOutputStream);
452 * Initializes all actions.
454 private void initActions() {
455 configureAction = new I18nAction("mainWindow.menu.jSite.configure", IconLoader.loadIcon("/preferences-system.png")) {
460 @SuppressWarnings("synthetic-access")
461 public void actionPerformed(ActionEvent actionEvent) {
465 importConfigAction = new I18nAction("mainWindow.menu.jSite.importConfig") {
470 @SuppressWarnings("synthetic-access")
471 public void actionPerformed(ActionEvent actionEvent) {
475 quitAction = new I18nAction("mainWindow.menu.jSite.quit", IconLoader.loadIcon("/system-log-out.png")) {
480 @SuppressWarnings("synthetic-access")
481 public void actionPerformed(ActionEvent actionEvent) {
485 manageNodesAction = new I18nAction("mainWindow.menu.node.item.manageNodes") {
490 @SuppressWarnings("synthetic-access")
491 public void actionPerformed(ActionEvent actionEvent) {
495 nodeConnectAction = new I18nAction("mainWindow.menu.node.item.connect", false) {
497 @SuppressWarnings("synthetic-access")
498 public void actionPerformed(ActionEvent actionEvent) {
499 List<Node> nodes = core.getNodes();
500 if (nodes.isEmpty()) {
503 nodeConnect(nodes.get(0));
507 nodeDisconnectAction = new I18nAction("mainWindow.menu.node.item.disconnect", false) {
512 @SuppressWarnings("synthetic-access")
513 public void actionPerformed(ActionEvent e) {
514 List<Node> nodes = core.getNodes();
515 if (nodes.isEmpty()) {
518 nodeDisconnect(nodes.get(0));
521 rebuildNodeActions(core.getNodes());
522 List<Locale> availableLanguages = I18n.findAvailableLanguages();
523 for (final Locale locale: availableLanguages) {
524 I18nAction languageAction = new I18nAction("general.language." + locale.getLanguage()) {
526 @SuppressWarnings("synthetic-access")
527 public void actionPerformed(ActionEvent e) {
528 changeLanguage(locale, this);
532 if (I18n.getLocale().getLanguage().equals(locale.getLanguage())) {
533 languageAction.setEnabled(false);
535 languageActions.add(languageAction);
537 helpAboutAction = new I18nAction("mainWindow.menu.help.item.about") {
542 @SuppressWarnings("synthetic-access")
543 public void actionPerformed(ActionEvent actionEvent) {
547 addProjectAction = new I18nAction("mainWindow.button.addProject") {
552 @SuppressWarnings("synthetic-access")
553 public void actionPerformed(ActionEvent actionEvent) {
557 cloneProjectAction = new I18nAction("mainWindow.button.cloneProject") {
562 @SuppressWarnings("synthetic-access")
563 public void actionPerformed(ActionEvent actionEvent) {
567 deleteProjectAction = new I18nAction("mainWindow.button.deleteProject") {
572 @SuppressWarnings("synthetic-access")
573 public void actionPerformed(ActionEvent actionEvent) {
580 * Initializes all child dialogs.
582 private void initDialogs() {
583 manageNodesDialog = new ManageNodesDialog(this);
584 aboutDialog = new AboutDialog(this);
585 configurationDialog = new ConfigurationDialog(this);
593 * Shows the configuration dialog.
595 private void configure() {
596 configurationDialog.setAdvancedMode(advancedMode);
597 configurationDialog.setAntialias(antialias);
598 configurationDialog.setControlFont(controlFont);
599 configurationDialog.setUserFont(userFont);
600 configurationDialog.setLookAndFeel(lookAndFeel);
601 configurationDialog.setVisible(true);
602 if (!configurationDialog.wasCancelled()) {
603 advancedMode = configurationDialog.isAdvancedMode();
604 if (!advancedMode && (nodeList.size() > 1)) {
605 JOptionPane.showMessageDialog(mainWindow, I18n.get("mainWindow.warning.multipleNodesNotAdvancedMode.message"), I18n.get("mainWindow.warning.multipleNodesNotAdvancedMode.title"), JOptionPane.WARNING_MESSAGE);
607 mainWindow.setAdvancedMode(advancedMode);
608 antialias = configurationDialog.isAntialias();
609 controlFont = configurationDialog.getControlFont();
610 userFont = configurationDialog.getUserFont();
611 lookAndFeel = configurationDialog.getLookAndFeel();
617 * Imports old jSite configuration.
619 private void importConfig() {
626 private void quit() {
634 * Rebuilds all node connect and disconnect actions.
639 private void rebuildNodeActions(List<Node> nodes) {
640 logger.fine("rebuilding node actions…");
641 nodeConnectActions.clear();
642 nodeNodeConnectActions.clear();
643 nodeConnectActionNodes.clear();
644 nodeDisconnectActions.clear();
645 nodeNodeDisconnectActions.clear();
646 nodeDisconnectActionNodes.clear();
647 for (Node node: nodes) {
648 logger.finer("adding node “" + node + "” to menu");
649 Action nodeConnectAction = new AbstractAction(node.getName()) {
654 @SuppressWarnings("synthetic-access")
655 public void actionPerformed(ActionEvent e) {
656 Node node = nodeConnectActionNodes.get(this);
660 nodeConnectActions.add(nodeConnectAction);
661 nodeConnectActionNodes.put(nodeConnectAction, node);
662 nodeNodeConnectActions.put(node, nodeConnectAction);
663 Action nodeDisconnectAction = new AbstractAction(node.getName()) {
668 @SuppressWarnings("synthetic-access")
669 public void actionPerformed(ActionEvent e) {
670 Node node = nodeDisconnectActionNodes.get(this);
671 nodeDisconnect(node);
674 // nodeDisconnectActions.add(nodeDisconnectAction);
675 nodeDisconnectActionNodes.put(nodeDisconnectAction, node);
676 nodeNodeDisconnectActions.put(node, nodeDisconnectAction);
681 * Pops up the “manage nodes” dialog.
683 private void manageNodes() {
685 manageNodesDialog.setNodeList(nodeList);
686 manageNodesDialog.setVisible(true);
687 nodeList = manageNodesDialog.getNodeList();
688 rebuildNodeActions(nodeList);
689 mainWindow.refreshNodeMenuItems();
691 if (nodeList.isEmpty()) {
692 Node newNode = new Node();
693 newNode.setName(I18n.get("general.defaultNode.name"));
694 newNode.setHostname("localhost");
695 newNode.setPort(9481);
696 nodeList.add(newNode);
698 Node firstNode = nodeList.get(0);
699 EditNodeDialog editNodeDialog = manageNodesDialog.getEditNodeDialog();
700 editNodeDialog.setNodeName(firstNode.getName());
701 editNodeDialog.setNodeHostname(firstNode.getHostname());
702 editNodeDialog.setNodePort(firstNode.getPort());
703 editNodeDialog.setVisible(true);
704 if (!editNodeDialog.wasCancelled()) {
705 firstNode.setName(editNodeDialog.getNodeName());
706 firstNode.setHostname(editNodeDialog.getNodeHostname());
707 firstNode.setPort(editNodeDialog.getNodePort());
708 /* TODO - give to core. */
714 * Connects to the node.
717 * The node to connect to
719 private void nodeConnect(final Node node) {
720 threadPool.execute(new Runnable() {
725 @SuppressWarnings("synthetic-access")
727 logger.log(Level.INFO, "connecting to node “" + node.getName() + "”…");
728 core.connectToNode(node);
734 * Disconnects from the node.
737 * The node to disconnect from
739 private void nodeDisconnect(Node node) {
740 logger.log(Level.INFO, "disconnecting from node “" + node.getName() + "”…");
741 core.disconnectFromNode(node);
745 * Changes the language of the interface. This method also disables the
746 * action for the newly set language and enables all others.
750 * @param languageAction
751 * The action that triggered the change
753 private void changeLanguage(Locale newLocale, I18nAction languageAction) {
754 for (I18nAction i18nAction: languageActions) {
755 i18nAction.setEnabled(i18nAction != languageAction);
757 I18n.setLocale(newLocale);
761 * Shows the “about” dialog.
763 private void helpAbout() {
764 aboutDialog.setVisible(true);
770 private void addProject() {
771 Project project = new Project();
772 project.setName("New Project");
773 project.setDescription("");
779 private void cloneProject() {
786 private void deleteProject() {
791 // INTERFACE CoreListener
797 public void loadingProjectsDone(String directory) {
798 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.projectLoadingDone"));
804 public void loadingProjectsFailed(String directory, Throwable throwable) {
805 JOptionPane.showMessageDialog(mainWindow, I18n.get("mainWindow.error.projectLoadingFailed.message", directory), I18n.get("mainWindow.error.projectLoadingFailed.title"), JOptionPane.ERROR_MESSAGE);
811 public void savingProjectsDone(String directory) {
812 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.projectSavingDone"));
818 public void savingProjectsFailed(String directory, Throwable throwabled) {
825 public void loadingNodesDone(String directory) {
826 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.loadingNodesDone"));
832 public void loadingNodesFailed(String directory, Throwable throwable) {
839 public void savingNodesDone(String directory) {
840 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.savingNodesDone"));
846 public void savingNodesFailed(String directory, Throwable throwable) {
853 public void coreLoaded() {
854 mainWindow.setVisible(true);
855 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.coreLoaded"));
861 public void coreStopped() {
862 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.coreStopped"));
868 public void nodeAdded(Node node) {
869 logger.log(Level.INFO, "node added: " + node);
871 manageNodesDialog.setNodeList(nodeList);
872 rebuildNodeActions(nodeList);
873 mainWindow.refreshNodeMenuItems();
879 public void nodeRemoved(Node node) {
880 logger.log(Level.INFO, "node removed: " + node);
881 nodeList.remove(node);
882 rebuildNodeActions(nodeList);
883 mainWindow.refreshNodeMenuItems();
889 public void nodeConnecting(Node node) {
890 Action nodeConnectAction = nodeNodeConnectActions.get(node);
891 nodeConnectActions.remove(nodeConnectAction);
892 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.connectingToNode", node.getName(), node.getHostname(), node.getPort()));
893 mainWindow.refreshNodeMenuItems();
899 public void nodeConnected(Node node) {
900 Action nodeDisconnectAction = nodeNodeDisconnectActions.get(node);
901 nodeDisconnectActions.add(nodeDisconnectAction);
902 mainWindow.refreshNodeMenuItems();
908 public void nodeDisconnected(Node node, Throwable throwable) {
909 Action nodeConnectAction = nodeNodeConnectActions.get(node);
910 nodeConnectActions.add(nodeConnectAction);
911 Action nodeDisconnectAction = nodeNodeDisconnectActions.get(node);
912 nodeDisconnectActions.remove(nodeDisconnectAction);
913 mainWindow.refreshNodeMenuItems();
919 public void requestAdded(Node node, Request request) {
920 logger.log(Level.INFO, "request added to node: " + request + ", " + node);
921 /* TODO - implement */
927 public void requestProgressed(Request request, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean finalizedTotal) {
928 /* TODO - update table model */
932 // INTERFACE LoggingListener
938 public void logged(LogRecord logRecord) {
939 logWindow.logged(logRecord);