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