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