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