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