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;
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() {
632 * Rebuilds all node connect and disconnect actions.
637 private void rebuildNodeActions(List<Node> nodes) {
638 nodeConnectActions.clear();
639 nodeNodeConnectActions.clear();
640 nodeConnectActionNodes.clear();
641 nodeDisconnectActions.clear();
642 nodeNodeDisconnectActions.clear();
643 nodeDisconnectActionNodes.clear();
644 for (Node node: nodes) {
645 Action nodeConnectAction = new AbstractAction(node.getName()) {
650 @SuppressWarnings("synthetic-access")
651 public void actionPerformed(ActionEvent e) {
652 Node node = nodeConnectActionNodes.get(this);
656 nodeConnectActions.add(nodeConnectAction);
657 nodeConnectActionNodes.put(nodeConnectAction, node);
658 nodeNodeConnectActions.put(node, nodeConnectAction);
659 Action nodeDisconnectAction = new AbstractAction(node.getName()) {
664 @SuppressWarnings("synthetic-access")
665 public void actionPerformed(ActionEvent e) {
666 Node node = nodeDisconnectActionNodes.get(this);
667 nodeDisconnect(node);
670 // nodeDisconnectActions.add(nodeDisconnectAction);
671 nodeDisconnectActionNodes.put(nodeDisconnectAction, node);
672 nodeNodeDisconnectActions.put(node, nodeDisconnectAction);
677 * Pops up the “manage nodes” dialog.
679 private void manageNodes() {
681 manageNodesDialog.setNodeList(nodeList);
682 manageNodesDialog.setVisible(true);
683 nodeList = manageNodesDialog.getNodeList();
684 rebuildNodeActions(nodeList);
685 mainWindow.refreshNodeMenuItems();
687 if (nodeList.isEmpty()) {
688 Node newNode = new Node();
689 newNode.setName(I18n.get("general.defaultNode.name"));
690 newNode.setHostname("localhost");
691 newNode.setPort(9481);
692 nodeList.add(newNode);
694 Node firstNode = nodeList.get(0);
695 EditNodeDialog editNodeDialog = manageNodesDialog.getEditNodeDialog();
696 editNodeDialog.setNodeName(firstNode.getName());
697 editNodeDialog.setNodeHostname(firstNode.getHostname());
698 editNodeDialog.setNodePort(firstNode.getPort());
699 editNodeDialog.setVisible(true);
700 if (!editNodeDialog.wasCancelled()) {
701 firstNode.setName(editNodeDialog.getNodeName());
702 firstNode.setHostname(editNodeDialog.getNodeHostname());
703 firstNode.setPort(editNodeDialog.getNodePort());
704 /* TODO - give to core. */
710 * Connects to the node.
713 * The node to connect to
715 private void nodeConnect(final Node node) {
716 threadPool.execute(new Runnable() {
721 @SuppressWarnings("synthetic-access")
723 logger.log(Level.INFO, "connecting to node “" + node.getName() + "”…");
724 core.connectToNode(node);
730 * Disconnects from the node.
733 * The node to disconnect from
735 private void nodeDisconnect(Node node) {
736 logger.log(Level.INFO, "disconnecting from node “" + node.getName() + "”…");
737 core.disconnectFromNode(node);
741 * Changes the language of the interface. This method also disables the
742 * action for the newly set language and enables all others.
746 * @param languageAction
747 * The action that triggered the change
749 private void changeLanguage(Locale newLocale, I18nAction languageAction) {
750 for (I18nAction i18nAction: languageActions) {
751 i18nAction.setEnabled(i18nAction != languageAction);
753 I18n.setLocale(newLocale);
757 * Shows the “about” dialog.
759 private void helpAbout() {
760 aboutDialog.setVisible(true);
766 private void addProject() {
767 Project project = new Project();
768 project.setName("New Project");
769 project.setDescription("");
775 private void cloneProject() {
782 private void deleteProject() {
787 // INTERFACE CoreListener
793 public void loadingProjectsDone(String directory) {
794 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.projectLoadingDone"));
800 public void loadingProjectsFailed(String directory, Throwable throwable) {
801 JOptionPane.showMessageDialog(mainWindow, I18n.get("mainWindow.error.projectLoadingFailed.message", directory), I18n.get("mainWindow.error.projectLoadingFailed.title"), JOptionPane.ERROR_MESSAGE);
807 public void savingProjectsDone(String directory) {
808 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.projectSavingDone"));
814 public void savingProjectsFailed(String directory, Throwable throwabled) {
821 public void loadingNodesDone(String directory) {
822 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.loadingNodesDone"));
828 public void loadingNodesFailed(String directory, Throwable throwable) {
835 public void savingNodesDone(String directory) {
836 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.savingNodesDone"));
842 public void savingNodesFailed(String directory, Throwable throwable) {
849 public void coreLoaded() {
850 this.nodeList = core.getNodes();
851 manageNodesDialog.setNodeList(nodeList);
852 mainWindow.setVisible(true);
853 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.coreLoaded"));
859 public void coreStopped() {
860 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.coreStopped"));
866 public void nodeConnecting(Node node) {
867 Action nodeConnectAction = nodeNodeConnectActions.get(node);
868 nodeConnectActions.remove(nodeConnectAction);
869 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.connectingToNode", node.getName(), node.getHostname(), node.getPort()));
870 mainWindow.refreshNodeMenuItems();
876 public void nodeConnected(Node node) {
877 Action nodeDisconnectAction = nodeNodeDisconnectActions.get(node);
878 nodeDisconnectActions.add(nodeDisconnectAction);
879 mainWindow.refreshNodeMenuItems();
885 public void nodeDisconnected(Node node, Throwable throwable) {
886 Action nodeConnectAction = nodeNodeConnectActions.get(node);
887 nodeConnectActions.add(nodeConnectAction);
888 Action nodeDisconnectAction = nodeNodeDisconnectActions.get(node);
889 nodeDisconnectActions.remove(nodeDisconnectAction);
890 mainWindow.refreshNodeMenuItems();
896 public void requestAdded(Node node, Request request) {
897 logger.log(Level.INFO, "request added to node: " + request + ", " + node);
898 /* TODO - implement */
902 // INTERFACE LoggingListener
908 public void logged(LogRecord logRecord) {
909 logWindow.logged(logRecord);