Fix bug when JAR file is stored in path with non-US-ASCII characters
[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
51 import de.todesbaum.jsite.application.Freenet7Interface;
52 import de.todesbaum.jsite.application.Node;
53 import de.todesbaum.jsite.application.Project;
54 import de.todesbaum.jsite.application.UpdateChecker;
55 import de.todesbaum.jsite.application.UpdateListener;
56 import de.todesbaum.jsite.application.WebOfTrustInterface;
57 import de.todesbaum.jsite.application.validation.CheckReport;
58 import de.todesbaum.jsite.application.validation.Issue;
59 import de.todesbaum.jsite.application.validation.ProjectValidator;
60 import de.todesbaum.jsite.gui.NodeManagerListener;
61 import de.todesbaum.jsite.gui.NodeManagerPage;
62 import de.todesbaum.jsite.gui.PreferencesPage;
63 import de.todesbaum.jsite.gui.ProjectFilesPage;
64 import de.todesbaum.jsite.gui.ProjectInsertPage;
65 import de.todesbaum.jsite.gui.ProjectPage;
66 import de.todesbaum.jsite.i18n.I18n;
67 import de.todesbaum.jsite.i18n.I18nContainer;
68 import de.todesbaum.jsite.main.ConfigurationLocator.ConfigurationLocation;
69 import de.todesbaum.jsite.main.JarFileLocator.DefaultJarFileLocator;
70 import de.todesbaum.util.swing.TWizard;
71 import de.todesbaum.util.swing.TWizardPage;
72 import de.todesbaum.util.swing.WizardListener;
73
74 /**
75  * The main class that ties together everything.
76  *
77  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
78  */
79 public class Main implements ActionListener, ListSelectionListener, WizardListener, NodeManagerListener, UpdateListener {
80
81         /** The logger. */
82         private static final Logger logger = Logger.getLogger(Main.class.getName());
83
84         /** The version. */
85         private static final Version VERSION = new Version(0, 13);
86
87         /** The configuration. */
88         private Configuration configuration;
89
90         /** The freenet interface. */
91         private Freenet7Interface freenetInterface = new Freenet7Interface();
92
93         /** The update checker. */
94         private final UpdateChecker updateChecker;
95
96         /** The web of trust interface. */
97         private final WebOfTrustInterface webOfTrustInterface;
98
99         /** The jSite icon. */
100         private Icon jSiteIcon;
101
102         /**
103          * Enumeration for all possible pages.
104          *
105          * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
106          */
107         private static enum PageType {
108
109                 /** The node manager page. */
110                 PAGE_NODE_MANAGER,
111
112                 /** The project page. */
113                 PAGE_PROJECTS,
114
115                 /** The project files page. */
116                 PAGE_PROJECT_FILES,
117
118                 /** The project insert page. */
119                 PAGE_INSERT_PROJECT,
120
121                 /** The preferences page. */
122                 PAGE_PREFERENCES
123
124         }
125
126         /** The supported locales. */
127         private static final Locale[] SUPPORTED_LOCALES = new Locale[] {
128                         Locale.ENGLISH,
129                         Locale.GERMAN,
130                         Locale.FRENCH,
131                         Locale.ITALIAN,
132                         new Locale("pl"),
133                         new Locale("fi")
134         };
135
136         /** The actions that switch the language. */
137         private Map<Locale, Action> languageActions = new HashMap<Locale, Action>();
138
139         /** The “manage nodes” action. */
140         private Action manageNodeAction;
141
142         /** The “preferences” action. */
143         private Action optionsPreferencesAction;
144
145         /** The “check for updates” action. */
146         private Action checkForUpdatesAction;
147
148         /** The “about jSite” action. */
149         private Action aboutAction;
150
151         /** The wizard. */
152         private TWizard wizard;
153
154         /** The node menu. */
155         private JMenu nodeMenu;
156
157         /** The currently selected node. */
158         private Node selectedNode;
159
160         /** Mapping from page type to page. */
161         private final Map<PageType, TWizardPage> pages = new HashMap<PageType, TWizardPage>();
162
163         /** The original location of the configuration file. */
164         private ConfigurationLocation originalLocation;
165
166         /**
167          * Creates a new core with the default configuration file.
168          */
169         private Main() {
170                 this(null);
171         }
172
173         /**
174          * Creates a new core with the given configuration from the given file.
175          *
176          * @param configFilename
177          *            The name of the configuration file
178          */
179         private Main(String configFilename) {
180                 /* collect all possible configuration file locations. */
181                 ConfigurationLocator configurationLocator = new ConfigurationLocator(new DefaultJarFileLocator(getClass().getClassLoader()));
182                 if (configFilename != null) {
183                         configurationLocator.setCustomLocation(configFilename);
184                 }
185
186                 originalLocation = configurationLocator.findPreferredLocation();
187                 logger.log(Level.CONFIG, "Using configuration from " + originalLocation + ".");
188                 configuration = new Configuration(configurationLocator, originalLocation);
189
190                 Locale.setDefault(configuration.getLocale());
191                 I18n.setLocale(configuration.getLocale());
192                 wizard = new TWizard();
193                 createActions();
194                 wizard.setJMenuBar(createMenuBar());
195                 wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
196                 wizard.setPreviousEnabled(false);
197                 wizard.setNextEnabled(true);
198                 wizard.addWizardListener(this);
199                 jSiteIcon = IconLoader.loadIcon("/jsite-icon.png");
200                 wizard.setIcon(jSiteIcon);
201
202                 updateChecker = new UpdateChecker(freenetInterface, getVersion());
203                 updateChecker.addUpdateListener(this);
204                 updateChecker.start();
205
206                 webOfTrustInterface = new WebOfTrustInterface(freenetInterface);
207
208                 initPages();
209                 showPage(PageType.PAGE_PROJECTS);
210         }
211
212         /**
213          * Creates all actions.
214          */
215         private void createActions() {
216                 for (final Locale locale : SUPPORTED_LOCALES) {
217                         languageActions.put(locale, new AbstractAction(I18n.getMessage("jsite.menu.language." + locale.getLanguage()), IconLoader.loadIcon("/flag-" + locale.getLanguage() + ".png")) {
218
219                                 @Override
220                                 @SuppressWarnings("synthetic-access")
221                                 public void actionPerformed(ActionEvent actionEvent) {
222                                         switchLanguage(locale);
223                                 }
224                         });
225                 }
226                 manageNodeAction = new AbstractAction(I18n.getMessage("jsite.menu.nodes.manage-nodes")) {
227
228                         @Override
229                         @SuppressWarnings("synthetic-access")
230                         public void actionPerformed(ActionEvent actionEvent) {
231                                 showPage(PageType.PAGE_NODE_MANAGER);
232                                 optionsPreferencesAction.setEnabled(true);
233                                 wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
234                                 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
235                         }
236                 };
237                 optionsPreferencesAction = new AbstractAction(I18n.getMessage("jsite.menu.options.preferences")) {
238
239                         /**
240                          * {@inheritDoc}
241                          */
242                         @Override
243                         @SuppressWarnings("synthetic-access")
244                         public void actionPerformed(ActionEvent actionEvent) {
245                                 optionsPreferences();
246                         }
247                 };
248                 checkForUpdatesAction = new AbstractAction(I18n.getMessage("jsite.menu.help.check-for-updates")) {
249
250                         /**
251                          * {@inheritDoc}
252                          */
253                         @Override
254                         @SuppressWarnings("synthetic-access")
255                         public void actionPerformed(ActionEvent actionEvent) {
256                                 showLatestUpdate();
257                         }
258                 };
259                 aboutAction = new AbstractAction(I18n.getMessage("jsite.menu.help.about")) {
260
261                         @Override
262                         @SuppressWarnings("synthetic-access")
263                         public void actionPerformed(ActionEvent e) {
264                                 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.about.message"), getVersion().toString()), null, JOptionPane.INFORMATION_MESSAGE, jSiteIcon);
265                         }
266                 };
267
268                 I18nContainer.getInstance().registerRunnable(new Runnable() {
269
270                         @Override
271                         @SuppressWarnings("synthetic-access")
272                         public void run() {
273                                 manageNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.nodes.manage-nodes"));
274                                 optionsPreferencesAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.options.preferences"));
275                                 checkForUpdatesAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.help.check-for-updates"));
276                                 aboutAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.help.about"));
277                         }
278                 });
279         }
280
281         /**
282          * Creates the menu bar.
283          *
284          * @return The menu bar
285          */
286         private JMenuBar createMenuBar() {
287                 JMenuBar menuBar = new JMenuBar();
288                 final JMenu languageMenu = new JMenu(I18n.getMessage("jsite.menu.languages"));
289                 menuBar.add(languageMenu);
290                 ButtonGroup languageButtonGroup = new ButtonGroup();
291                 for (Locale locale : SUPPORTED_LOCALES) {
292                         Action languageAction = languageActions.get(locale);
293                         JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(languageActions.get(locale));
294                         if (locale.equals(Locale.getDefault())) {
295                                 menuItem.setSelected(true);
296                         }
297                         languageAction.putValue("menuItem", menuItem);
298                         languageButtonGroup.add(menuItem);
299                         languageMenu.add(menuItem);
300                 }
301                 nodeMenu = new JMenu(I18n.getMessage("jsite.menu.nodes"));
302                 menuBar.add(nodeMenu);
303                 selectedNode = configuration.getSelectedNode();
304                 nodesUpdated(configuration.getNodes());
305
306                 final JMenu optionsMenu = new JMenu(I18n.getMessage("jsite.menu.options"));
307                 menuBar.add(optionsMenu);
308                 optionsMenu.add(optionsPreferencesAction);
309
310                 /* evil hack to right-align the help menu */
311                 JPanel panel = new JPanel();
312                 panel.setOpaque(false);
313                 menuBar.add(panel);
314
315                 final JMenu helpMenu = new JMenu(I18n.getMessage("jsite.menu.help"));
316                 menuBar.add(helpMenu);
317                 helpMenu.add(checkForUpdatesAction);
318                 helpMenu.add(aboutAction);
319
320                 I18nContainer.getInstance().registerRunnable(new Runnable() {
321
322                         @Override
323                         @SuppressWarnings("synthetic-access")
324                         public void run() {
325                                 languageMenu.setText(I18n.getMessage("jsite.menu.languages"));
326                                 nodeMenu.setText(I18n.getMessage("jsite.menu.nodes"));
327                                 optionsMenu.setText(I18n.getMessage("jsite.menu.options"));
328                                 helpMenu.setText(I18n.getMessage("jsite.menu.help"));
329                                 for (Map.Entry<Locale, Action> languageActionEntry : languageActions.entrySet()) {
330                                         languageActionEntry.getValue().putValue(Action.NAME, I18n.getMessage("jsite.menu.language." + languageActionEntry.getKey().getLanguage()));
331                                 }
332                         }
333                 });
334
335                 return menuBar;
336         }
337
338         /**
339          * Initializes all pages.
340          */
341         private void initPages() {
342                 NodeManagerPage nodeManagerPage = new NodeManagerPage(wizard);
343                 nodeManagerPage.setName("page.node-manager");
344                 nodeManagerPage.addNodeManagerListener(this);
345                 nodeManagerPage.setNodes(configuration.getNodes());
346                 pages.put(PageType.PAGE_NODE_MANAGER, nodeManagerPage);
347
348                 ProjectPage projectPage = new ProjectPage(wizard);
349                 projectPage.setName("page.project");
350                 projectPage.setProjects(configuration.getProjects());
351                 projectPage.setFreenetInterface(freenetInterface);
352                 projectPage.setWebOfTrustInterface(webOfTrustInterface);
353                 projectPage.addListSelectionListener(this);
354                 pages.put(PageType.PAGE_PROJECTS, projectPage);
355
356                 ProjectFilesPage projectFilesPage = new ProjectFilesPage(wizard);
357                 projectFilesPage.setName("page.project.files");
358                 pages.put(PageType.PAGE_PROJECT_FILES, projectFilesPage);
359
360                 ProjectInsertPage projectInsertPage = new ProjectInsertPage(wizard);
361                 projectInsertPage.setName("page.project.insert");
362                 projectInsertPage.setFreenetInterface(freenetInterface);
363                 pages.put(PageType.PAGE_INSERT_PROJECT, projectInsertPage);
364
365                 PreferencesPage preferencesPage = new PreferencesPage(wizard);
366                 preferencesPage.setName("page.preferences");
367                 preferencesPage.setTempDirectory(configuration.getTempDirectory());
368                 pages.put(PageType.PAGE_PREFERENCES, preferencesPage);
369         }
370
371         /**
372          * Shows the page with the given type.
373          *
374          * @param pageType
375          *            The page type to show
376          */
377         private void showPage(PageType pageType) {
378                 wizard.setPreviousEnabled(pageType.ordinal() > 0);
379                 wizard.setNextEnabled(pageType.ordinal() < (pages.size() - 1));
380                 wizard.setPage(pages.get(pageType));
381                 wizard.setTitle(pages.get(pageType).getHeading() + " - jSite");
382         }
383
384         /**
385          * Returns whether a configuration file would be overwritten when calling
386          * {@link #saveConfiguration()}.
387          *
388          * @return {@code true} if {@link #saveConfiguration()} would overwrite an
389          *         existing file, {@code false} otherwise
390          */
391         private boolean isOverwritingConfiguration() {
392                 return configuration.getConfigurationLocator().hasFile(configuration.getConfigurationDirectory());
393         }
394
395         /**
396          * Saves the configuration.
397          *
398          * @return <code>true</code> if the configuration could be saved,
399          *         <code>false</code> otherwise
400          */
401         private boolean saveConfiguration() {
402                 NodeManagerPage nodeManagerPage = (NodeManagerPage) pages.get(PageType.PAGE_NODE_MANAGER);
403                 configuration.setNodes(nodeManagerPage.getNodes());
404                 if (selectedNode != null) {
405                         configuration.setSelectedNode(selectedNode);
406                 }
407
408                 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
409                 configuration.setProjects(projectPage.getProjects());
410
411                 PreferencesPage preferencesPage = (PreferencesPage) pages.get(PageType.PAGE_PREFERENCES);
412                 configuration.setTempDirectory(preferencesPage.getTempDirectory());
413
414                 return configuration.save();
415         }
416
417         /**
418          * Finds a supported locale for the given locale.
419          *
420          * @param forLocale
421          *            The locale to find a supported locale for
422          * @return The supported locale that was found, or the default locale if no
423          *         supported locale could be found
424          */
425         private static Locale findSupportedLocale(Locale forLocale) {
426                 for (Locale locale : SUPPORTED_LOCALES) {
427                         if (locale.equals(forLocale)) {
428                                 return locale;
429                         }
430                 }
431                 for (Locale locale : SUPPORTED_LOCALES) {
432                         if (locale.getCountry().equals(forLocale.getCountry()) && locale.getLanguage().equals(forLocale.getLanguage())) {
433                                 return locale;
434                         }
435                 }
436                 for (Locale locale : SUPPORTED_LOCALES) {
437                         if (locale.getLanguage().equals(forLocale.getLanguage())) {
438                                 return locale;
439                         }
440                 }
441                 return SUPPORTED_LOCALES[0];
442         }
443
444         /**
445          * Returns the version.
446          *
447          * @return The version
448          */
449         public static final Version getVersion() {
450                 return VERSION;
451         }
452
453         //
454         // ACTIONS
455         //
456
457         /**
458          * Switches the language of the interface to the given locale.
459          *
460          * @param locale
461          *            The locale to switch to
462          */
463         private void switchLanguage(Locale locale) {
464                 Locale supportedLocale = findSupportedLocale(locale);
465                 Action languageAction = languageActions.get(supportedLocale);
466                 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) languageAction.getValue("menuItem");
467                 menuItem.setSelected(true);
468                 I18n.setLocale(supportedLocale);
469                 for (Runnable i18nRunnable : I18nContainer.getInstance()) {
470                         try {
471                                 i18nRunnable.run();
472                         } catch (Throwable t) {
473                                 /* we probably shouldn't swallow this. */
474                         }
475                 }
476                 wizard.setPage(wizard.getPage());
477                 configuration.setLocale(supportedLocale);
478         }
479
480         /**
481          * Shows a dialog with general preferences.
482          */
483         private void optionsPreferences() {
484                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setConfigurationLocation(configuration.getConfigurationDirectory());
485                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setHasNextToJarConfiguration(configuration.getConfigurationLocator().isValidLocation(ConfigurationLocation.NEXT_TO_JAR_FILE));
486                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setHasCustomConfiguration(configuration.getConfigurationLocator().isValidLocation(ConfigurationLocation.CUSTOM));
487                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setUseEarlyEncode(configuration.useEarlyEncode());
488                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setPriority(configuration.getPriority());
489                 showPage(PageType.PAGE_PREFERENCES);
490                 optionsPreferencesAction.setEnabled(false);
491                 wizard.setNextEnabled(true);
492                 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
493         }
494
495         /**
496          * Shows a dialog box that shows the last version that was found by the
497          * {@link UpdateChecker}.
498          */
499         private void showLatestUpdate() {
500                 Version latestVersion = updateChecker.getLatestVersion();
501                 int versionDifference = latestVersion.compareTo(VERSION);
502                 if (versionDifference > 0) {
503                         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);
504                 } else if (versionDifference < 0) {
505                         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);
506                 } else {
507                         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);
508                 }
509         }
510
511         /**
512          * Quits jSite, stopping all background services.
513          */
514         private void quit() {
515                 updateChecker.stop();
516                 System.exit(0);
517         }
518
519         //
520         // INTERFACE ListSelectionListener
521         //
522
523         /**
524          * {@inheritDoc}
525          */
526         @Override
527         public void valueChanged(ListSelectionEvent e) {
528                 JList list = (JList) e.getSource();
529                 int selectedRow = list.getSelectedIndex();
530                 wizard.setNextEnabled(selectedRow > -1);
531         }
532
533         //
534         // INTERFACE WizardListener
535         //
536
537         /**
538          * {@inheritDoc}
539          */
540         @Override
541         public void wizardNextPressed(TWizard wizard) {
542                 String pageName = wizard.getPage().getName();
543                 if ("page.node-manager".equals(pageName)) {
544                         showPage(PageType.PAGE_PROJECTS);
545                 } else if ("page.project".equals(pageName)) {
546                         ProjectPage projectPage = (ProjectPage) wizard.getPage();
547                         Project project = projectPage.getSelectedProject();
548                         if ((project.getLocalPath() == null) || (project.getLocalPath().trim().length() == 0)) {
549                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.warning.no-local-path"), null, JOptionPane.ERROR_MESSAGE);
550                                 return;
551                         }
552                         if ((project.getPath() == null) || (project.getPath().trim().length() == 0)) {
553                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.warning.no-path"), null, JOptionPane.ERROR_MESSAGE);
554                                 return;
555                         }
556                         ((ProjectFilesPage) pages.get(PageType.PAGE_PROJECT_FILES)).setProject(project);
557                         ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).setProject(project);
558                         showPage(PageType.PAGE_PROJECT_FILES);
559                 } else if ("page.project.files".equals(pageName)) {
560                         ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
561                         Project project = projectPage.getSelectedProject();
562                         if (selectedNode == null) {
563                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.error.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
564                                 return;
565                         }
566                         CheckReport checkReport = ProjectValidator.validateProject(project);
567                         for (Issue issue : checkReport) {
568                                 if (issue.isFatal()) {
569                                         JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite." + issue.getErrorKey()), (Object[]) issue.getParameters()), null, JOptionPane.ERROR_MESSAGE);
570                                         return;
571                                 }
572                                 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) {
573                                         return;
574                                 }
575                         }
576                         boolean nodeRunning = false;
577                         try {
578                                 nodeRunning = freenetInterface.isNodePresent();
579                         } catch (IOException e) {
580                                 /* ignore. */
581                         }
582                         if (!nodeRunning) {
583                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.error.no-node-running"), null, JOptionPane.ERROR_MESSAGE);
584                                 return;
585                         }
586                         configuration.save();
587                         showPage(PageType.PAGE_INSERT_PROJECT);
588                         ProjectInsertPage projectInsertPage = (ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT);
589                         String tempDirectory = ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).getTempDirectory();
590                         projectInsertPage.setTempDirectory(tempDirectory);
591                         projectInsertPage.setUseEarlyEncode(configuration.useEarlyEncode());
592                         projectInsertPage.setPriority(configuration.getPriority());
593                         projectInsertPage.startInsert();
594                         nodeMenu.setEnabled(false);
595                         optionsPreferencesAction.setEnabled(false);
596                 } else if ("page.project.insert".equals(pageName)) {
597                         ProjectInsertPage projectInsertPage = (ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT);
598                         if (projectInsertPage.isRunning()) {
599                                 projectInsertPage.stopInsert();
600                         } else {
601                                 showPage(PageType.PAGE_PROJECTS);
602                                 nodeMenu.setEnabled(true);
603                                 optionsPreferencesAction.setEnabled(true);
604                         }
605                 } else if ("page.preferences".equals(pageName)) {
606                         PreferencesPage preferencesPage = (PreferencesPage) pages.get(PageType.PAGE_PREFERENCES);
607                         showPage(PageType.PAGE_PROJECTS);
608                         optionsPreferencesAction.setEnabled(true);
609                         configuration.setUseEarlyEncode(preferencesPage.useEarlyEncode());
610                         configuration.setPriority(preferencesPage.getPriority());
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 }