Log the latest found version with date.
[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.event.ActionEvent;
23 import java.awt.event.ActionListener;
24 import java.io.File;
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.Set;
32 import java.util.Map.Entry;
33 import java.util.logging.ConsoleHandler;
34 import java.util.logging.Handler;
35 import java.util.logging.Level;
36 import java.util.logging.Logger;
37
38 import javax.swing.AbstractAction;
39 import javax.swing.Action;
40 import javax.swing.ButtonGroup;
41 import javax.swing.Icon;
42 import javax.swing.JList;
43 import javax.swing.JMenu;
44 import javax.swing.JMenuBar;
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.FileOption;
52 import de.todesbaum.jsite.application.Freenet7Interface;
53 import de.todesbaum.jsite.application.Node;
54 import de.todesbaum.jsite.application.Project;
55 import de.todesbaum.jsite.application.UpdateChecker;
56 import de.todesbaum.jsite.application.UpdateListener;
57 import de.todesbaum.jsite.gui.NodeManagerListener;
58 import de.todesbaum.jsite.gui.NodeManagerPage;
59 import de.todesbaum.jsite.gui.PreferencesPage;
60 import de.todesbaum.jsite.gui.ProjectFilesPage;
61 import de.todesbaum.jsite.gui.ProjectInsertPage;
62 import de.todesbaum.jsite.gui.ProjectPage;
63 import de.todesbaum.jsite.i18n.I18n;
64 import de.todesbaum.jsite.i18n.I18nContainer;
65 import de.todesbaum.util.image.IconLoader;
66 import de.todesbaum.util.swing.TWizard;
67 import de.todesbaum.util.swing.TWizardPage;
68 import de.todesbaum.util.swing.WizardListener;
69
70 /**
71  * The main class that ties together everything.
72  *
73  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
74  */
75 public class Main implements ActionListener, ListSelectionListener, WizardListener, NodeManagerListener, UpdateListener {
76
77         /** The logger. */
78         private static final Logger logger = Logger.getLogger(Main.class.getName());
79
80         /** The version. */
81         private static final Version VERSION = new Version(0, 8);
82
83         /** The configuration. */
84         private Configuration configuration;
85
86         /** The freenet interface. */
87         private Freenet7Interface freenetInterface = new Freenet7Interface();
88
89         /** The update checker. */
90         private final UpdateChecker updateChecker;
91
92         /** The jSite icon. */
93         private Icon jSiteIcon;
94
95         /**
96          * Enumeration for all possible pages.
97          *
98          * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
99          */
100         private static enum PageType {
101
102                 /** The node manager page. */
103                 PAGE_NODE_MANAGER,
104
105                 /** The project page. */
106                 PAGE_PROJECTS,
107
108                 /** The project files page. */
109                 PAGE_PROJECT_FILES,
110
111                 /** The project insert page. */
112                 PAGE_INSERT_PROJECT,
113
114                 /** The preferences page. */
115                 PAGE_PREFERENCES
116
117         }
118
119         /** The supported locales. */
120         private static final Locale[] SUPPORTED_LOCALES = new Locale[] { Locale.ENGLISH, Locale.GERMAN, Locale.FRENCH, Locale.ITALIAN, new Locale("pl") };
121
122         /** The actions that switch the language. */
123         private Map<Locale, Action> languageActions = new HashMap<Locale, Action>();
124
125         /** The “manage nodes” action. */
126         private Action manageNodeAction;
127
128         /** The “preferences” action. */
129         private Action optionsPreferencesAction;
130
131         /** The “check for updates” action. */
132         private Action checkForUpdatesAction;
133
134         /** The “about jSite” action. */
135         private Action aboutAction;
136
137         /** The wizard. */
138         private TWizard wizard;
139
140         /** The node menu. */
141         private JMenu nodeMenu;
142
143         /** The currently selected node. */
144         private Node selectedNode;
145
146         /** Mapping from page type to page. */
147         private final Map<PageType, TWizardPage> pages = new HashMap<PageType, TWizardPage>();
148
149         /**
150          * Creates a new core with the default configuration file.
151          */
152         private Main() {
153                 this(null);
154         }
155
156         /**
157          * Creates a new core with the given configuration from the given file.
158          *
159          * @param configFilename
160          *            The name of the configuration file
161          */
162         private Main(String configFilename) {
163                 if (configFilename != null) {
164                         configuration = new Configuration(configFilename);
165                 } else {
166                         configuration = new Configuration();
167                 }
168                 Locale.setDefault(configuration.getLocale());
169                 I18n.setLocale(configuration.getLocale());
170                 if (!configuration.createLockFile()) {
171                         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"));
172                         if (option != 0) {
173                                 throw new IllegalStateException("Lockfile override not active, refusing start.");
174                         }
175                         configuration.removeLockfileOnExit();
176                 }
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                                 wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
214                                 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
215                         }
216                 };
217                 optionsPreferencesAction = new AbstractAction(I18n.getMessage("jsite.menu.options.preferences")) {
218
219                         /**
220                          * {@inheritDoc}
221                          */
222                         @Override
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          * Saves the configuration.
361          *
362          * @return <code>true</code> if the configuration could be saved,
363          *         <code>false</code> otherwise
364          */
365         private boolean saveConfiguration() {
366                 NodeManagerPage nodeManagerPage = (NodeManagerPage) pages.get(PageType.PAGE_NODE_MANAGER);
367                 configuration.setNodes(nodeManagerPage.getNodes());
368                 if (selectedNode != null) {
369                         configuration.setSelectedNode(selectedNode);
370                 }
371
372                 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
373                 configuration.setProjects(projectPage.getProjects());
374
375                 PreferencesPage preferencesPage = (PreferencesPage) pages.get(PageType.PAGE_PREFERENCES);
376                 configuration.setTempDirectory(preferencesPage.getTempDirectory());
377
378                 return configuration.save();
379         }
380
381         /**
382          * Finds a supported locale for the given locale.
383          *
384          * @param forLocale
385          *            The locale to find a supported locale for
386          * @return The supported locale that was found, or the default locale if no
387          *         supported locale could be found
388          */
389         private Locale findSupportedLocale(Locale forLocale) {
390                 for (Locale locale : SUPPORTED_LOCALES) {
391                         if (locale.equals(forLocale)) {
392                                 return locale;
393                         }
394                 }
395                 for (Locale locale : SUPPORTED_LOCALES) {
396                         if (locale.getCountry().equals(forLocale.getCountry()) && locale.getLanguage().equals(forLocale.getLanguage())) {
397                                 return locale;
398                         }
399                 }
400                 for (Locale locale : SUPPORTED_LOCALES) {
401                         if (locale.getLanguage().equals(forLocale.getLanguage())) {
402                                 return locale;
403                         }
404                 }
405                 return SUPPORTED_LOCALES[0];
406         }
407
408         /**
409          * Returns the version.
410          *
411          * @return The version
412          */
413         public static final Version getVersion() {
414                 return VERSION;
415         }
416
417         //
418         // ACTIONS
419         //
420
421         /**
422          * Switches the language of the interface to the given locale.
423          *
424          * @param locale
425          *            The locale to switch to
426          */
427         private void switchLanguage(Locale locale) {
428                 Locale supportedLocale = findSupportedLocale(locale);
429                 Action languageAction = languageActions.get(supportedLocale);
430                 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) languageAction.getValue("menuItem");
431                 menuItem.setSelected(true);
432                 I18n.setLocale(supportedLocale);
433                 for (Runnable i18nRunnable : I18nContainer.getInstance()) {
434                         try {
435                                 i18nRunnable.run();
436                         } catch (Throwable t) {
437                                 /* we probably shouldn't swallow this. */
438                         }
439                 }
440                 wizard.setPage(wizard.getPage());
441                 configuration.setLocale(supportedLocale);
442         }
443
444         /**
445          * Shows a dialog with general preferences.
446          */
447         private void optionsPreferences() {
448                 showPage(PageType.PAGE_PREFERENCES);
449                 optionsPreferencesAction.setEnabled(false);
450                 wizard.setNextEnabled(true);
451                 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
452         }
453
454         /**
455          * Shows a dialog box that shows the last version that was found by the
456          * {@link UpdateChecker}.
457          */
458         private void showLatestUpdate() {
459                 Version latestVersion = updateChecker.getLatestVersion();
460                 int versionDifference = latestVersion.compareTo(VERSION);
461                 if (versionDifference > 0) {
462                         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);
463                 } else if (versionDifference < 0) {
464                         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);
465                 } else {
466                         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);
467                 }
468         }
469
470         //
471         // INTERFACE ListSelectionListener
472         //
473
474         /**
475          * {@inheritDoc}
476          */
477         public void valueChanged(ListSelectionEvent e) {
478                 JList list = (JList) e.getSource();
479                 int selectedRow = list.getSelectedIndex();
480                 wizard.setNextEnabled(selectedRow > -1);
481         }
482
483         //
484         // INTERFACE WizardListener
485         //
486
487         /**
488          * {@inheritDoc}
489          */
490         public void wizardNextPressed(TWizard wizard) {
491                 String pageName = wizard.getPage().getName();
492                 if ("page.node-manager".equals(pageName)) {
493                         showPage(PageType.PAGE_PROJECTS);
494                 } else if ("page.project".equals(pageName)) {
495                         ProjectPage projectPage = (ProjectPage) wizard.getPage();
496                         Project project = projectPage.getSelectedProject();
497                         if ((project.getLocalPath() == null) || (project.getLocalPath().trim().length() == 0)) {
498                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-local-path"), null, JOptionPane.ERROR_MESSAGE);
499                                 return;
500                         }
501                         if ((project.getPath() == null) || (project.getPath().trim().length() == 0)) {
502                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-path"), null, JOptionPane.ERROR_MESSAGE);
503                                 return;
504                         }
505                         ((ProjectFilesPage) pages.get(PageType.PAGE_PROJECT_FILES)).setProject(project);
506                         ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).setProject(project);
507                         showPage(PageType.PAGE_PROJECT_FILES);
508                 } else if ("page.project.files".equals(pageName)) {
509                         ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
510                         Project project = projectPage.getSelectedProject();
511                         if (selectedNode == null) {
512                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
513                                 return;
514                         }
515                         if ((project.getIndexFile() == null) || (project.getIndexFile().length() == 0)) {
516                                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.empty-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
517                                         return;
518                                 }
519                         } else {
520                                 File indexFile = new File(project.getLocalPath(), project.getIndexFile());
521                                 if (!indexFile.exists()) {
522                                         JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.index-missing"), null, JOptionPane.ERROR_MESSAGE);
523                                         return;
524                                 }
525                         }
526                         String indexFile = project.getIndexFile();
527                         boolean hasIndexFile = (indexFile != null);
528                         if (hasIndexFile && !project.getFileOption(indexFile).getContainer().equals("")) {
529                                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.container-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
530                                         return;
531                                 }
532                         }
533                         if (hasIndexFile && !project.getFileOption(indexFile).getMimeType().equals("text/html")) {
534                                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.index-not-html"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
535                                         return;
536                                 }
537                         }
538                         Map<String, FileOption> fileOptions = project.getFileOptions();
539                         Set<Entry<String, FileOption>> fileOptionEntries = fileOptions.entrySet();
540                         for (Entry<String, FileOption> fileOptionEntry : fileOptionEntries) {
541                                 FileOption fileOption = fileOptionEntry.getValue();
542                                 if (!fileOption.isInsert() && ((fileOption.getCustomKey().length() == 0) || "CHK@".equals(fileOption.getCustomKey()))) {
543                                         JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.project-files.no-custom-key"), fileOptionEntry.getKey()), null, JOptionPane.ERROR_MESSAGE);
544                                         return;
545                                 }
546                         }
547                         boolean nodeRunning = false;
548                         try {
549                                 nodeRunning = freenetInterface.isNodePresent();
550                         } catch (IOException e) {
551                                 /* ignore. */
552                         }
553                         if (!nodeRunning) {
554                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-running"), null, JOptionPane.ERROR_MESSAGE);
555                                 return;
556                         }
557                         configuration.save();
558                         showPage(PageType.PAGE_INSERT_PROJECT);
559                         ProjectInsertPage projectInsertPage = (ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT);
560                         String tempDirectory = ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).getTempDirectory();
561                         projectInsertPage.setTempDirectory(tempDirectory);
562                         projectInsertPage.startInsert();
563                         nodeMenu.setEnabled(false);
564                         optionsPreferencesAction.setEnabled(false);
565                 } else if ("page.project.insert".equals(pageName)) {
566                         showPage(PageType.PAGE_PROJECTS);
567                         nodeMenu.setEnabled(true);
568                         optionsPreferencesAction.setEnabled(true);
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)) {
581                         showPage(PageType.PAGE_NODE_MANAGER);
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 (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.question"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
594                         if (saveConfiguration()) {
595                                 System.exit(0);
596                         }
597                         if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.config-not-saved"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
598                                 System.exit(0);
599                         }
600                 }
601         }
602
603         //
604         // INTERFACE NodeManagerListener
605         //
606
607         /**
608          * {@inheritDoc}
609          */
610         public void nodesUpdated(Node[] nodes) {
611                 nodeMenu.removeAll();
612                 ButtonGroup nodeButtonGroup = new ButtonGroup();
613                 Node newSelectedNode = null;
614                 for (Node node : nodes) {
615                         JRadioButtonMenuItem nodeMenuItem = new JRadioButtonMenuItem(node.getName());
616                         nodeMenuItem.putClientProperty("Node", node);
617                         nodeMenuItem.addActionListener(this);
618                         nodeButtonGroup.add(nodeMenuItem);
619                         if (node.equals(selectedNode)) {
620                                 newSelectedNode = node;
621                                 nodeMenuItem.setSelected(true);
622                         }
623                         nodeMenu.add(nodeMenuItem);
624                 }
625                 nodeMenu.addSeparator();
626                 nodeMenu.add(manageNodeAction);
627                 selectedNode = newSelectedNode;
628                 freenetInterface.setNode(selectedNode);
629         }
630
631         /**
632          * {@inheritDoc}
633          */
634         public void actionPerformed(ActionEvent e) {
635                 Object source = e.getSource();
636                 if (source instanceof JRadioButtonMenuItem) {
637                         JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) source;
638                         Node node = (Node) menuItem.getClientProperty("Node");
639                         selectedNode = node;
640                         freenetInterface.setNode(selectedNode);
641                 }
642         }
643
644         //
645         // INTERFACE UpdateListener
646         //
647
648         /**
649          * {@inheritDoc}
650          */
651         public void foundUpdateData(Version foundVersion, long versionTimestamp) {
652                 logger.log(Level.FINEST, "Found version {0} from {1,date}.", new Object[] { foundVersion, versionTimestamp });
653                 if (foundVersion.compareTo(VERSION) > 0) {
654                         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);
655                 }
656         }
657
658         //
659         // MAIN METHOD
660         //
661
662         /**
663          * Main method that is called by the VM.
664          *
665          * @param args
666          *            The command-line arguments
667          */
668         public static void main(String[] args) {
669                 /* initialize logger. */
670                 Logger logger = Logger.getLogger("de.todesbaum");
671                 Handler handler = new ConsoleHandler();
672                 logger.addHandler(handler);
673                 String configFilename = null;
674                 boolean nextIsConfigFilename = false;
675                 for (String argument : args) {
676                         if (nextIsConfigFilename) {
677                                 configFilename = argument;
678                                 nextIsConfigFilename = false;
679                         }
680                         if ("--help".equals(argument)) {
681                                 printHelp();
682                                 return;
683                         } else if ("--debug".equals(argument)) {
684                                 logger.setLevel(Level.ALL);
685                                 handler.setLevel(Level.ALL);
686                         } else if ("--config-file".equals(argument)) {
687                                 nextIsConfigFilename = true;
688                         }
689                 }
690                 if (nextIsConfigFilename) {
691                         System.out.println("--config-file needs parameter!");
692                         return;
693                 }
694                 new Main(configFilename);
695         }
696
697         /**
698          * Prints a small syntax help.
699          */
700         private static void printHelp() {
701                 System.out.println("--help\tshows this cruft");
702                 System.out.println("--debug\tenables some debug output");
703                 System.out.println("--config-file <file>\tuse specified configuration file");
704         }
705
706 }