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