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