6a729125734bd314af367b5acdfa9cc93c6c2696
[jSite.git] / src / main / java / de / todesbaum / jsite / main / Main.java
1 /*
2  * jSite - Main.java - Copyright © 2006–2014 David Roden
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 package de.todesbaum.jsite.main;
20
21 import java.awt.Component;
22 import java.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
24 import java.io.IOException;
25 import java.text.MessageFormat;
26 import java.util.Date;
27 import java.util.HashMap;
28 import java.util.Locale;
29 import java.util.Map;
30 import java.util.logging.ConsoleHandler;
31 import java.util.logging.Handler;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
34
35 import javax.swing.AbstractAction;
36 import javax.swing.Action;
37 import javax.swing.ButtonGroup;
38 import javax.swing.Icon;
39 import javax.swing.JList;
40 import javax.swing.JMenu;
41 import javax.swing.JMenuBar;
42 import javax.swing.JMenuItem;
43 import javax.swing.JOptionPane;
44 import javax.swing.JPanel;
45 import javax.swing.JRadioButtonMenuItem;
46 import javax.swing.event.ListSelectionEvent;
47 import javax.swing.event.ListSelectionListener;
48
49 import net.pterodactylus.util.image.IconLoader;
50
51 import de.todesbaum.jsite.application.Freenet7Interface;
52 import de.todesbaum.jsite.application.Node;
53 import de.todesbaum.jsite.application.Project;
54 import de.todesbaum.jsite.application.UpdateChecker;
55 import de.todesbaum.jsite.application.UpdateListener;
56 import de.todesbaum.jsite.application.WebOfTrustInterface;
57 import de.todesbaum.jsite.application.validation.CheckReport;
58 import de.todesbaum.jsite.application.validation.Issue;
59 import de.todesbaum.jsite.application.validation.ProjectValidator;
60 import de.todesbaum.jsite.gui.NodeManagerListener;
61 import de.todesbaum.jsite.gui.NodeManagerPage;
62 import de.todesbaum.jsite.gui.PreferencesPage;
63 import de.todesbaum.jsite.gui.ProjectFilesPage;
64 import de.todesbaum.jsite.gui.ProjectInsertPage;
65 import de.todesbaum.jsite.gui.ProjectPage;
66 import de.todesbaum.jsite.i18n.I18n;
67 import de.todesbaum.jsite.i18n.I18nContainer;
68 import de.todesbaum.jsite.main.ConfigurationLocator.ConfigurationLocation;
69 import de.todesbaum.jsite.main.JarFileLocator.DefaultJarFileLocator;
70 import de.todesbaum.util.swing.TWizard;
71 import de.todesbaum.util.swing.TWizardPage;
72 import de.todesbaum.util.swing.WizardListener;
73
74 /**
75  * The main class that ties together everything.
76  *
77  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
78  */
79 public class Main implements ActionListener, ListSelectionListener, WizardListener, NodeManagerListener, UpdateListener {
80
81         /** The logger. */
82         private static final Logger logger = Logger.getLogger(Main.class.getName());
83
84         /** The version. */
85         private static final Version VERSION = new Version(0, 14);
86
87         /** The configuration. */
88         private Configuration configuration;
89
90         /** The freenet interface. */
91         private Freenet7Interface freenetInterface = new Freenet7Interface();
92
93         /** The update checker. */
94         private final UpdateChecker updateChecker;
95
96         /** The web of trust interface. */
97         private final WebOfTrustInterface webOfTrustInterface;
98
99         /** The jSite icon. */
100         private Icon jSiteIcon;
101
102         /**
103          * Enumeration for all possible pages.
104          *
105          * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
106          */
107         private static enum PageType {
108
109                 /** The node manager page. */
110                 PAGE_NODE_MANAGER,
111
112                 /** The project page. */
113                 PAGE_PROJECTS,
114
115                 /** The project files page. */
116                 PAGE_PROJECT_FILES,
117
118                 /** The project insert page. */
119                 PAGE_INSERT_PROJECT,
120
121                 /** The preferences page. */
122                 PAGE_PREFERENCES
123
124         }
125
126         /** The supported locales. */
127         private static final Locale[] SUPPORTED_LOCALES = new Locale[] {
128                         Locale.ENGLISH,
129                         Locale.GERMAN,
130                         Locale.FRENCH,
131                         Locale.ITALIAN,
132                         new Locale("es"),
133                         new Locale("pl"),
134                         new Locale("fi")
135         };
136
137         /** The actions that switch the language. */
138         private Map<Locale, Action> languageActions = new HashMap<Locale, Action>();
139
140         /** The “manage nodes” action. */
141         private Action manageNodeAction;
142
143         /** The “preferences” action. */
144         private Action optionsPreferencesAction;
145
146         /** The “check for updates” action. */
147         private Action checkForUpdatesAction;
148
149         /** The “about jSite” action. */
150         private Action aboutAction;
151
152         /** The wizard. */
153         private TWizard wizard;
154
155         /** The node menu. */
156         private JMenu nodeMenu;
157
158         /** The currently selected node. */
159         private Node selectedNode;
160
161         /** Mapping from page type to page. */
162         private final Map<PageType, TWizardPage> pages = new HashMap<PageType, TWizardPage>();
163
164         /** The original location of the configuration file. */
165         private ConfigurationLocation originalLocation;
166
167         /**
168          * Creates a new core with the default configuration file.
169          */
170         private Main() {
171                 this(null);
172         }
173
174         /**
175          * Creates a new core with the given configuration from the given file.
176          *
177          * @param configFilename
178          *            The name of the configuration file
179          */
180         private Main(String configFilename) {
181                 /* collect all possible configuration file locations. */
182                 ConfigurationLocator configurationLocator = new ConfigurationLocator(new DefaultJarFileLocator(getClass().getClassLoader()));
183                 if (configFilename != null) {
184                         configurationLocator.setCustomLocation(configFilename);
185                 }
186
187                 originalLocation = configurationLocator.findPreferredLocation();
188                 logger.log(Level.CONFIG, "Using configuration from " + originalLocation + ".");
189                 configuration = new Configuration(configurationLocator, originalLocation);
190
191                 Locale.setDefault(configuration.getLocale());
192                 I18n.setLocale(configuration.getLocale());
193                 wizard = new TWizard();
194                 createActions();
195                 wizard.setJMenuBar(createMenuBar());
196                 wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
197                 wizard.setPreviousEnabled(false);
198                 wizard.setNextEnabled(true);
199                 wizard.addWizardListener(this);
200                 jSiteIcon = IconLoader.loadIcon("/jsite-icon.png");
201                 wizard.setIcon(jSiteIcon);
202
203                 updateChecker = new UpdateChecker(freenetInterface, getVersion());
204                 updateChecker.addUpdateListener(this);
205                 updateChecker.start();
206
207                 webOfTrustInterface = new WebOfTrustInterface(freenetInterface);
208
209                 initPages();
210                 showPage(PageType.PAGE_PROJECTS);
211         }
212
213         /**
214          * Creates all actions.
215          */
216         private void createActions() {
217                 for (final Locale locale : SUPPORTED_LOCALES) {
218                         languageActions.put(locale, new AbstractAction(I18n.getMessage("jsite.menu.language." + locale.getLanguage()), IconLoader.loadIcon("/flag-" + locale.getLanguage() + ".png")) {
219
220                                 @Override
221                                 @SuppressWarnings("synthetic-access")
222                                 public void actionPerformed(ActionEvent actionEvent) {
223                                         switchLanguage(locale);
224                                 }
225                         });
226                 }
227                 manageNodeAction = new AbstractAction(I18n.getMessage("jsite.menu.nodes.manage-nodes")) {
228
229                         @Override
230                         @SuppressWarnings("synthetic-access")
231                         public void actionPerformed(ActionEvent actionEvent) {
232                                 showPage(PageType.PAGE_NODE_MANAGER);
233                                 optionsPreferencesAction.setEnabled(true);
234                                 wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
235                                 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
236                         }
237                 };
238                 optionsPreferencesAction = new AbstractAction(I18n.getMessage("jsite.menu.options.preferences")) {
239
240                         /**
241                          * {@inheritDoc}
242                          */
243                         @Override
244                         @SuppressWarnings("synthetic-access")
245                         public void actionPerformed(ActionEvent actionEvent) {
246                                 optionsPreferences();
247                         }
248                 };
249                 checkForUpdatesAction = new AbstractAction(I18n.getMessage("jsite.menu.help.check-for-updates")) {
250
251                         /**
252                          * {@inheritDoc}
253                          */
254                         @Override
255                         @SuppressWarnings("synthetic-access")
256                         public void actionPerformed(ActionEvent actionEvent) {
257                                 showLatestUpdate();
258                         }
259                 };
260                 aboutAction = new AbstractAction(I18n.getMessage("jsite.menu.help.about")) {
261
262                         @Override
263                         @SuppressWarnings("synthetic-access")
264                         public void actionPerformed(ActionEvent e) {
265                                 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.about.message"), getVersion().toString()), null, JOptionPane.INFORMATION_MESSAGE, jSiteIcon);
266                         }
267                 };
268
269                 I18nContainer.getInstance().registerRunnable(new Runnable() {
270
271                         @Override
272                         @SuppressWarnings("synthetic-access")
273                         public void run() {
274                                 manageNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.nodes.manage-nodes"));
275                                 optionsPreferencesAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.options.preferences"));
276                                 checkForUpdatesAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.help.check-for-updates"));
277                                 aboutAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.help.about"));
278                         }
279                 });
280         }
281
282         /**
283          * Creates the menu bar.
284          *
285          * @return The menu bar
286          */
287         private JMenuBar createMenuBar() {
288                 JMenuBar menuBar = new JMenuBar();
289                 final JMenu languageMenu = new JMenu(I18n.getMessage("jsite.menu.languages"));
290                 menuBar.add(languageMenu);
291                 ButtonGroup languageButtonGroup = new ButtonGroup();
292                 for (Locale locale : SUPPORTED_LOCALES) {
293                         Action languageAction = languageActions.get(locale);
294                         JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(languageActions.get(locale));
295                         if (locale.equals(Locale.getDefault())) {
296                                 menuItem.setSelected(true);
297                         }
298                         languageAction.putValue("menuItem", menuItem);
299                         languageButtonGroup.add(menuItem);
300                         languageMenu.add(menuItem);
301                 }
302                 nodeMenu = new JMenu(I18n.getMessage("jsite.menu.nodes"));
303                 menuBar.add(nodeMenu);
304                 selectedNode = configuration.getSelectedNode();
305                 nodesUpdated(configuration.getNodes());
306
307                 final JMenu optionsMenu = new JMenu(I18n.getMessage("jsite.menu.options"));
308                 menuBar.add(optionsMenu);
309                 optionsMenu.add(optionsPreferencesAction);
310
311                 /* evil hack to right-align the help menu */
312                 JPanel panel = new JPanel();
313                 panel.setOpaque(false);
314                 menuBar.add(panel);
315
316                 final JMenu helpMenu = new JMenu(I18n.getMessage("jsite.menu.help"));
317                 menuBar.add(helpMenu);
318                 helpMenu.add(checkForUpdatesAction);
319                 helpMenu.add(aboutAction);
320
321                 I18nContainer.getInstance().registerRunnable(new Runnable() {
322
323                         @Override
324                         @SuppressWarnings("synthetic-access")
325                         public void run() {
326                                 languageMenu.setText(I18n.getMessage("jsite.menu.languages"));
327                                 nodeMenu.setText(I18n.getMessage("jsite.menu.nodes"));
328                                 optionsMenu.setText(I18n.getMessage("jsite.menu.options"));
329                                 helpMenu.setText(I18n.getMessage("jsite.menu.help"));
330                                 for (Map.Entry<Locale, Action> languageActionEntry : languageActions.entrySet()) {
331                                         languageActionEntry.getValue().putValue(Action.NAME, I18n.getMessage("jsite.menu.language." + languageActionEntry.getKey().getLanguage()));
332                                 }
333                         }
334                 });
335
336                 return menuBar;
337         }
338
339         /**
340          * Initializes all pages.
341          */
342         private void initPages() {
343                 NodeManagerPage nodeManagerPage = new NodeManagerPage(wizard);
344                 nodeManagerPage.setName("page.node-manager");
345                 nodeManagerPage.addNodeManagerListener(this);
346                 nodeManagerPage.setNodes(configuration.getNodes());
347                 pages.put(PageType.PAGE_NODE_MANAGER, nodeManagerPage);
348
349                 ProjectPage projectPage = new ProjectPage(wizard);
350                 projectPage.setName("page.project");
351                 projectPage.setProjects(configuration.getProjects());
352                 projectPage.setFreenetInterface(freenetInterface);
353                 projectPage.setWebOfTrustInterface(webOfTrustInterface);
354                 projectPage.addListSelectionListener(this);
355                 pages.put(PageType.PAGE_PROJECTS, projectPage);
356
357                 ProjectFilesPage projectFilesPage = new ProjectFilesPage(wizard);
358                 projectFilesPage.setName("page.project.files");
359                 pages.put(PageType.PAGE_PROJECT_FILES, projectFilesPage);
360
361                 ProjectInsertPage projectInsertPage = new ProjectInsertPage(wizard);
362                 projectInsertPage.setName("page.project.insert");
363                 projectInsertPage.setFreenetInterface(freenetInterface);
364                 pages.put(PageType.PAGE_INSERT_PROJECT, projectInsertPage);
365
366                 PreferencesPage preferencesPage = new PreferencesPage(wizard);
367                 preferencesPage.setName("page.preferences");
368                 preferencesPage.setTempDirectory(configuration.getTempDirectory());
369                 pages.put(PageType.PAGE_PREFERENCES, preferencesPage);
370         }
371
372         /**
373          * Shows the page with the given type.
374          *
375          * @param pageType
376          *            The page type to show
377          */
378         private void showPage(PageType pageType) {
379                 wizard.setPreviousEnabled(pageType.ordinal() > 0);
380                 wizard.setNextEnabled(pageType.ordinal() < (pages.size() - 1));
381                 wizard.setPage(pages.get(pageType));
382                 wizard.setTitle(pages.get(pageType).getHeading() + " - jSite");
383         }
384
385         /**
386          * Returns whether a configuration file would be overwritten when calling
387          * {@link #saveConfiguration()}.
388          *
389          * @return {@code true} if {@link #saveConfiguration()} would overwrite an
390          *         existing file, {@code false} otherwise
391          */
392         private boolean isOverwritingConfiguration() {
393                 return configuration.getConfigurationLocator().hasFile(configuration.getConfigurationDirectory());
394         }
395
396         /**
397          * Saves the configuration.
398          *
399          * @return <code>true</code> if the configuration could be saved,
400          *         <code>false</code> otherwise
401          */
402         private boolean saveConfiguration() {
403                 NodeManagerPage nodeManagerPage = (NodeManagerPage) pages.get(PageType.PAGE_NODE_MANAGER);
404                 configuration.setNodes(nodeManagerPage.getNodes());
405                 if (selectedNode != null) {
406                         configuration.setSelectedNode(selectedNode);
407                 }
408
409                 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
410                 configuration.setProjects(projectPage.getProjects());
411
412                 PreferencesPage preferencesPage = (PreferencesPage) pages.get(PageType.PAGE_PREFERENCES);
413                 configuration.setTempDirectory(preferencesPage.getTempDirectory());
414
415                 return configuration.save();
416         }
417
418         /**
419          * Finds a supported locale for the given locale.
420          *
421          * @param forLocale
422          *            The locale to find a supported locale for
423          * @return The supported locale that was found, or the default locale if no
424          *         supported locale could be found
425          */
426         private static Locale findSupportedLocale(Locale forLocale) {
427                 for (Locale locale : SUPPORTED_LOCALES) {
428                         if (locale.equals(forLocale)) {
429                                 return locale;
430                         }
431                 }
432                 for (Locale locale : SUPPORTED_LOCALES) {
433                         if (locale.getCountry().equals(forLocale.getCountry()) && locale.getLanguage().equals(forLocale.getLanguage())) {
434                                 return locale;
435                         }
436                 }
437                 for (Locale locale : SUPPORTED_LOCALES) {
438                         if (locale.getLanguage().equals(forLocale.getLanguage())) {
439                                 return locale;
440                         }
441                 }
442                 return SUPPORTED_LOCALES[0];
443         }
444
445         /**
446          * Returns the version.
447          *
448          * @return The version
449          */
450         public static final Version getVersion() {
451                 return VERSION;
452         }
453
454         //
455         // ACTIONS
456         //
457
458         /**
459          * Switches the language of the interface to the given locale.
460          *
461          * @param locale
462          *            The locale to switch to
463          */
464         private void switchLanguage(Locale locale) {
465                 Locale supportedLocale = findSupportedLocale(locale);
466                 Action languageAction = languageActions.get(supportedLocale);
467                 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) languageAction.getValue("menuItem");
468                 menuItem.setSelected(true);
469                 I18n.setLocale(supportedLocale);
470                 for (Runnable i18nRunnable : I18nContainer.getInstance()) {
471                         try {
472                                 i18nRunnable.run();
473                         } catch (Throwable t) {
474                                 /* we probably shouldn't swallow this. */
475                         }
476                 }
477                 wizard.setPage(wizard.getPage());
478                 configuration.setLocale(supportedLocale);
479         }
480
481         /**
482          * Shows a dialog with general preferences.
483          */
484         private void optionsPreferences() {
485                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setConfigurationLocation(configuration.getConfigurationDirectory());
486                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setHasNextToJarConfiguration(configuration.getConfigurationLocator().isValidLocation(ConfigurationLocation.NEXT_TO_JAR_FILE));
487                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setHasCustomConfiguration(configuration.getConfigurationLocator().isValidLocation(ConfigurationLocation.CUSTOM));
488                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setUseEarlyEncode(configuration.useEarlyEncode());
489                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setPriority(configuration.getPriority());
490                 showPage(PageType.PAGE_PREFERENCES);
491                 optionsPreferencesAction.setEnabled(false);
492                 wizard.setNextEnabled(true);
493                 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
494         }
495
496         /**
497          * Shows a dialog box that shows the last version that was found by the
498          * {@link UpdateChecker}.
499          */
500         private void showLatestUpdate() {
501                 Version latestVersion = updateChecker.getLatestVersion();
502                 int versionDifference = latestVersion.compareTo(VERSION);
503                 if (versionDifference > 0) {
504                         JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.update-checker.latest-version.newer.message"), VERSION, latestVersion), I18n.getMessage("jsite.update-checker.latest-version.title"), JOptionPane.INFORMATION_MESSAGE);
505                 } else if (versionDifference < 0) {
506                         JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.update-checker.latest-version.older.message"), VERSION, latestVersion), I18n.getMessage("jsite.update-checker.latest-version.title"), JOptionPane.INFORMATION_MESSAGE);
507                 } else {
508                         JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.update-checker.latest-version.okay.message"), VERSION, latestVersion), I18n.getMessage("jsite.update-checker.latest-version.title"), JOptionPane.INFORMATION_MESSAGE);
509                 }
510         }
511
512         /**
513          * Quits jSite, stopping all background services.
514          */
515         private void quit() {
516                 updateChecker.stop();
517                 System.exit(0);
518         }
519
520         //
521         // INTERFACE ListSelectionListener
522         //
523
524         /**
525          * {@inheritDoc}
526          */
527         @Override
528         public void valueChanged(ListSelectionEvent e) {
529                 JList list = (JList) e.getSource();
530                 int selectedRow = list.getSelectedIndex();
531                 wizard.setNextEnabled(selectedRow > -1);
532         }
533
534         //
535         // INTERFACE WizardListener
536         //
537
538         /**
539          * {@inheritDoc}
540          */
541         @Override
542         public void wizardNextPressed(TWizard wizard) {
543                 String pageName = wizard.getPage().getName();
544                 if ("page.node-manager".equals(pageName)) {
545                         showPage(PageType.PAGE_PROJECTS);
546                 } else if ("page.project".equals(pageName)) {
547                         ProjectPage projectPage = (ProjectPage) wizard.getPage();
548                         Project project = projectPage.getSelectedProject();
549                         if ((project.getLocalPath() == null) || (project.getLocalPath().trim().length() == 0)) {
550                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.warning.no-local-path"), null, JOptionPane.ERROR_MESSAGE);
551                                 return;
552                         }
553                         if ((project.getPath() == null) || (project.getPath().trim().length() == 0)) {
554                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.warning.no-path"), null, JOptionPane.ERROR_MESSAGE);
555                                 return;
556                         }
557                         ((ProjectFilesPage) pages.get(PageType.PAGE_PROJECT_FILES)).setProject(project);
558                         ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).setProject(project);
559                         showPage(PageType.PAGE_PROJECT_FILES);
560                 } else if ("page.project.files".equals(pageName)) {
561                         ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
562                         Project project = projectPage.getSelectedProject();
563                         if (selectedNode == null) {
564                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.error.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
565                                 return;
566                         }
567                         CheckReport checkReport = ProjectValidator.validateProject(project);
568                         for (Issue issue : checkReport) {
569                                 if (issue.isFatal()) {
570                                         JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite." + issue.getErrorKey()), (Object[]) issue.getParameters()), null, JOptionPane.ERROR_MESSAGE);
571                                         return;
572                                 }
573                                 if (JOptionPane.showConfirmDialog(wizard, MessageFormat.format(I18n.getMessage("jsite." + issue.getErrorKey()), (Object[]) issue.getParameters()), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
574                                         return;
575                                 }
576                         }
577                         boolean nodeRunning = false;
578                         try {
579                                 nodeRunning = freenetInterface.isNodePresent();
580                         } catch (IOException e) {
581                                 /* ignore. */
582                         }
583                         if (!nodeRunning) {
584                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.error.no-node-running"), null, JOptionPane.ERROR_MESSAGE);
585                                 return;
586                         }
587                         configuration.save();
588                         showPage(PageType.PAGE_INSERT_PROJECT);
589                         ProjectInsertPage projectInsertPage = (ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT);
590                         String tempDirectory = ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).getTempDirectory();
591                         projectInsertPage.setTempDirectory(tempDirectory);
592                         projectInsertPage.setUseEarlyEncode(configuration.useEarlyEncode());
593                         projectInsertPage.setPriority(configuration.getPriority());
594                         projectInsertPage.startInsert();
595                         nodeMenu.setEnabled(false);
596                         optionsPreferencesAction.setEnabled(false);
597                 } else if ("page.project.insert".equals(pageName)) {
598                         ProjectInsertPage projectInsertPage = (ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT);
599                         if (projectInsertPage.isRunning()) {
600                                 projectInsertPage.stopInsert();
601                         } else {
602                                 showPage(PageType.PAGE_PROJECTS);
603                                 nodeMenu.setEnabled(true);
604                                 optionsPreferencesAction.setEnabled(true);
605                         }
606                 } else if ("page.preferences".equals(pageName)) {
607                         PreferencesPage preferencesPage = (PreferencesPage) pages.get(PageType.PAGE_PREFERENCES);
608                         showPage(PageType.PAGE_PROJECTS);
609                         optionsPreferencesAction.setEnabled(true);
610                         configuration.setUseEarlyEncode(preferencesPage.useEarlyEncode());
611                         configuration.setPriority(preferencesPage.getPriority());
612                         configuration.setConfigurationLocation(preferencesPage.getConfigurationLocation());
613                 }
614         }
615
616         /**
617          * {@inheritDoc}
618          */
619         @Override
620         public void wizardPreviousPressed(TWizard wizard) {
621                 String pageName = wizard.getPage().getName();
622                 if ("page.project".equals(pageName) || "page.preferences".equals(pageName)) {
623                         showPage(PageType.PAGE_NODE_MANAGER);
624                         optionsPreferencesAction.setEnabled(true);
625                 } else if ("page.project.files".equals(pageName)) {
626                         showPage(PageType.PAGE_PROJECTS);
627                 } else if ("page.project.insert".equals(pageName)) {
628                         showPage(PageType.PAGE_PROJECT_FILES);
629                 }
630         }
631
632         /**
633          * {@inheritDoc}
634          */
635         @Override
636         public void wizardQuitPressed(TWizard wizard) {
637                 if (((ProjectPage) pages.get(PageType.PAGE_PROJECTS)).wasUriCopied() || ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).wasUriCopied()) {
638                         JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.use-clipboard-now"));
639                 }
640                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.question"), I18n.getMessage("jsite.quit.question.title"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
641                         if (isOverwritingConfiguration() && !originalLocation.equals(configuration.getConfigurationDirectory())) {
642                                 int overwriteConfigurationAnswer = JOptionPane.showConfirmDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.quit.overwrite-configuration"), configuration.getConfigurationLocator().getFile(configuration.getConfigurationDirectory())), I18n.getMessage("jsite.quit.overwrite-configuration.title"), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
643                                 if (overwriteConfigurationAnswer == JOptionPane.YES_OPTION) {
644                                         if (saveConfiguration()) {
645                                                 quit();
646                                         }
647                                 } else if (overwriteConfigurationAnswer == JOptionPane.CANCEL_OPTION) {
648                                         return;
649                                 }
650                                 if (overwriteConfigurationAnswer == JOptionPane.NO_OPTION) {
651                                         quit();
652                                 }
653                         } else {
654                                 if (saveConfiguration()) {
655                                         quit();
656                                 }
657                         }
658                         if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.config-not-saved"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
659                                 quit();
660                         }
661                 }
662         }
663
664         //
665         // INTERFACE NodeManagerListener
666         //
667
668         /**
669          * {@inheritDoc}
670          */
671         @Override
672         public void nodesUpdated(Node[] nodes) {
673                 nodeMenu.removeAll();
674                 ButtonGroup nodeButtonGroup = new ButtonGroup();
675                 Node newSelectedNode = null;
676                 for (Node node : nodes) {
677                         JRadioButtonMenuItem nodeMenuItem = new JRadioButtonMenuItem(node.getName());
678                         nodeMenuItem.putClientProperty("Node", node);
679                         nodeMenuItem.addActionListener(this);
680                         nodeButtonGroup.add(nodeMenuItem);
681                         if (node.equals(selectedNode)) {
682                                 newSelectedNode = node;
683                                 nodeMenuItem.setSelected(true);
684                         }
685                         nodeMenu.add(nodeMenuItem);
686                 }
687                 nodeMenu.addSeparator();
688                 nodeMenu.add(manageNodeAction);
689                 selectedNode = newSelectedNode;
690                 freenetInterface.setNode(selectedNode);
691         }
692
693         /**
694          * {@inheritDoc}
695          */
696         @Override
697         public void nodeSelected(Node node) {
698                 for (Component menuItem : nodeMenu.getMenuComponents()) {
699                         if (menuItem instanceof JMenuItem) {
700                                 if (node.equals(((JMenuItem) menuItem).getClientProperty("Node"))) {
701                                         ((JMenuItem) menuItem).setSelected(true);
702                                 }
703                         }
704                 }
705                 freenetInterface.setNode(node);
706                 selectedNode = node;
707         }
708
709         //
710         // INTERFACE ActionListener
711         //
712
713         /**
714          * {@inheritDoc}
715          */
716         @Override
717         public void actionPerformed(ActionEvent e) {
718                 Object source = e.getSource();
719                 if (source instanceof JRadioButtonMenuItem) {
720                         JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) source;
721                         Node node = (Node) menuItem.getClientProperty("Node");
722                         selectedNode = node;
723                         freenetInterface.setNode(selectedNode);
724                 }
725         }
726
727         //
728         // INTERFACE UpdateListener
729         //
730
731         /**
732          * {@inheritDoc}
733          */
734         @Override
735         public void foundUpdateData(Version foundVersion, long versionTimestamp) {
736                 logger.log(Level.FINEST, "Found version {0} from {1,date}.", new Object[] { foundVersion, versionTimestamp });
737                 if (foundVersion.compareTo(VERSION) > 0) {
738                         JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.update-checker.found-version.message"), foundVersion.toString(), new Date(versionTimestamp)), I18n.getMessage("jsite.update-checker.found-version.title"), JOptionPane.INFORMATION_MESSAGE);
739                 }
740         }
741
742         //
743         // MAIN METHOD
744         //
745
746         /**
747          * Main method that is called by the VM.
748          *
749          * @param args
750          *            The command-line arguments
751          */
752         public static void main(String[] args) {
753                 /* initialize logger. */
754                 Logger logger = Logger.getLogger("de.todesbaum");
755                 Handler handler = new ConsoleHandler();
756                 logger.addHandler(handler);
757                 String configFilename = null;
758                 boolean nextIsConfigFilename = false;
759                 for (String argument : args) {
760                         if (nextIsConfigFilename) {
761                                 configFilename = argument;
762                                 nextIsConfigFilename = false;
763                         }
764                         if ("--help".equals(argument)) {
765                                 printHelp();
766                                 return;
767                         } else if ("--debug".equals(argument)) {
768                                 logger.setLevel(Level.ALL);
769                                 handler.setLevel(Level.ALL);
770                         } else if ("--config-file".equals(argument)) {
771                                 nextIsConfigFilename = true;
772                         }
773                 }
774                 if (nextIsConfigFilename) {
775                         System.out.println("--config-file needs parameter!");
776                         return;
777                 }
778                 new Main(configFilename);
779         }
780
781         /**
782          * Prints a small syntax help.
783          */
784         private static void printHelp() {
785                 System.out.println("--help\tshows this cruft");
786                 System.out.println("--debug\tenables some debug output");
787                 System.out.println("--config-file <file>\tuse specified configuration file");
788         }
789
790 }