Format supported languages differently.
[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, 11, 1);
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         };
131
132         /** The actions that switch the language. */
133         private Map<Locale, Action> languageActions = new HashMap<Locale, Action>();
134
135         /** The “manage nodes” action. */
136         private Action manageNodeAction;
137
138         /** The “preferences” action. */
139         private Action optionsPreferencesAction;
140
141         /** The “check for updates” action. */
142         private Action checkForUpdatesAction;
143
144         /** The “about jSite” action. */
145         private Action aboutAction;
146
147         /** The wizard. */
148         private TWizard wizard;
149
150         /** The node menu. */
151         private JMenu nodeMenu;
152
153         /** The currently selected node. */
154         private Node selectedNode;
155
156         /** Mapping from page type to page. */
157         private final Map<PageType, TWizardPage> pages = new HashMap<PageType, TWizardPage>();
158
159         /** The original location of the configuration file. */
160         private ConfigurationLocation originalLocation;
161
162         /**
163          * Creates a new core with the default configuration file.
164          */
165         private Main() {
166                 this(null);
167         }
168
169         /**
170          * Creates a new core with the given configuration from the given file.
171          *
172          * @param configFilename
173          *            The name of the configuration file
174          */
175         private Main(String configFilename) {
176                 /* collect all possible configuration file locations. */
177                 ConfigurationLocator configurationLocator = new ConfigurationLocator();
178                 if (configFilename != null) {
179                         configurationLocator.setCustomLocation(configFilename);
180                 }
181
182                 originalLocation = configurationLocator.findPreferredLocation();
183                 logger.log(Level.CONFIG, "Using configuration from " + originalLocation + ".");
184                 configuration = new Configuration(configurationLocator, originalLocation);
185
186                 Locale.setDefault(configuration.getLocale());
187                 I18n.setLocale(configuration.getLocale());
188                 wizard = new TWizard();
189                 createActions();
190                 wizard.setJMenuBar(createMenuBar());
191                 wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
192                 wizard.setPreviousEnabled(false);
193                 wizard.setNextEnabled(true);
194                 wizard.addWizardListener(this);
195                 jSiteIcon = IconLoader.loadIcon("/jsite-icon.png");
196                 wizard.setIcon(jSiteIcon);
197
198                 updateChecker = new UpdateChecker(freenetInterface);
199                 updateChecker.addUpdateListener(this);
200                 updateChecker.start();
201
202                 webOfTrustInterface = new WebOfTrustInterface(freenetInterface);
203
204                 initPages();
205                 showPage(PageType.PAGE_PROJECTS);
206         }
207
208         /**
209          * Creates all actions.
210          */
211         private void createActions() {
212                 for (final Locale locale : SUPPORTED_LOCALES) {
213                         languageActions.put(locale, new AbstractAction(I18n.getMessage("jsite.menu.language." + locale.getLanguage()), IconLoader.loadIcon("/flag-" + locale.getLanguage() + ".png")) {
214
215                                 @Override
216                                 @SuppressWarnings("synthetic-access")
217                                 public void actionPerformed(ActionEvent actionEvent) {
218                                         switchLanguage(locale);
219                                 }
220                         });
221                 }
222                 manageNodeAction = new AbstractAction(I18n.getMessage("jsite.menu.nodes.manage-nodes")) {
223
224                         @Override
225                         @SuppressWarnings("synthetic-access")
226                         public void actionPerformed(ActionEvent actionEvent) {
227                                 showPage(PageType.PAGE_NODE_MANAGER);
228                                 optionsPreferencesAction.setEnabled(true);
229                                 wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
230                                 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
231                         }
232                 };
233                 optionsPreferencesAction = new AbstractAction(I18n.getMessage("jsite.menu.options.preferences")) {
234
235                         /**
236                          * {@inheritDoc}
237                          */
238                         @Override
239                         @SuppressWarnings("synthetic-access")
240                         public void actionPerformed(ActionEvent actionEvent) {
241                                 optionsPreferences();
242                         }
243                 };
244                 checkForUpdatesAction = new AbstractAction(I18n.getMessage("jsite.menu.help.check-for-updates")) {
245
246                         /**
247                          * {@inheritDoc}
248                          */
249                         @Override
250                         @SuppressWarnings("synthetic-access")
251                         public void actionPerformed(ActionEvent actionEvent) {
252                                 showLatestUpdate();
253                         }
254                 };
255                 aboutAction = new AbstractAction(I18n.getMessage("jsite.menu.help.about")) {
256
257                         @Override
258                         @SuppressWarnings("synthetic-access")
259                         public void actionPerformed(ActionEvent e) {
260                                 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.about.message"), getVersion().toString()), null, JOptionPane.INFORMATION_MESSAGE, jSiteIcon);
261                         }
262                 };
263
264                 I18nContainer.getInstance().registerRunnable(new Runnable() {
265
266                         @Override
267                         @SuppressWarnings("synthetic-access")
268                         public void run() {
269                                 manageNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.nodes.manage-nodes"));
270                                 optionsPreferencesAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.options.preferences"));
271                                 checkForUpdatesAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.help.check-for-updates"));
272                                 aboutAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.help.about"));
273                         }
274                 });
275         }
276
277         /**
278          * Creates the menu bar.
279          *
280          * @return The menu bar
281          */
282         private JMenuBar createMenuBar() {
283                 JMenuBar menuBar = new JMenuBar();
284                 final JMenu languageMenu = new JMenu(I18n.getMessage("jsite.menu.languages"));
285                 menuBar.add(languageMenu);
286                 ButtonGroup languageButtonGroup = new ButtonGroup();
287                 for (Locale locale : SUPPORTED_LOCALES) {
288                         Action languageAction = languageActions.get(locale);
289                         JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(languageActions.get(locale));
290                         if (locale.equals(Locale.getDefault())) {
291                                 menuItem.setSelected(true);
292                         }
293                         languageAction.putValue("menuItem", menuItem);
294                         languageButtonGroup.add(menuItem);
295                         languageMenu.add(menuItem);
296                 }
297                 nodeMenu = new JMenu(I18n.getMessage("jsite.menu.nodes"));
298                 menuBar.add(nodeMenu);
299                 selectedNode = configuration.getSelectedNode();
300                 nodesUpdated(configuration.getNodes());
301
302                 final JMenu optionsMenu = new JMenu(I18n.getMessage("jsite.menu.options"));
303                 menuBar.add(optionsMenu);
304                 optionsMenu.add(optionsPreferencesAction);
305
306                 /* evil hack to right-align the help menu */
307                 JPanel panel = new JPanel();
308                 panel.setOpaque(false);
309                 menuBar.add(panel);
310
311                 final JMenu helpMenu = new JMenu(I18n.getMessage("jsite.menu.help"));
312                 menuBar.add(helpMenu);
313                 helpMenu.add(checkForUpdatesAction);
314                 helpMenu.add(aboutAction);
315
316                 I18nContainer.getInstance().registerRunnable(new Runnable() {
317
318                         @Override
319                         @SuppressWarnings("synthetic-access")
320                         public void run() {
321                                 languageMenu.setText(I18n.getMessage("jsite.menu.languages"));
322                                 nodeMenu.setText(I18n.getMessage("jsite.menu.nodes"));
323                                 optionsMenu.setText(I18n.getMessage("jsite.menu.options"));
324                                 helpMenu.setText(I18n.getMessage("jsite.menu.help"));
325                                 for (Map.Entry<Locale, Action> languageActionEntry : languageActions.entrySet()) {
326                                         languageActionEntry.getValue().putValue(Action.NAME, I18n.getMessage("jsite.menu.language." + languageActionEntry.getKey().getLanguage()));
327                                 }
328                         }
329                 });
330
331                 return menuBar;
332         }
333
334         /**
335          * Initializes all pages.
336          */
337         private void initPages() {
338                 NodeManagerPage nodeManagerPage = new NodeManagerPage(wizard);
339                 nodeManagerPage.setName("page.node-manager");
340                 nodeManagerPage.addNodeManagerListener(this);
341                 nodeManagerPage.setNodes(configuration.getNodes());
342                 pages.put(PageType.PAGE_NODE_MANAGER, nodeManagerPage);
343
344                 ProjectPage projectPage = new ProjectPage(wizard);
345                 projectPage.setName("page.project");
346                 projectPage.setProjects(configuration.getProjects());
347                 projectPage.setFreenetInterface(freenetInterface);
348                 projectPage.setWebOfTrustInterface(webOfTrustInterface);
349                 projectPage.addListSelectionListener(this);
350                 pages.put(PageType.PAGE_PROJECTS, projectPage);
351
352                 ProjectFilesPage projectFilesPage = new ProjectFilesPage(wizard);
353                 projectFilesPage.setName("page.project.files");
354                 pages.put(PageType.PAGE_PROJECT_FILES, projectFilesPage);
355
356                 ProjectInsertPage projectInsertPage = new ProjectInsertPage(wizard);
357                 projectInsertPage.setName("page.project.insert");
358                 projectInsertPage.setFreenetInterface(freenetInterface);
359                 pages.put(PageType.PAGE_INSERT_PROJECT, projectInsertPage);
360
361                 PreferencesPage preferencesPage = new PreferencesPage(wizard);
362                 preferencesPage.setName("page.preferences");
363                 preferencesPage.setTempDirectory(configuration.getTempDirectory());
364                 pages.put(PageType.PAGE_PREFERENCES, preferencesPage);
365         }
366
367         /**
368          * Shows the page with the given type.
369          *
370          * @param pageType
371          *            The page type to show
372          */
373         private void showPage(PageType pageType) {
374                 wizard.setPreviousEnabled(pageType.ordinal() > 0);
375                 wizard.setNextEnabled(pageType.ordinal() < (pages.size() - 1));
376                 wizard.setPage(pages.get(pageType));
377                 wizard.setTitle(pages.get(pageType).getHeading() + " - jSite");
378         }
379
380         /**
381          * Returns whether a configuration file would be overwritten when calling
382          * {@link #saveConfiguration()}.
383          *
384          * @return {@code true} if {@link #saveConfiguration()} would overwrite an
385          *         existing file, {@code false} otherwise
386          */
387         private boolean isOverwritingConfiguration() {
388                 return configuration.getConfigurationLocator().hasFile(configuration.getConfigurationDirectory());
389         }
390
391         /**
392          * Saves the configuration.
393          *
394          * @return <code>true</code> if the configuration could be saved,
395          *         <code>false</code> otherwise
396          */
397         private boolean saveConfiguration() {
398                 NodeManagerPage nodeManagerPage = (NodeManagerPage) pages.get(PageType.PAGE_NODE_MANAGER);
399                 configuration.setNodes(nodeManagerPage.getNodes());
400                 if (selectedNode != null) {
401                         configuration.setSelectedNode(selectedNode);
402                 }
403
404                 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
405                 configuration.setProjects(projectPage.getProjects());
406
407                 PreferencesPage preferencesPage = (PreferencesPage) pages.get(PageType.PAGE_PREFERENCES);
408                 configuration.setTempDirectory(preferencesPage.getTempDirectory());
409
410                 return configuration.save();
411         }
412
413         /**
414          * Finds a supported locale for the given locale.
415          *
416          * @param forLocale
417          *            The locale to find a supported locale for
418          * @return The supported locale that was found, or the default locale if no
419          *         supported locale could be found
420          */
421         private static Locale findSupportedLocale(Locale forLocale) {
422                 for (Locale locale : SUPPORTED_LOCALES) {
423                         if (locale.equals(forLocale)) {
424                                 return locale;
425                         }
426                 }
427                 for (Locale locale : SUPPORTED_LOCALES) {
428                         if (locale.getCountry().equals(forLocale.getCountry()) && locale.getLanguage().equals(forLocale.getLanguage())) {
429                                 return locale;
430                         }
431                 }
432                 for (Locale locale : SUPPORTED_LOCALES) {
433                         if (locale.getLanguage().equals(forLocale.getLanguage())) {
434                                 return locale;
435                         }
436                 }
437                 return SUPPORTED_LOCALES[0];
438         }
439
440         /**
441          * Returns the version.
442          *
443          * @return The version
444          */
445         public static final Version getVersion() {
446                 return VERSION;
447         }
448
449         //
450         // ACTIONS
451         //
452
453         /**
454          * Switches the language of the interface to the given locale.
455          *
456          * @param locale
457          *            The locale to switch to
458          */
459         private void switchLanguage(Locale locale) {
460                 Locale supportedLocale = findSupportedLocale(locale);
461                 Action languageAction = languageActions.get(supportedLocale);
462                 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) languageAction.getValue("menuItem");
463                 menuItem.setSelected(true);
464                 I18n.setLocale(supportedLocale);
465                 for (Runnable i18nRunnable : I18nContainer.getInstance()) {
466                         try {
467                                 i18nRunnable.run();
468                         } catch (Throwable t) {
469                                 /* we probably shouldn't swallow this. */
470                         }
471                 }
472                 wizard.setPage(wizard.getPage());
473                 configuration.setLocale(supportedLocale);
474         }
475
476         /**
477          * Shows a dialog with general preferences.
478          */
479         private void optionsPreferences() {
480                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setConfigurationLocation(configuration.getConfigurationDirectory());
481                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setHasNextToJarConfiguration(configuration.getConfigurationLocator().isValidLocation(ConfigurationLocation.NEXT_TO_JAR_FILE));
482                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setHasCustomConfiguration(configuration.getConfigurationLocator().isValidLocation(ConfigurationLocation.CUSTOM));
483                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setUseEarlyEncode(configuration.useEarlyEncode());
484                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setPriority(configuration.getPriority());
485                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setManifestPutter(configuration.getManifestPutter());
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.setManifestPutter(configuration.getManifestPutter());
591                         projectInsertPage.startInsert();
592                         nodeMenu.setEnabled(false);
593                         optionsPreferencesAction.setEnabled(false);
594                 } else if ("page.project.insert".equals(pageName)) {
595                         ProjectInsertPage projectInsertPage = (ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT);
596                         if (projectInsertPage.isRunning()) {
597                                 projectInsertPage.stopInsert();
598                         } else {
599                                 showPage(PageType.PAGE_PROJECTS);
600                                 nodeMenu.setEnabled(true);
601                                 optionsPreferencesAction.setEnabled(true);
602                         }
603                 } else if ("page.preferences".equals(pageName)) {
604                         PreferencesPage preferencesPage = (PreferencesPage) pages.get(PageType.PAGE_PREFERENCES);
605                         showPage(PageType.PAGE_PROJECTS);
606                         optionsPreferencesAction.setEnabled(true);
607                         configuration.setUseEarlyEncode(preferencesPage.useEarlyEncode());
608                         configuration.setPriority(preferencesPage.getPriority());
609                         configuration.setManifestPutter(preferencesPage.getManifestPutter());
610                         configuration.setConfigurationLocation(preferencesPage.getConfigurationLocation());
611                 }
612         }
613
614         /**
615          * {@inheritDoc}
616          */
617         @Override
618         public void wizardPreviousPressed(TWizard wizard) {
619                 String pageName = wizard.getPage().getName();
620                 if ("page.project".equals(pageName) || "page.preferences".equals(pageName)) {
621                         showPage(PageType.PAGE_NODE_MANAGER);
622                         optionsPreferencesAction.setEnabled(true);
623                 } else if ("page.project.files".equals(pageName)) {
624                         showPage(PageType.PAGE_PROJECTS);
625                 } else if ("page.project.insert".equals(pageName)) {
626                         showPage(PageType.PAGE_PROJECT_FILES);
627                 }
628         }
629
630         /**
631          * {@inheritDoc}
632          */
633         @Override
634         public void wizardQuitPressed(TWizard wizard) {
635                 if (((ProjectPage) pages.get(PageType.PAGE_PROJECTS)).wasUriCopied() || ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).wasUriCopied()) {
636                         JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.use-clipboard-now"));
637                 }
638                 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) {
639                         if (isOverwritingConfiguration() && !originalLocation.equals(configuration.getConfigurationDirectory())) {
640                                 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);
641                                 if (overwriteConfigurationAnswer == JOptionPane.YES_OPTION) {
642                                         if (saveConfiguration()) {
643                                                 quit();
644                                         }
645                                 } else if (overwriteConfigurationAnswer == JOptionPane.CANCEL_OPTION) {
646                                         return;
647                                 }
648                                 if (overwriteConfigurationAnswer == JOptionPane.NO_OPTION) {
649                                         quit();
650                                 }
651                         } else {
652                                 if (saveConfiguration()) {
653                                         quit();
654                                 }
655                         }
656                         if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.config-not-saved"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
657                                 quit();
658                         }
659                 }
660         }
661
662         //
663         // INTERFACE NodeManagerListener
664         //
665
666         /**
667          * {@inheritDoc}
668          */
669         @Override
670         public void nodesUpdated(Node[] nodes) {
671                 nodeMenu.removeAll();
672                 ButtonGroup nodeButtonGroup = new ButtonGroup();
673                 Node newSelectedNode = null;
674                 for (Node node : nodes) {
675                         JRadioButtonMenuItem nodeMenuItem = new JRadioButtonMenuItem(node.getName());
676                         nodeMenuItem.putClientProperty("Node", node);
677                         nodeMenuItem.addActionListener(this);
678                         nodeButtonGroup.add(nodeMenuItem);
679                         if (node.equals(selectedNode)) {
680                                 newSelectedNode = node;
681                                 nodeMenuItem.setSelected(true);
682                         }
683                         nodeMenu.add(nodeMenuItem);
684                 }
685                 nodeMenu.addSeparator();
686                 nodeMenu.add(manageNodeAction);
687                 selectedNode = newSelectedNode;
688                 freenetInterface.setNode(selectedNode);
689         }
690
691         /**
692          * {@inheritDoc}
693          */
694         @Override
695         public void nodeSelected(Node node) {
696                 for (Component menuItem : nodeMenu.getMenuComponents()) {
697                         if (menuItem instanceof JMenuItem) {
698                                 if (node.equals(((JMenuItem) menuItem).getClientProperty("Node"))) {
699                                         ((JMenuItem) menuItem).setSelected(true);
700                                 }
701                         }
702                 }
703                 freenetInterface.setNode(node);
704                 selectedNode = node;
705         }
706
707         //
708         // INTERFACE ActionListener
709         //
710
711         /**
712          * {@inheritDoc}
713          */
714         @Override
715         public void actionPerformed(ActionEvent e) {
716                 Object source = e.getSource();
717                 if (source instanceof JRadioButtonMenuItem) {
718                         JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) source;
719                         Node node = (Node) menuItem.getClientProperty("Node");
720                         selectedNode = node;
721                         freenetInterface.setNode(selectedNode);
722                 }
723         }
724
725         //
726         // INTERFACE UpdateListener
727         //
728
729         /**
730          * {@inheritDoc}
731          */
732         @Override
733         public void foundUpdateData(Version foundVersion, long versionTimestamp) {
734                 logger.log(Level.FINEST, "Found version {0} from {1,date}.", new Object[] { foundVersion, versionTimestamp });
735                 if (foundVersion.compareTo(VERSION) > 0) {
736                         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);
737                 }
738         }
739
740         //
741         // MAIN METHOD
742         //
743
744         /**
745          * Main method that is called by the VM.
746          *
747          * @param args
748          *            The command-line arguments
749          */
750         public static void main(String[] args) {
751                 /* initialize logger. */
752                 Logger logger = Logger.getLogger("de.todesbaum");
753                 Handler handler = new ConsoleHandler();
754                 logger.addHandler(handler);
755                 String configFilename = null;
756                 boolean nextIsConfigFilename = false;
757                 for (String argument : args) {
758                         if (nextIsConfigFilename) {
759                                 configFilename = argument;
760                                 nextIsConfigFilename = false;
761                         }
762                         if ("--help".equals(argument)) {
763                                 printHelp();
764                                 return;
765                         } else if ("--debug".equals(argument)) {
766                                 logger.setLevel(Level.ALL);
767                                 handler.setLevel(Level.ALL);
768                         } else if ("--config-file".equals(argument)) {
769                                 nextIsConfigFilename = true;
770                         }
771                 }
772                 if (nextIsConfigFilename) {
773                         System.out.println("--config-file needs parameter!");
774                         return;
775                 }
776                 new Main(configFilename);
777         }
778
779         /**
780          * Prints a small syntax help.
781          */
782         private static void printHelp() {
783                 System.out.println("--help\tshows this cruft");
784                 System.out.println("--debug\tenables some debug output");
785                 System.out.println("--config-file <file>\tuse specified configuration file");
786         }
787
788 }