8a4f905318535e7e8e9ba1aa0fde7b6ce994ee55
[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, 11, 1);
84
85         /** The configuration. */
86         private Configuration configuration;
87
88         /** The freenet interface. */
89         private Freenet7Interface freenetInterface = new Freenet7Interface();
90
91         /** The update checker. */
92         private final UpdateChecker updateChecker;
93
94         /** The web of trust interface. */
95         private final WebOfTrustInterface webOfTrustInterface;
96
97         /** The jSite icon. */
98         private Icon jSiteIcon;
99
100         /**
101          * Enumeration for all possible pages.
102          *
103          * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
104          */
105         private static enum PageType {
106
107                 /** The node manager page. */
108                 PAGE_NODE_MANAGER,
109
110                 /** The project page. */
111                 PAGE_PROJECTS,
112
113                 /** The project files page. */
114                 PAGE_PROJECT_FILES,
115
116                 /** The project insert page. */
117                 PAGE_INSERT_PROJECT,
118
119                 /** The preferences page. */
120                 PAGE_PREFERENCES
121
122         }
123
124         /** The supported locales. */
125         private static final Locale[] SUPPORTED_LOCALES = new Locale[] { Locale.ENGLISH, Locale.GERMAN, Locale.FRENCH, new Locale("pl") };
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.setWebOfTrustInterface(webOfTrustInterface);
345                 projectPage.addListSelectionListener(this);
346                 pages.put(PageType.PAGE_PROJECTS, projectPage);
347
348                 ProjectFilesPage projectFilesPage = new ProjectFilesPage(wizard);
349                 projectFilesPage.setName("page.project.files");
350                 pages.put(PageType.PAGE_PROJECT_FILES, projectFilesPage);
351
352                 ProjectInsertPage projectInsertPage = new ProjectInsertPage(wizard);
353                 projectInsertPage.setName("page.project.insert");
354                 projectInsertPage.setFreenetInterface(freenetInterface);
355                 pages.put(PageType.PAGE_INSERT_PROJECT, projectInsertPage);
356
357                 PreferencesPage preferencesPage = new PreferencesPage(wizard);
358                 preferencesPage.setName("page.preferences");
359                 preferencesPage.setTempDirectory(configuration.getTempDirectory());
360                 pages.put(PageType.PAGE_PREFERENCES, preferencesPage);
361         }
362
363         /**
364          * Shows the page with the given type.
365          *
366          * @param pageType
367          *            The page type to show
368          */
369         private void showPage(PageType pageType) {
370                 wizard.setPreviousEnabled(pageType.ordinal() > 0);
371                 wizard.setNextEnabled(pageType.ordinal() < (pages.size() - 1));
372                 wizard.setPage(pages.get(pageType));
373                 wizard.setTitle(pages.get(pageType).getHeading() + " - jSite");
374         }
375
376         /**
377          * Returns whether a configuration file would be overwritten when calling
378          * {@link #saveConfiguration()}.
379          *
380          * @return {@code true} if {@link #saveConfiguration()} would overwrite an
381          *         existing file, {@code false} otherwise
382          */
383         private boolean isOverwritingConfiguration() {
384                 return configuration.getConfigurationLocator().hasFile(configuration.getConfigurationDirectory());
385         }
386
387         /**
388          * Saves the configuration.
389          *
390          * @return <code>true</code> if the configuration could be saved,
391          *         <code>false</code> otherwise
392          */
393         private boolean saveConfiguration() {
394                 NodeManagerPage nodeManagerPage = (NodeManagerPage) pages.get(PageType.PAGE_NODE_MANAGER);
395                 configuration.setNodes(nodeManagerPage.getNodes());
396                 if (selectedNode != null) {
397                         configuration.setSelectedNode(selectedNode);
398                 }
399
400                 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
401                 configuration.setProjects(projectPage.getProjects());
402
403                 PreferencesPage preferencesPage = (PreferencesPage) pages.get(PageType.PAGE_PREFERENCES);
404                 configuration.setTempDirectory(preferencesPage.getTempDirectory());
405
406                 return configuration.save();
407         }
408
409         /**
410          * Finds a supported locale for the given locale.
411          *
412          * @param forLocale
413          *            The locale to find a supported locale for
414          * @return The supported locale that was found, or the default locale if no
415          *         supported locale could be found
416          */
417         private static Locale findSupportedLocale(Locale forLocale) {
418                 for (Locale locale : SUPPORTED_LOCALES) {
419                         if (locale.equals(forLocale)) {
420                                 return locale;
421                         }
422                 }
423                 for (Locale locale : SUPPORTED_LOCALES) {
424                         if (locale.getCountry().equals(forLocale.getCountry()) && locale.getLanguage().equals(forLocale.getLanguage())) {
425                                 return locale;
426                         }
427                 }
428                 for (Locale locale : SUPPORTED_LOCALES) {
429                         if (locale.getLanguage().equals(forLocale.getLanguage())) {
430                                 return locale;
431                         }
432                 }
433                 return SUPPORTED_LOCALES[0];
434         }
435
436         /**
437          * Returns the version.
438          *
439          * @return The version
440          */
441         public static final Version getVersion() {
442                 return VERSION;
443         }
444
445         //
446         // ACTIONS
447         //
448
449         /**
450          * Switches the language of the interface to the given locale.
451          *
452          * @param locale
453          *            The locale to switch to
454          */
455         private void switchLanguage(Locale locale) {
456                 Locale supportedLocale = findSupportedLocale(locale);
457                 Action languageAction = languageActions.get(supportedLocale);
458                 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) languageAction.getValue("menuItem");
459                 menuItem.setSelected(true);
460                 I18n.setLocale(supportedLocale);
461                 for (Runnable i18nRunnable : I18nContainer.getInstance()) {
462                         try {
463                                 i18nRunnable.run();
464                         } catch (Throwable t) {
465                                 /* we probably shouldn't swallow this. */
466                         }
467                 }
468                 wizard.setPage(wizard.getPage());
469                 configuration.setLocale(supportedLocale);
470         }
471
472         /**
473          * Shows a dialog with general preferences.
474          */
475         private void optionsPreferences() {
476                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setConfigurationLocation(configuration.getConfigurationDirectory());
477                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setHasNextToJarConfiguration(configuration.getConfigurationLocator().isValidLocation(ConfigurationLocation.NEXT_TO_JAR_FILE));
478                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setHasCustomConfiguration(configuration.getConfigurationLocator().isValidLocation(ConfigurationLocation.CUSTOM));
479                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setUseEarlyEncode(configuration.useEarlyEncode());
480                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setPriority(configuration.getPriority());
481                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setManifestPutter(configuration.getManifestPutter());
482                 showPage(PageType.PAGE_PREFERENCES);
483                 optionsPreferencesAction.setEnabled(false);
484                 wizard.setNextEnabled(true);
485                 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
486         }
487
488         /**
489          * Shows a dialog box that shows the last version that was found by the
490          * {@link UpdateChecker}.
491          */
492         private void showLatestUpdate() {
493                 Version latestVersion = updateChecker.getLatestVersion();
494                 int versionDifference = latestVersion.compareTo(VERSION);
495                 if (versionDifference > 0) {
496                         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);
497                 } else if (versionDifference < 0) {
498                         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);
499                 } else {
500                         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);
501                 }
502         }
503
504         /**
505          * Quits jSite, stopping all background services.
506          */
507         private void quit() {
508                 updateChecker.stop();
509                 webOfTrustInterface.stop();
510                 System.exit(0);
511         }
512
513         //
514         // INTERFACE ListSelectionListener
515         //
516
517         /**
518          * {@inheritDoc}
519          */
520         @Override
521         public void valueChanged(ListSelectionEvent e) {
522                 JList list = (JList) e.getSource();
523                 int selectedRow = list.getSelectedIndex();
524                 wizard.setNextEnabled(selectedRow > -1);
525         }
526
527         //
528         // INTERFACE WizardListener
529         //
530
531         /**
532          * {@inheritDoc}
533          */
534         @Override
535         public void wizardNextPressed(TWizard wizard) {
536                 String pageName = wizard.getPage().getName();
537                 if ("page.node-manager".equals(pageName)) {
538                         showPage(PageType.PAGE_PROJECTS);
539                 } else if ("page.project".equals(pageName)) {
540                         ProjectPage projectPage = (ProjectPage) wizard.getPage();
541                         Project project = projectPage.getSelectedProject();
542                         if ((project.getLocalPath() == null) || (project.getLocalPath().trim().length() == 0)) {
543                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.warning.no-local-path"), null, JOptionPane.ERROR_MESSAGE);
544                                 return;
545                         }
546                         if ((project.getPath() == null) || (project.getPath().trim().length() == 0)) {
547                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.warning.no-path"), null, JOptionPane.ERROR_MESSAGE);
548                                 return;
549                         }
550                         ((ProjectFilesPage) pages.get(PageType.PAGE_PROJECT_FILES)).setProject(project);
551                         ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).setProject(project);
552                         showPage(PageType.PAGE_PROJECT_FILES);
553                 } else if ("page.project.files".equals(pageName)) {
554                         ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
555                         Project project = projectPage.getSelectedProject();
556                         if (selectedNode == null) {
557                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.error.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
558                                 return;
559                         }
560                         CheckReport checkReport = ProjectInserter.validateProject(project);
561                         for (Issue issue : checkReport) {
562                                 if (issue.isFatal()) {
563                                         JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite." + issue.getErrorKey()), (Object[]) issue.getParameters()), null, JOptionPane.ERROR_MESSAGE);
564                                         return;
565                                 }
566                                 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) {
567                                         return;
568                                 }
569                         }
570                         boolean nodeRunning = false;
571                         try {
572                                 nodeRunning = freenetInterface.isNodePresent();
573                         } catch (IOException e) {
574                                 /* ignore. */
575                         }
576                         if (!nodeRunning) {
577                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.error.no-node-running"), null, JOptionPane.ERROR_MESSAGE);
578                                 return;
579                         }
580                         configuration.save();
581                         showPage(PageType.PAGE_INSERT_PROJECT);
582                         ProjectInsertPage projectInsertPage = (ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT);
583                         String tempDirectory = ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).getTempDirectory();
584                         projectInsertPage.setTempDirectory(tempDirectory);
585                         projectInsertPage.setUseEarlyEncode(configuration.useEarlyEncode());
586                         projectInsertPage.setPriority(configuration.getPriority());
587                         projectInsertPage.setManifestPutter(configuration.getManifestPutter());
588                         projectInsertPage.startInsert();
589                         nodeMenu.setEnabled(false);
590                         optionsPreferencesAction.setEnabled(false);
591                 } else if ("page.project.insert".equals(pageName)) {
592                         ProjectInsertPage projectInsertPage = (ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT);
593                         if (projectInsertPage.isRunning()) {
594                                 projectInsertPage.stopInsert();
595                         } else {
596                                 showPage(PageType.PAGE_PROJECTS);
597                                 nodeMenu.setEnabled(true);
598                                 optionsPreferencesAction.setEnabled(true);
599                         }
600                 } else if ("page.preferences".equals(pageName)) {
601                         PreferencesPage preferencesPage = (PreferencesPage) pages.get(PageType.PAGE_PREFERENCES);
602                         showPage(PageType.PAGE_PROJECTS);
603                         optionsPreferencesAction.setEnabled(true);
604                         configuration.setUseEarlyEncode(preferencesPage.useEarlyEncode());
605                         configuration.setPriority(preferencesPage.getPriority());
606                         configuration.setManifestPutter(preferencesPage.getManifestPutter());
607                         configuration.setConfigurationLocation(preferencesPage.getConfigurationLocation());
608                 }
609         }
610
611         /**
612          * {@inheritDoc}
613          */
614         @Override
615         public void wizardPreviousPressed(TWizard wizard) {
616                 String pageName = wizard.getPage().getName();
617                 if ("page.project".equals(pageName) || "page.preferences".equals(pageName)) {
618                         showPage(PageType.PAGE_NODE_MANAGER);
619                         optionsPreferencesAction.setEnabled(true);
620                 } else if ("page.project.files".equals(pageName)) {
621                         showPage(PageType.PAGE_PROJECTS);
622                 } else if ("page.project.insert".equals(pageName)) {
623                         showPage(PageType.PAGE_PROJECT_FILES);
624                 }
625         }
626
627         /**
628          * {@inheritDoc}
629          */
630         @Override
631         public void wizardQuitPressed(TWizard wizard) {
632                 if (((ProjectPage) pages.get(PageType.PAGE_PROJECTS)).wasUriCopied() || ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).wasUriCopied()) {
633                         JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.use-clipboard-now"));
634                 }
635                 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) {
636                         if (isOverwritingConfiguration() && !originalLocation.equals(configuration.getConfigurationDirectory())) {
637                                 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);
638                                 if (overwriteConfigurationAnswer == JOptionPane.YES_OPTION) {
639                                         if (saveConfiguration()) {
640                                                 quit();
641                                         }
642                                 } else if (overwriteConfigurationAnswer == JOptionPane.CANCEL_OPTION) {
643                                         return;
644                                 }
645                                 if (overwriteConfigurationAnswer == JOptionPane.NO_OPTION) {
646                                         quit();
647                                 }
648                         } else {
649                                 if (saveConfiguration()) {
650                                         quit();
651                                 }
652                         }
653                         if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.config-not-saved"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
654                                 quit();
655                         }
656                 }
657         }
658
659         //
660         // INTERFACE NodeManagerListener
661         //
662
663         /**
664          * {@inheritDoc}
665          */
666         @Override
667         public void nodesUpdated(Node[] nodes) {
668                 nodeMenu.removeAll();
669                 ButtonGroup nodeButtonGroup = new ButtonGroup();
670                 Node newSelectedNode = null;
671                 for (Node node : nodes) {
672                         JRadioButtonMenuItem nodeMenuItem = new JRadioButtonMenuItem(node.getName());
673                         nodeMenuItem.putClientProperty("Node", node);
674                         nodeMenuItem.addActionListener(this);
675                         nodeButtonGroup.add(nodeMenuItem);
676                         if (node.equals(selectedNode)) {
677                                 newSelectedNode = node;
678                                 nodeMenuItem.setSelected(true);
679                         }
680                         nodeMenu.add(nodeMenuItem);
681                 }
682                 nodeMenu.addSeparator();
683                 nodeMenu.add(manageNodeAction);
684                 selectedNode = newSelectedNode;
685                 freenetInterface.setNode(selectedNode);
686         }
687
688         /**
689          * {@inheritDoc}
690          */
691         @Override
692         public void nodeSelected(Node node) {
693                 for (Component menuItem : nodeMenu.getMenuComponents()) {
694                         if (menuItem instanceof JMenuItem) {
695                                 if (node.equals(((JMenuItem) menuItem).getClientProperty("Node"))) {
696                                         ((JMenuItem) menuItem).setSelected(true);
697                                 }
698                         }
699                 }
700                 freenetInterface.setNode(node);
701                 selectedNode = node;
702         }
703
704         //
705         // INTERFACE ActionListener
706         //
707
708         /**
709          * {@inheritDoc}
710          */
711         @Override
712         public void actionPerformed(ActionEvent e) {
713                 Object source = e.getSource();
714                 if (source instanceof JRadioButtonMenuItem) {
715                         JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) source;
716                         Node node = (Node) menuItem.getClientProperty("Node");
717                         selectedNode = node;
718                         freenetInterface.setNode(selectedNode);
719                 }
720         }
721
722         //
723         // INTERFACE UpdateListener
724         //
725
726         /**
727          * {@inheritDoc}
728          */
729         @Override
730         public void foundUpdateData(Version foundVersion, long versionTimestamp) {
731                 logger.log(Level.FINEST, "Found version {0} from {1,date}.", new Object[] { foundVersion, versionTimestamp });
732                 if (foundVersion.compareTo(VERSION) > 0) {
733                         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);
734                 }
735         }
736
737         //
738         // MAIN METHOD
739         //
740
741         /**
742          * Main method that is called by the VM.
743          *
744          * @param args
745          *            The command-line arguments
746          */
747         public static void main(String[] args) {
748                 /* initialize logger. */
749                 Logger logger = Logger.getLogger("de.todesbaum");
750                 Handler handler = new ConsoleHandler();
751                 logger.addHandler(handler);
752                 String configFilename = null;
753                 boolean nextIsConfigFilename = false;
754                 for (String argument : args) {
755                         if (nextIsConfigFilename) {
756                                 configFilename = argument;
757                                 nextIsConfigFilename = false;
758                         }
759                         if ("--help".equals(argument)) {
760                                 printHelp();
761                                 return;
762                         } else if ("--debug".equals(argument)) {
763                                 logger.setLevel(Level.ALL);
764                                 handler.setLevel(Level.ALL);
765                         } else if ("--config-file".equals(argument)) {
766                                 nextIsConfigFilename = true;
767                         }
768                 }
769                 if (nextIsConfigFilename) {
770                         System.out.println("--config-file needs parameter!");
771                         return;
772                 }
773                 new Main(configFilename);
774         }
775
776         /**
777          * Prints a small syntax help.
778          */
779         private static void printHelp() {
780                 System.out.println("--help\tshows this cruft");
781                 System.out.println("--debug\tenables some debug output");
782                 System.out.println("--config-file <file>\tuse specified configuration file");
783         }
784
785 }