make delete and clone buttons project specific
[jSite2.git] / src / net / pterodactylus / jsite / gui / SwingInterface.java
1 /*
2  * jSite2 - SwingInterface.java -
3  * Copyright © 2008 David Roden
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 package net.pterodactylus.jsite.gui;
21
22 import java.awt.event.ActionEvent;
23 import java.beans.PropertyChangeEvent;
24 import java.beans.PropertyChangeListener;
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.net.UnknownHostException;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.Date;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Locale;
36 import java.util.Map;
37 import java.util.Properties;
38 import java.util.concurrent.Executor;
39 import java.util.concurrent.Executors;
40 import java.util.logging.Level;
41 import java.util.logging.LogRecord;
42 import java.util.logging.Logger;
43
44 import javax.swing.JOptionPane;
45 import javax.swing.UIManager;
46 import javax.swing.UnsupportedLookAndFeelException;
47
48 import net.pterodactylus.jsite.core.Core;
49 import net.pterodactylus.jsite.core.CoreListener;
50 import net.pterodactylus.jsite.core.JSiteException;
51 import net.pterodactylus.jsite.core.Node;
52 import net.pterodactylus.jsite.i18n.I18n;
53 import net.pterodactylus.jsite.i18n.gui.I18nAction;
54 import net.pterodactylus.jsite.project.Project;
55 import net.pterodactylus.util.image.IconLoader;
56 import net.pterodactylus.util.io.Closer;
57 import net.pterodactylus.util.logging.Logging;
58 import net.pterodactylus.util.logging.LoggingListener;
59
60 /**
61  * The Swing user interface.
62  * 
63  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
64  */
65 public class SwingInterface implements CoreListener, LoggingListener, PropertyChangeListener {
66
67         /** The logger. */
68         private static final Logger logger = Logging.getLogger(SwingInterface.class.getName());
69
70         /** The application core. */
71         private final Core core;
72
73         /** The configuration directory. */
74         private final String configDirectory;
75
76         /** The main window. */
77         private MainWindow mainWindow;
78
79         /** Thread pool. */
80         private Executor threadPool = Executors.newCachedThreadPool();
81
82         /** The logger window. */
83         private LogWindow logWindow;
84
85         /** The “configure” action. */
86         private I18nAction configureAction;
87
88         /** The “import config” action. */
89         private I18nAction importConfigAction;
90
91         /** The “quit” action. */
92         private I18nAction quitAction;
93
94         /** The “add node” action. */
95         private I18nAction addNodeAction;
96
97         /** All node menu items. */
98         private Map<Node, I18nAction> nodeConnectActions = Collections.synchronizedMap(new HashMap<Node, I18nAction>());
99
100         /** All node disconnect actions. */
101         private Map<Node, I18nAction> nodeDisconnectActions = Collections.synchronizedMap(new HashMap<Node, I18nAction>());
102
103         /** All node edit actions. */
104         private Map<Node, I18nAction> nodeEditActions = Collections.synchronizedMap(new HashMap<Node, I18nAction>());
105
106         /** All node removal actions. */
107         private Map<Node, I18nAction> nodeDeleteActions = Collections.synchronizedMap(new HashMap<Node, I18nAction>());
108
109         /** All lanugage menu items. */
110         private List<I18nAction> languageActions = new ArrayList<I18nAction>();
111
112         /** The “about” action. */
113         private I18nAction helpAboutAction;
114
115         /** The “add project” action. */
116         private I18nAction addProjectAction;
117
118         /** The “clone project” actions. */
119         private Map<Project, I18nAction> cloneProjectActions = new HashMap<Project, I18nAction>();
120
121         /** The “delete project” actions. */
122         private Map<Project, I18nAction> deleteProjectActions = new HashMap<Project, I18nAction>();
123
124         /** The “about” dialog. */
125         private AboutDialog aboutDialog;
126
127         /** The configuration dialog. */
128         private ConfigurationDialog configurationDialog;
129
130         /** The node editor dialog. */
131         private EditNodeDialog editNodeDialog;
132
133         /** The list of all defined nodes. */
134         private List<Node> nodeList = Collections.synchronizedList(new ArrayList<Node>());
135
136         /** The list of all projects. */
137         private List<Project> projectList = Collections.synchronizedList(new ArrayList<Project>());
138
139         //
140         // CONFIGURATION
141         //
142
143         /** The advanced mode. */
144         private boolean advancedMode;
145
146         /** Whether to antialias the GUI. */
147         private boolean antialias;
148
149         /** The control font. */
150         private String controlFont;
151
152         /** The user font. */
153         private String userFont;
154
155         /** The class name of the look and feel. */
156         private String lookAndFeel;
157
158         /** X coordinate of the main window. */
159         private int mainWindowX = -1;
160
161         /** Y coordinate of the main window. */
162         private int mainWindowY = -1;
163
164         /** Width of the main window. */
165         private int mainWindowWidth = -1;
166
167         /** Height of the main window. */
168         private int mainWindowHeight = -1;
169
170         /**
171          * Creates a new swing interface.
172          * 
173          * @param core
174          *            The core to operate on
175          * @param configDirectory
176          *            The directory the configuration is stored in
177          */
178         public SwingInterface(Core core, String configDirectory) {
179                 this.core = core;
180                 this.configDirectory = configDirectory;
181                 I18n.setLocale(Locale.ENGLISH);
182                 loadConfig();
183                 if (lookAndFeel != null) {
184                         try {
185                                 UIManager.setLookAndFeel(lookAndFeel);
186                         } catch (ClassNotFoundException cnfe1) {
187                                 logger.log(Level.WARNING, "could not load look and feel", cnfe1);
188                         } catch (InstantiationException ie1) {
189                                 logger.log(Level.WARNING, "could not load look and feel", ie1);
190                         } catch (IllegalAccessException iae1) {
191                                 logger.log(Level.WARNING, "could not load look and feel", iae1);
192                         } catch (UnsupportedLookAndFeelException ulafe1) {
193                                 logger.log(Level.WARNING, "could not load look and feel", ulafe1);
194                         }
195                 }
196                 if (antialias) {
197                         System.setProperty("swing.aatext", "true");
198                 }
199                 if (controlFont != null) {
200                         System.setProperty("swing.plaf.metal.controlFont", controlFont);
201                 }
202                 if (userFont != null) {
203                         System.setProperty("swing.plaf.metal.userFont", userFont);
204                 }
205                 initActions();
206                 initDialogs();
207                 mainWindow = new MainWindow(this);
208                 mainWindow.setAdvancedMode(advancedMode);
209                 if ((mainWindowX != -1) && (mainWindowY != -1) && (mainWindowWidth != -1) && (mainWindowHeight != -1)) {
210                         mainWindow.setLocation(mainWindowX, mainWindowY);
211                         mainWindow.setSize(mainWindowWidth, mainWindowHeight);
212                 }
213                 logWindow = new LogWindow();
214         }
215
216         //
217         // ACCESSORS
218         //
219
220         /**
221          * Returns the core that is controlled by the Swing interface.
222          * 
223          * @return The core
224          */
225         Core getCore() {
226                 return core;
227         }
228
229         /**
230          * Returns the main window of the Swing interface.
231          * 
232          * @return The main window
233          */
234         MainWindow getMainWindow() {
235                 return mainWindow;
236         }
237
238         /**
239          * Returns whether the advanced mode is activated.
240          * 
241          * @return <code>true</code> if the advanced mode is activated,
242          *         <code>false</code> if the simple mode is activated
243          */
244         boolean isAdvancedMode() {
245                 return advancedMode;
246         }
247
248         /**
249          * Returns the “configure” action.
250          * 
251          * @return The “configure” action
252          */
253         I18nAction getConfigureAction() {
254                 return configureAction;
255         }
256
257         /**
258          * Returns the “import config” action.
259          * 
260          * @return The “import config” action
261          */
262         I18nAction getImportConfigAction() {
263                 return importConfigAction;
264         }
265
266         /**
267          * Returns the “quit” action.
268          * 
269          * @return The “quit” action
270          */
271         I18nAction getQuitAction() {
272                 return quitAction;
273         }
274
275         /**
276          * Returns the “add node” action.
277          * 
278          * @return The “add node” action
279          */
280         I18nAction getAddNodeAction() {
281                 return addNodeAction;
282         }
283
284         /**
285          * Returns the “connect to node” action for the given node.
286          * 
287          * @param node
288          *            The node go get the “connect” action for
289          * @return The “connect to node” action
290          */
291         I18nAction getNodeConnectAction(Node node) {
292                 return nodeConnectActions.get(node);
293         }
294
295         /**
296          * Returns the “disconnect from node” action for the given node.
297          * 
298          * @param node
299          *            The node go get the “disconnect” action for
300          * @return The “disconnect from node” action
301          */
302         I18nAction getNodeDisconnectAction(Node node) {
303                 return nodeDisconnectActions.get(node);
304         }
305
306         /**
307          * Returns the “edit node” action for the given node.
308          * 
309          * @param node
310          *            The node to edit
311          * @return The “edit node” action
312          */
313         I18nAction getNodeEditAction(Node node) {
314                 return nodeEditActions.get(node);
315         }
316
317         /**
318          * Returns the “delete node” action for the given node.
319          * 
320          * @param node
321          *            The node to delete
322          * @return The “delete node” action
323          */
324         I18nAction getNodeDeleteAction(Node node) {
325                 return nodeDeleteActions.get(node);
326         }
327
328         /**
329          * Returns all language actions.
330          * 
331          * @return All language actions
332          */
333         List<I18nAction> getLanguageActions() {
334                 return languageActions;
335         }
336
337         /**
338          * Returns the “about” action.
339          * 
340          * @return The “about” action
341          */
342         I18nAction getHelpAboutAction() {
343                 return helpAboutAction;
344         }
345
346         /**
347          * Returns the “add project” action.
348          * 
349          * @return The “add project” action
350          */
351         I18nAction getAddProjectAction() {
352                 return addProjectAction;
353         }
354
355         /**
356          * Returns the “clone project” action for the given project.
357          * 
358          * @param project
359          *            The project to get the “clone project” action for
360          * @return The “clone project” action
361          */
362         I18nAction getCloneProjectAction(Project project) {
363                 return cloneProjectActions.get(project);
364         }
365
366         /**
367          * Returns the “delete project” action for the given project.
368          * 
369          * @param project
370          *            The project to get the “delete project” action for
371          * @return The “delete project” action
372          */
373         I18nAction getDeleteProjectAction(Project project) {
374                 return deleteProjectActions.get(project);
375         }
376
377         /**
378          * Returns all currently configured nodes.
379          * 
380          * @return All configured nodes
381          */
382         List<Node> getNodes() {
383                 return nodeList;
384         }
385
386         /**
387          * Returns a list of all projects.
388          * 
389          * @return All projects
390          */
391         List<Project> getProjects() {
392                 return projectList;
393         }
394
395         /**
396          * Returns the thread pool used for off-thread processes.
397          * 
398          * @return The thread pool
399          */
400         Executor getThreadPool() {
401                 return threadPool;
402         }
403
404         //
405         // ACTIONS
406         //
407
408         //
409         // SERVICE METHODS
410         //
411
412         //
413         // PRIVATE METHODS
414         //
415
416         /**
417          * Loads the configuration of the interface.
418          */
419         private void loadConfig() {
420                 /* initialize default stuff. */
421                 antialias = false;
422                 /* now read config. */
423                 File configFile = new File(configDirectory, "swing-interface.properties");
424                 if (!configFile.exists() || !configFile.canRead() || !configFile.isFile()) {
425                         System.err.println("could not find “" + configFile.getAbsolutePath() + "”!");
426                         return;
427                 }
428                 Properties configProperties = new Properties();
429                 FileInputStream configInputStream = null;
430                 try {
431                         configInputStream = new FileInputStream(configFile);
432                         configProperties.load(configInputStream);
433                 } catch (IOException ioe1) {
434                         System.err.println("could not load config, " + ioe1.getMessage());
435                 } finally {
436                         Closer.close(configInputStream);
437                 }
438                 if (configProperties.containsKey("advancedMode")) {
439                         advancedMode = Boolean.valueOf(configProperties.getProperty("advancedMode"));
440                 }
441                 if (configProperties.containsKey("antialias")) {
442                         antialias = Boolean.valueOf(configProperties.getProperty("antialias"));
443                 }
444                 if (configProperties.containsKey("controlFont")) {
445                         controlFont = configProperties.getProperty("controlFont");
446                 }
447                 if (configProperties.containsKey("userFont")) {
448                         userFont = configProperties.getProperty("userFont");
449                 }
450                 if (configProperties.containsKey("lookAndFeel")) {
451                         lookAndFeel = configProperties.getProperty("lookAndFeel");
452                 }
453                 if (configProperties.containsKey("language")) {
454                         I18n.setLocale(new Locale(configProperties.getProperty("language")));
455                 }
456                 if (configProperties.containsKey("mainWindowX")) {
457                         mainWindowX = Integer.valueOf(configProperties.getProperty("mainWindowX"));
458                 }
459                 if (configProperties.containsKey("mainWindowY")) {
460                         mainWindowY = Integer.valueOf(configProperties.getProperty("mainWindowY"));
461                 }
462                 if (configProperties.containsKey("mainWindowWidth")) {
463                         mainWindowWidth = Integer.valueOf(configProperties.getProperty("mainWindowWidth"));
464                 }
465                 if (configProperties.containsKey("mainWindowHeight")) {
466                         mainWindowHeight = Integer.valueOf(configProperties.getProperty("mainWindowHeight"));
467                 }
468         }
469
470         /**
471          * Saves the configuration.
472          */
473         private void saveConfig() {
474                 File configDirectory = new File(this.configDirectory);
475                 if (!configDirectory.exists()) {
476                         if (!configDirectory.mkdirs()) {
477                                 System.err.println("could not create “" + this.configDirectory + "”!");
478                                 return;
479                         }
480                 }
481                 if (!configDirectory.exists() || !configDirectory.isDirectory() || !configDirectory.canWrite()) {
482                         System.err.println("can not access “" + this.configDirectory + "”!");
483                         return;
484                 }
485                 File configFile = new File(configDirectory, "swing-interface.properties");
486                 Properties configProperties = new Properties();
487                 configProperties.setProperty("advancedMode", String.valueOf(advancedMode));
488                 configProperties.setProperty("antialias", String.valueOf(antialias));
489                 if (controlFont != null) {
490                         configProperties.setProperty("controlFont", controlFont);
491                 }
492                 if (userFont != null) {
493                         configProperties.setProperty("userFont", userFont);
494                 }
495                 if (lookAndFeel != null) {
496                         configProperties.setProperty("lookAndFeel", lookAndFeel);
497                 }
498                 configProperties.setProperty("language", I18n.getLocale().getLanguage());
499                 configProperties.setProperty("mainWindowX", String.valueOf(mainWindowX));
500                 configProperties.setProperty("mainWindowY", String.valueOf(mainWindowY));
501                 configProperties.setProperty("mainWindowWidth", String.valueOf(mainWindowWidth));
502                 configProperties.setProperty("mainWindowHeight", String.valueOf(mainWindowHeight));
503                 FileOutputStream configOutputStream = null;
504                 try {
505                         configOutputStream = new FileOutputStream(configFile);
506                         configProperties.store(configOutputStream, "configuration of swing interface");
507                 } catch (IOException ioe1) {
508                         System.err.println("could not save config, " + ioe1.getMessage());
509                 } finally {
510                         Closer.close(configOutputStream);
511                 }
512         }
513
514         /**
515          * Initializes all actions.
516          */
517         private void initActions() {
518                 configureAction = new I18nAction("mainWindow.menu.jSite.configure", IconLoader.loadIcon("/preferences-system.png")) {
519
520                         /**
521                          * {@inheritDoc}
522                          */
523                         @SuppressWarnings("synthetic-access")
524                         public void actionPerformed(ActionEvent actionEvent) {
525                                 configure();
526                         }
527                 };
528                 importConfigAction = new I18nAction("mainWindow.menu.jSite.importConfig") {
529
530                         /**
531                          * {@inheritDoc}
532                          */
533                         @SuppressWarnings("synthetic-access")
534                         public void actionPerformed(ActionEvent actionEvent) {
535                                 importConfig();
536                         }
537                 };
538                 quitAction = new I18nAction("mainWindow.menu.jSite.quit", IconLoader.loadIcon("/system-log-out.png")) {
539
540                         /**
541                          * {@inheritDoc}
542                          */
543                         @SuppressWarnings("synthetic-access")
544                         public void actionPerformed(ActionEvent actionEvent) {
545                                 quit();
546                         }
547                 };
548                 List<Locale> availableLanguages = I18n.findAvailableLanguages();
549                 for (final Locale locale: availableLanguages) {
550                         I18nAction languageAction = new I18nAction("general.language." + locale.getLanguage()) {
551
552                                 @SuppressWarnings("synthetic-access")
553                                 public void actionPerformed(ActionEvent e) {
554                                         changeLanguage(locale, this);
555                                 }
556
557                         };
558                         if (I18n.getLocale().getLanguage().equals(locale.getLanguage())) {
559                                 languageAction.setEnabled(false);
560                         }
561                         languageActions.add(languageAction);
562                 }
563                 addNodeAction = new I18nAction("mainWindow.menu.node.item.addNode", IconLoader.loadIcon("/node-new.png")) {
564
565                         /**
566                          * {@inheritDoc}
567                          */
568                         @SuppressWarnings("synthetic-access")
569                         public void actionPerformed(ActionEvent actionEvent) {
570                                 addNode();
571                         }
572                 };
573                 helpAboutAction = new I18nAction("mainWindow.menu.help.item.about") {
574
575                         /**
576                          * {@inheritDoc}
577                          */
578                         @SuppressWarnings("synthetic-access")
579                         public void actionPerformed(ActionEvent actionEvent) {
580                                 helpAbout();
581                         }
582                 };
583                 addProjectAction = new I18nAction("mainWindow.button.addProject") {
584
585                         /**
586                          * {@inheritDoc}
587                          */
588                         @SuppressWarnings("synthetic-access")
589                         public void actionPerformed(ActionEvent actionEvent) {
590                                 addProject();
591                         }
592                 };
593         }
594
595         /**
596          * Initializes all child dialogs.
597          */
598         private void initDialogs() {
599                 aboutDialog = new AboutDialog(this);
600                 configurationDialog = new ConfigurationDialog(this);
601                 editNodeDialog = new EditNodeDialog(mainWindow);
602         }
603
604         //
605         // PRIVATE ACTIONS
606         //
607
608         /**
609          * Shows the configuration dialog.
610          */
611         private void configure() {
612                 configurationDialog.setAdvancedMode(advancedMode);
613                 configurationDialog.setAntialias(antialias);
614                 configurationDialog.setControlFont(controlFont);
615                 configurationDialog.setUserFont(userFont);
616                 configurationDialog.setLookAndFeel(lookAndFeel);
617                 configurationDialog.setVisible(true);
618                 if (!configurationDialog.wasCancelled()) {
619                         advancedMode = configurationDialog.isAdvancedMode();
620                         if (!advancedMode && (nodeList.size() > 1)) {
621                                 JOptionPane.showMessageDialog(mainWindow, I18n.get("mainWindow.warning.multipleNodesNotAdvancedMode.message"), I18n.get("mainWindow.warning.multipleNodesNotAdvancedMode.title"), JOptionPane.WARNING_MESSAGE);
622                         }
623                         mainWindow.setAdvancedMode(advancedMode);
624                         antialias = configurationDialog.isAntialias();
625                         controlFont = configurationDialog.getControlFont();
626                         userFont = configurationDialog.getUserFont();
627                         lookAndFeel = configurationDialog.getLookAndFeel();
628                         saveConfig();
629                 }
630         }
631
632         /**
633          * Imports old jSite configuration.
634          */
635         private void importConfig() {
636                 /* TODO */
637         }
638
639         /**
640          * Quits jSite.
641          */
642         private void quit() {
643                 /* TODO - ask */
644                 core.stop();
645                 mainWindowX = mainWindow.getX();
646                 mainWindowY = mainWindow.getY();
647                 mainWindowWidth = mainWindow.getWidth();
648                 mainWindowHeight = mainWindow.getHeight();
649                 saveConfig();
650                 System.exit(0);
651         }
652
653         /**
654          * Adds a node.
655          */
656         private void addNode() {
657                 editNodeDialog.setNodeName(I18n.get(nodeList.isEmpty() ? "general.defaultNode.name" : "general.newNode.name"));
658                 editNodeDialog.setNodeHostname("localhost");
659                 editNodeDialog.setNodePort(9481);
660                 editNodeDialog.setVisible(true);
661                 if (!editNodeDialog.wasCancelled()) {
662                         Node newNode = new Node();
663                         newNode.setName(editNodeDialog.getNodeName());
664                         newNode.setHostname(editNodeDialog.getNodeHostname());
665                         newNode.setPort(editNodeDialog.getNodePort());
666                         try {
667                                 core.addNode(newNode);
668                         } catch (UnknownHostException e) {
669                                 JOptionPane.showMessageDialog(mainWindow, I18n.get("mainWindow.error.hostnameUnresolvable.message"), I18n.get("mainWindow.error.hostnameUnresolvable.title"), JOptionPane.ERROR_MESSAGE);
670                         }
671                 }
672         }
673
674         /**
675          * Edits the given node.
676          * 
677          * @param node
678          *            The node to edit
679          */
680         private void editNode(Node node) {
681                 editNodeDialog.setNodeName(node.getName());
682                 editNodeDialog.setNodeHostname(node.getHostname());
683                 editNodeDialog.setNodePort(node.getPort());
684                 editNodeDialog.setVisible(true);
685                 if (!editNodeDialog.wasCancelled()) {
686                         node.setName(editNodeDialog.getNodeName());
687                         node.setHostname(editNodeDialog.getNodeHostname());
688                         node.setPort(editNodeDialog.getNodePort());
689                 }
690         }
691
692         /**
693          * Deletes the given node.
694          * 
695          * @param node
696          *            The node to delete
697          */
698         private void deleteNode(Node node) {
699                 int option = JOptionPane.showConfirmDialog(mainWindow, I18n.get("mainWindow.question.deleteNode.message", node.getName()), I18n.get("mainWindow.question.deleteNode.title"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
700                 if (option == JOptionPane.OK_OPTION) {
701                         core.removeNode(node);
702                 }
703         }
704
705         /**
706          * Connects to the node.
707          * 
708          * @param node
709          *            The node to connect to
710          */
711         private void nodeConnect(final Node node) {
712                 threadPool.execute(new Runnable() {
713
714                         /**
715                          * {@inheritDoc}
716                          */
717                         @SuppressWarnings("synthetic-access")
718                         public void run() {
719                                 logger.log(Level.INFO, "connecting to node “" + node.getName() + "”…");
720                                 core.connectToNode(node);
721                         }
722                 });
723         }
724
725         /**
726          * Disconnects from the node.
727          * 
728          * @param node
729          *            The node to disconnect from
730          */
731         private void nodeDisconnect(Node node) {
732                 logger.log(Level.INFO, "disconnecting from node “" + node.getName() + "”…");
733                 core.disconnectFromNode(node);
734         }
735
736         /**
737          * Changes the language of the interface. This method also disables the
738          * action for the newly set language and enables all others.
739          * 
740          * @param newLocale
741          *            The new language
742          * @param languageAction
743          *            The action that triggered the change
744          */
745         private void changeLanguage(Locale newLocale, I18nAction languageAction) {
746                 for (I18nAction i18nAction: languageActions) {
747                         i18nAction.setEnabled(i18nAction != languageAction);
748                 }
749                 I18n.setLocale(newLocale);
750         }
751
752         /**
753          * Shows the “about” dialog.
754          */
755         private void helpAbout() {
756                 aboutDialog.setVisible(true);
757         }
758
759         /**
760          * Adds a project.
761          */
762         private void addProject() {
763                 try {
764                         core.createProject();
765                 } catch (JSiteException nne1) {
766                         /* TODO - add i18n */
767                         JOptionPane.showMessageDialog(mainWindow, I18n.get(""), I18n.get(""), JOptionPane.ERROR_MESSAGE);
768                 } catch (IOException e) {
769                         /* TODO - add i18n */
770                         JOptionPane.showMessageDialog(mainWindow, I18n.get(""), I18n.get(""), JOptionPane.ERROR_MESSAGE);
771                 }
772         }
773
774         /**
775          * Clones a project.
776          * 
777          * @param project
778          *            The project to clone
779          */
780         private void cloneProject(Project project) {
781                 System.out.println("clone " + project);
782                 /* TODO */
783         }
784
785         /**
786          * Deletes a project.
787          * 
788          * @param project
789          *            The project to delete
790          */
791         private void deleteProject(Project project) {
792                 System.out.println("delete " + project);
793         }
794
795         //
796         // INTERFACE CoreListener
797         //
798
799         /**
800          * {@inheritDoc}
801          */
802         public void loadingProjectsDone(String directory) {
803                 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.projectLoadingDone"));
804                 for (Project project: core.getProjects()) {
805                         projectAdded(project, false);
806                 }
807         }
808
809         /**
810          * {@inheritDoc}
811          */
812         public void loadingProjectsFailed(String directory, Throwable throwable) {
813                 JOptionPane.showMessageDialog(mainWindow, I18n.get("mainWindow.error.projectLoadingFailed.message", directory), I18n.get("mainWindow.error.projectLoadingFailed.title"), JOptionPane.ERROR_MESSAGE);
814         }
815
816         /**
817          * {@inheritDoc}
818          */
819         public void savingProjectsDone(String directory) {
820                 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.projectSavingDone"));
821         }
822
823         /**
824          * {@inheritDoc}
825          */
826         public void savingProjectsFailed(String directory, Throwable throwabled) {
827                 /* TODO */
828         }
829
830         /**
831          * {@inheritDoc}
832          */
833         public void projectAdded(Project project) {
834                 projectAdded(project, true);
835         }
836         
837         /**
838          * 
839          * @param project
840          * @param switchToProject
841          */
842         private void projectAdded(final Project project, boolean switchToProject) {
843                 project.setName(I18n.get("general.newProject.name"));
844                 project.setDescription(I18n.get("general.newProject.description", new Date()));
845                 cloneProjectActions.put(project, new I18nAction("mainWindow.button.cloneProject") {
846
847                         /**
848                          * {@inheritDoc}
849                          */
850                         @SuppressWarnings("synthetic-access")
851                         public void actionPerformed(ActionEvent actionEvent) {
852                                 cloneProject(project);
853                         }
854                 });
855                 deleteProjectActions.put(project, new I18nAction("mainWindow.button.deleteProject") {
856
857                         /**
858                          * {@inheritDoc}
859                          */
860                         @SuppressWarnings("synthetic-access")
861                         public void actionPerformed(ActionEvent actionEvent) {
862                                 deleteProject(project);
863                         }
864                 });
865                 projectList.add(project);
866                 mainWindow.addProject(project, switchToProject);
867         }
868
869         /**
870          * {@inheritDoc}
871          */
872         public void projectRemoved(Project project) {
873                 /* TODO - implement */
874         }
875
876         /**
877          * {@inheritDoc}
878          */
879         public void loadingNodesDone(String directory) {
880                 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.loadingNodesDone"));
881         }
882
883         /**
884          * {@inheritDoc}
885          */
886         public void loadingNodesFailed(String directory, Throwable throwable) {
887                 /* TODO */
888         }
889
890         /**
891          * {@inheritDoc}
892          */
893         public void savingNodesDone(String directory) {
894                 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.savingNodesDone"));
895         }
896
897         /**
898          * {@inheritDoc}
899          */
900         public void savingNodesFailed(String directory, Throwable throwable) {
901                 /* TODO */
902         }
903
904         /**
905          * {@inheritDoc}
906          */
907         public void coreLoaded() {
908                 mainWindow.setVisible(true);
909                 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.coreLoaded"));
910         }
911
912         /**
913          * {@inheritDoc}
914          */
915         public void coreStopped() {
916                 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.coreStopped"));
917         }
918
919         /**
920          * {@inheritDoc}
921          */
922         public void nodeAdded(final Node node) {
923                 logger.log(Level.INFO, "node added: " + node);
924                 nodeList.add(node);
925                 node.addPropertyChangeListener(this);
926                 logger.log(Level.FINE, "nodeList.size(): " + nodeList.size());
927                 nodeConnectActions.put(node, new I18nAction("mainWindow.menu.node.item.connect") {
928
929                         /**
930                          * {@inheritDoc}
931                          */
932                         @SuppressWarnings("synthetic-access")
933                         public void actionPerformed(ActionEvent e) {
934                                 nodeConnect(node);
935                         }
936                 });
937                 nodeDisconnectActions.put(node, new I18nAction("mainWindow.menu.node.item.disconnect") {
938
939                         /**
940                          * {@inheritDoc}
941                          */
942                         @SuppressWarnings("synthetic-access")
943                         public void actionPerformed(ActionEvent e) {
944                                 nodeDisconnect(node);
945                         }
946                 });
947                 nodeDisconnectActions.get(node).setEnabled(false);
948                 nodeEditActions.put(node, new I18nAction("mainWindow.menu.node.item.edit") {
949
950                         /**
951                          * {@inheritDoc}
952                          */
953                         @SuppressWarnings("synthetic-access")
954                         public void actionPerformed(ActionEvent actionEvent) {
955                                 editNode(node);
956                         }
957                 });
958                 nodeDeleteActions.put(node, new I18nAction("mainWindow.menu.node.item.remove") {
959
960                         /**
961                          * {@inheritDoc}
962                          */
963                         @SuppressWarnings("synthetic-access")
964                         public void actionPerformed(ActionEvent actionEvent) {
965                                 deleteNode(node);
966                         }
967                 });
968                 mainWindow.addNode(node);
969         }
970
971         /**
972          * {@inheritDoc}
973          */
974         public void nodeRemoved(Node node) {
975                 logger.log(Level.INFO, "node removed: " + node);
976                 nodeList.remove(node);
977                 node.removePropertyChangeListener(this);
978                 nodeConnectActions.remove(node);
979                 nodeDisconnectActions.remove(node);
980                 nodeEditActions.remove(node);
981                 nodeDeleteActions.remove(node);
982                 mainWindow.removeNode(node);
983         }
984
985         /**
986          * {@inheritDoc}
987          */
988         public void nodeConnecting(Node node) {
989                 nodeConnectActions.get(node).setEnabled(false);
990                 nodeEditActions.get(node).setEnabled(false);
991                 nodeDeleteActions.get(node).setEnabled(false);
992                 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.connectingToNode", node.getName(), node.getHostname(), node.getPort()));
993         }
994
995         /**
996          * {@inheritDoc}
997          */
998         public void nodeConnected(Node node) {
999                 nodeDisconnectActions.get(node).setEnabled(true);
1000                 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.connectedToNode", node.getName(), node.getHostname(), node.getPort()));
1001                 mainWindow.setOnline(node);
1002         }
1003
1004         /**
1005          * {@inheritDoc}
1006          */
1007         public void nodeConnectionFailed(Node node, Throwable cause) {
1008                 nodeConnectActions.get(node).setEnabled(true);
1009                 nodeEditActions.get(node).setEnabled(true);
1010                 nodeDeleteActions.get(node).setEnabled(true);
1011                 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.connectionToNodeFailed", node.getName(), node.getHostname(), node.getPort(), (cause != null) ? cause.getMessage() : "no reason given"));
1012                 mainWindow.setError(node);
1013                 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);
1014         }
1015
1016         /**
1017          * {@inheritDoc}
1018          */
1019         public void nodeDisconnected(Node node, Throwable throwable) {
1020                 nodeDisconnectActions.get(node).setEnabled(false);
1021                 nodeConnectActions.get(node).setEnabled(true);
1022                 nodeEditActions.get(node).setEnabled(true);
1023                 nodeDeleteActions.get(node).setEnabled(true);
1024                 mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.disconnectedFromNode", node.getName(), node.getHostname(), node.getPort()));
1025                 mainWindow.setOffline(node);
1026         }
1027
1028         /**
1029          * @see net.pterodactylus.jsite.core.CoreListener#projectInsertStarted(net.pterodactylus.jsite.project.Project)
1030          */
1031         public void projectInsertStarted(Project project) {
1032                 mainWindow.projectInsertStarted(project);
1033         }
1034
1035         /**
1036          * @see net.pterodactylus.jsite.core.CoreListener#projectInsertProgressed(net.pterodactylus.jsite.project.Project,
1037          *      int, int, int, int, int, boolean)
1038          */
1039         public void projectInsertProgressed(Project project, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean finalizedTotal) {
1040                 mainWindow.projectInsertProgressed(project, totalBlocks, requiredBlocks, successfulBlocks, failedBlocks, fatallyFailedBlocks, finalizedTotal);
1041         }
1042
1043         /**
1044          * @see net.pterodactylus.jsite.core.CoreListener#projectInsertGeneratedURI(net.pterodactylus.jsite.project.Project,
1045          *      java.lang.String)
1046          */
1047         public void projectInsertGeneratedURI(Project project, String uri) {
1048                 mainWindow.projectInsertGeneratedURI(project);
1049         }
1050
1051         /**
1052          * @see net.pterodactylus.jsite.core.CoreListener#projectInsertFinished(net.pterodactylus.jsite.project.Project,
1053          *      boolean)
1054          */
1055         public void projectInsertFinished(Project project, boolean success) {
1056                 mainWindow.projectInsertFinished(project, success);
1057         }
1058
1059         //
1060         // INTERFACE LoggingListener
1061         //
1062
1063         /**
1064          * {@inheritDoc}
1065          */
1066         public void logged(LogRecord logRecord) {
1067                 logWindow.logged(logRecord);
1068         }
1069
1070         //
1071         // INTERFACE PropertyChangeListener
1072         //
1073
1074         /**
1075          * {@inheritDoc}
1076          */
1077         public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
1078                 /* do not react to anything (yet). */
1079         }
1080
1081 }