f59e29c4a3f28b1ce4027479d6db66ac963cee0d
[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.Arrays;
28 import java.util.Date;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Locale;
32 import java.util.Map;
33 import java.util.Set;
34 import java.util.Map.Entry;
35 import java.util.logging.ConsoleHandler;
36 import java.util.logging.Handler;
37 import java.util.logging.Level;
38 import java.util.logging.Logger;
39
40 import javax.swing.AbstractAction;
41 import javax.swing.Action;
42 import javax.swing.ButtonGroup;
43 import javax.swing.Icon;
44 import javax.swing.JList;
45 import javax.swing.JMenu;
46 import javax.swing.JMenuBar;
47 import javax.swing.JOptionPane;
48 import javax.swing.JPanel;
49 import javax.swing.JRadioButtonMenuItem;
50 import javax.swing.event.ListSelectionEvent;
51 import javax.swing.event.ListSelectionListener;
52
53 import de.todesbaum.jsite.application.FileOption;
54 import de.todesbaum.jsite.application.Freenet7Interface;
55 import de.todesbaum.jsite.application.Node;
56 import de.todesbaum.jsite.application.Project;
57 import de.todesbaum.jsite.application.UpdateChecker;
58 import de.todesbaum.jsite.application.UpdateListener;
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.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, 8);
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, Locale.ITALIAN, new Locale("pl") };
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                 if (configFilename != null) {
166                         configuration = new Configuration(configFilename);
167                 } else {
168                         configuration = new Configuration();
169                 }
170                 Locale.setDefault(configuration.getLocale());
171                 I18n.setLocale(configuration.getLocale());
172                 if (!configuration.createLockFile()) {
173                         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"));
174                         if (option != 0) {
175                                 throw new IllegalStateException("Lockfile override not active, refusing start.");
176                         }
177                         configuration.removeLockfileOnExit();
178                 }
179                 wizard = new TWizard();
180                 createActions();
181                 wizard.setJMenuBar(createMenuBar());
182                 wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
183                 wizard.setPreviousEnabled(false);
184                 wizard.setNextEnabled(true);
185                 wizard.addWizardListener(this);
186                 jSiteIcon = IconLoader.loadIcon("/jsite-icon.png");
187                 wizard.setIcon(jSiteIcon);
188
189                 updateChecker = new UpdateChecker(freenetInterface);
190                 updateChecker.addUpdateListener(this);
191                 updateChecker.start();
192
193                 initPages();
194                 showPage(PageType.PAGE_PROJECTS);
195         }
196
197         /**
198          * Creates all actions.
199          */
200         private void createActions() {
201                 for (final Locale locale : SUPPORTED_LOCALES) {
202                         languageActions.put(locale, new AbstractAction(I18n.getMessage("jsite.menu.language." + locale.getLanguage()), IconLoader.loadIcon("/flag-" + locale.getLanguage() + ".png")) {
203
204                                 @SuppressWarnings("synthetic-access")
205                                 public void actionPerformed(ActionEvent actionEvent) {
206                                         switchLanguage(locale);
207                                 }
208                         });
209                 }
210                 manageNodeAction = new AbstractAction(I18n.getMessage("jsite.menu.nodes.manage-nodes")) {
211
212                         @SuppressWarnings("synthetic-access")
213                         public void actionPerformed(ActionEvent actionEvent) {
214                                 showPage(PageType.PAGE_NODE_MANAGER);
215                                 wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
216                                 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
217                         }
218                 };
219                 optionsPreferencesAction = new AbstractAction(I18n.getMessage("jsite.menu.options.preferences")) {
220
221                         /**
222                          * {@inheritDoc}
223                          */
224                         @Override
225                         @SuppressWarnings("synthetic-access")
226                         public void actionPerformed(ActionEvent actionEvent) {
227                                 optionsPreferences();
228                         }
229                 };
230                 checkForUpdatesAction = new AbstractAction(I18n.getMessage("jsite.menu.help.check-for-updates")) {
231
232                         /**
233                          * {@inheritDoc}
234                          */
235                         @SuppressWarnings("synthetic-access")
236                         public void actionPerformed(ActionEvent actionEvent) {
237                                 showLatestUpdate();
238                         }
239                 };
240                 aboutAction = new AbstractAction(I18n.getMessage("jsite.menu.help.about")) {
241
242                         @SuppressWarnings("synthetic-access")
243                         public void actionPerformed(ActionEvent e) {
244                                 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.about.message"), getVersion().toString()), null, JOptionPane.INFORMATION_MESSAGE, jSiteIcon);
245                         }
246                 };
247
248                 I18nContainer.getInstance().registerRunnable(new Runnable() {
249
250                         @SuppressWarnings("synthetic-access")
251                         public void run() {
252                                 manageNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.nodes.manage-nodes"));
253                                 optionsPreferencesAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.options.preferences"));
254                                 checkForUpdatesAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.help.check-for-updates"));
255                                 aboutAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.help.about"));
256                         }
257                 });
258         }
259
260         /**
261          * Creates the menu bar.
262          *
263          * @return The menu bar
264          */
265         private JMenuBar createMenuBar() {
266                 JMenuBar menuBar = new JMenuBar();
267                 final JMenu languageMenu = new JMenu(I18n.getMessage("jsite.menu.languages"));
268                 menuBar.add(languageMenu);
269                 ButtonGroup languageButtonGroup = new ButtonGroup();
270                 for (Locale locale : SUPPORTED_LOCALES) {
271                         Action languageAction = languageActions.get(locale);
272                         JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(languageActions.get(locale));
273                         if (locale.equals(Locale.getDefault())) {
274                                 menuItem.setSelected(true);
275                         }
276                         languageAction.putValue("menuItem", menuItem);
277                         languageButtonGroup.add(menuItem);
278                         languageMenu.add(menuItem);
279                 }
280                 nodeMenu = new JMenu(I18n.getMessage("jsite.menu.nodes"));
281                 menuBar.add(nodeMenu);
282                 selectedNode = configuration.getSelectedNode();
283                 nodesUpdated(configuration.getNodes());
284
285                 final JMenu optionsMenu = new JMenu(I18n.getMessage("jsite.menu.options"));
286                 menuBar.add(optionsMenu);
287                 optionsMenu.add(optionsPreferencesAction);
288
289                 /* evil hack to right-align the help menu */
290                 JPanel panel = new JPanel();
291                 panel.setOpaque(false);
292                 menuBar.add(panel);
293
294                 final JMenu helpMenu = new JMenu(I18n.getMessage("jsite.menu.help"));
295                 menuBar.add(helpMenu);
296                 helpMenu.add(checkForUpdatesAction);
297                 helpMenu.add(aboutAction);
298
299                 I18nContainer.getInstance().registerRunnable(new Runnable() {
300
301                         @SuppressWarnings("synthetic-access")
302                         public void run() {
303                                 languageMenu.setText(I18n.getMessage("jsite.menu.languages"));
304                                 nodeMenu.setText(I18n.getMessage("jsite.menu.nodes"));
305                                 optionsMenu.setText(I18n.getMessage("jsite.menu.options"));
306                                 helpMenu.setText(I18n.getMessage("jsite.menu.help"));
307                                 for (Map.Entry<Locale, Action> languageActionEntry : languageActions.entrySet()) {
308                                         languageActionEntry.getValue().putValue(Action.NAME, I18n.getMessage("jsite.menu.language." + languageActionEntry.getKey().getLanguage()));
309                                 }
310                         }
311                 });
312
313                 return menuBar;
314         }
315
316         /**
317          * Initializes all pages.
318          */
319         private void initPages() {
320                 NodeManagerPage nodeManagerPage = new NodeManagerPage(wizard);
321                 nodeManagerPage.setName("page.node-manager");
322                 nodeManagerPage.addNodeManagerListener(this);
323                 nodeManagerPage.setNodes(configuration.getNodes());
324                 pages.put(PageType.PAGE_NODE_MANAGER, nodeManagerPage);
325
326                 ProjectPage projectPage = new ProjectPage(wizard);
327                 projectPage.setName("page.project");
328                 projectPage.setProjects(configuration.getProjects());
329                 projectPage.setFreenetInterface(freenetInterface);
330                 projectPage.addListSelectionListener(this);
331                 pages.put(PageType.PAGE_PROJECTS, projectPage);
332
333                 ProjectFilesPage projectFilesPage = new ProjectFilesPage(wizard);
334                 projectFilesPage.setName("page.project.files");
335                 pages.put(PageType.PAGE_PROJECT_FILES, projectFilesPage);
336
337                 ProjectInsertPage projectInsertPage = new ProjectInsertPage(wizard);
338                 projectInsertPage.setName("page.project.insert");
339                 projectInsertPage.setFreenetInterface(freenetInterface);
340                 pages.put(PageType.PAGE_INSERT_PROJECT, projectInsertPage);
341
342                 PreferencesPage preferencesPage = new PreferencesPage(wizard);
343                 preferencesPage.setName("page.preferences");
344                 preferencesPage.setTempDirectory(configuration.getTempDirectory());
345                 pages.put(PageType.PAGE_PREFERENCES, preferencesPage);
346         }
347
348         /**
349          * Shows the page with the given type.
350          *
351          * @param pageType
352          *            The page type to show
353          */
354         private void showPage(PageType pageType) {
355                 wizard.setPreviousEnabled(pageType.ordinal() > 0);
356                 wizard.setNextEnabled(pageType.ordinal() < (pages.size() - 1));
357                 wizard.setPage(pages.get(pageType));
358                 wizard.setTitle(pages.get(pageType).getHeading() + " - jSite");
359         }
360
361         /**
362          * Saves the configuration.
363          *
364          * @return <code>true</code> if the configuration could be saved,
365          *         <code>false</code> otherwise
366          */
367         private boolean saveConfiguration() {
368                 NodeManagerPage nodeManagerPage = (NodeManagerPage) pages.get(PageType.PAGE_NODE_MANAGER);
369                 configuration.setNodes(nodeManagerPage.getNodes());
370                 if (selectedNode != null) {
371                         configuration.setSelectedNode(selectedNode);
372                 }
373
374                 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
375                 configuration.setProjects(projectPage.getProjects());
376
377                 PreferencesPage preferencesPage = (PreferencesPage) pages.get(PageType.PAGE_PREFERENCES);
378                 configuration.setTempDirectory(preferencesPage.getTempDirectory());
379
380                 return configuration.save();
381         }
382
383         /**
384          * Finds a supported locale for the given locale.
385          *
386          * @param forLocale
387          *            The locale to find a supported locale for
388          * @return The supported locale that was found, or the default locale if no
389          *         supported locale could be found
390          */
391         private Locale findSupportedLocale(Locale forLocale) {
392                 for (Locale locale : SUPPORTED_LOCALES) {
393                         if (locale.equals(forLocale)) {
394                                 return locale;
395                         }
396                 }
397                 for (Locale locale : SUPPORTED_LOCALES) {
398                         if (locale.getCountry().equals(forLocale.getCountry()) && locale.getLanguage().equals(forLocale.getLanguage())) {
399                                 return locale;
400                         }
401                 }
402                 for (Locale locale : SUPPORTED_LOCALES) {
403                         if (locale.getLanguage().equals(forLocale.getLanguage())) {
404                                 return locale;
405                         }
406                 }
407                 return SUPPORTED_LOCALES[0];
408         }
409
410         /**
411          * Returns the version.
412          *
413          * @return The version
414          */
415         public static final Version getVersion() {
416                 return VERSION;
417         }
418
419         //
420         // ACTIONS
421         //
422
423         /**
424          * Switches the language of the interface to the given locale.
425          *
426          * @param locale
427          *            The locale to switch to
428          */
429         private void switchLanguage(Locale locale) {
430                 Locale supportedLocale = findSupportedLocale(locale);
431                 Action languageAction = languageActions.get(supportedLocale);
432                 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) languageAction.getValue("menuItem");
433                 menuItem.setSelected(true);
434                 I18n.setLocale(supportedLocale);
435                 for (Runnable i18nRunnable : I18nContainer.getInstance()) {
436                         try {
437                                 i18nRunnable.run();
438                         } catch (Throwable t) {
439                                 /* we probably shouldn't swallow this. */
440                         }
441                 }
442                 wizard.setPage(wizard.getPage());
443                 configuration.setLocale(supportedLocale);
444         }
445
446         /**
447          * Shows a dialog with general preferences.
448          */
449         private void optionsPreferences() {
450                 showPage(PageType.PAGE_PREFERENCES);
451                 optionsPreferencesAction.setEnabled(false);
452                 wizard.setNextEnabled(true);
453                 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
454         }
455
456         /**
457          * Shows a dialog box that shows the last version that was found by the
458          * {@link UpdateChecker}.
459          */
460         private void showLatestUpdate() {
461                 Version latestVersion = updateChecker.getLatestVersion();
462                 int versionDifference = latestVersion.compareTo(VERSION);
463                 if (versionDifference > 0) {
464                         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);
465                 } else if (versionDifference < 0) {
466                         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);
467                 } else {
468                         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);
469                 }
470         }
471
472         //
473         // INTERFACE ListSelectionListener
474         //
475
476         /**
477          * {@inheritDoc}
478          */
479         public void valueChanged(ListSelectionEvent e) {
480                 JList list = (JList) e.getSource();
481                 int selectedRow = list.getSelectedIndex();
482                 wizard.setNextEnabled(selectedRow > -1);
483         }
484
485         //
486         // INTERFACE WizardListener
487         //
488
489         /**
490          * {@inheritDoc}
491          */
492         public void wizardNextPressed(TWizard wizard) {
493                 String pageName = wizard.getPage().getName();
494                 if ("page.node-manager".equals(pageName)) {
495                         showPage(PageType.PAGE_PROJECTS);
496                 } else if ("page.project".equals(pageName)) {
497                         ProjectPage projectPage = (ProjectPage) wizard.getPage();
498                         Project project = projectPage.getSelectedProject();
499                         if ((project.getLocalPath() == null) || (project.getLocalPath().trim().length() == 0)) {
500                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-local-path"), null, JOptionPane.ERROR_MESSAGE);
501                                 return;
502                         }
503                         if ((project.getPath() == null) || (project.getPath().trim().length() == 0)) {
504                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-path"), null, JOptionPane.ERROR_MESSAGE);
505                                 return;
506                         }
507                         ((ProjectFilesPage) pages.get(PageType.PAGE_PROJECT_FILES)).setProject(project);
508                         ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).setProject(project);
509                         showPage(PageType.PAGE_PROJECT_FILES);
510                 } else if ("page.project.files".equals(pageName)) {
511                         ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
512                         Project project = projectPage.getSelectedProject();
513                         if (selectedNode == null) {
514                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
515                                 return;
516                         }
517                         if ((project.getIndexFile() == null) || (project.getIndexFile().length() == 0)) {
518                                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.empty-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
519                                         return;
520                                 }
521                         } else {
522                                 File indexFile = new File(project.getLocalPath(), project.getIndexFile());
523                                 if (!indexFile.exists()) {
524                                         JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.index-missing"), null, JOptionPane.ERROR_MESSAGE);
525                                         return;
526                                 }
527                         }
528                         String indexFile = project.getIndexFile();
529                         boolean hasIndexFile = (indexFile != null);
530                         if (hasIndexFile && !project.getFileOption(indexFile).getContainer().equals("")) {
531                                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.container-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
532                                         return;
533                                 }
534                         }
535                         List<String> allowedIndexContentTypes = Arrays.asList("text/html", "application/xhtml+xml");
536                         if (hasIndexFile && !allowedIndexContentTypes.contains(project.getFileOption(indexFile).getMimeType())) {
537                                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.index-not-html"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
538                                         return;
539                                 }
540                         }
541                         Map<String, FileOption> fileOptions = project.getFileOptions();
542                         Set<Entry<String, FileOption>> fileOptionEntries = fileOptions.entrySet();
543                         for (Entry<String, FileOption> fileOptionEntry : fileOptionEntries) {
544                                 FileOption fileOption = fileOptionEntry.getValue();
545                                 if (!fileOption.isInsert() && ((fileOption.getCustomKey().length() == 0) || "CHK@".equals(fileOption.getCustomKey()))) {
546                                         JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.project-files.no-custom-key"), fileOptionEntry.getKey()), null, JOptionPane.ERROR_MESSAGE);
547                                         return;
548                                 }
549                         }
550                         boolean nodeRunning = false;
551                         try {
552                                 nodeRunning = freenetInterface.isNodePresent();
553                         } catch (IOException e) {
554                                 /* ignore. */
555                         }
556                         if (!nodeRunning) {
557                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-running"), null, JOptionPane.ERROR_MESSAGE);
558                                 return;
559                         }
560                         configuration.save();
561                         showPage(PageType.PAGE_INSERT_PROJECT);
562                         ProjectInsertPage projectInsertPage = (ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT);
563                         String tempDirectory = ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).getTempDirectory();
564                         projectInsertPage.setTempDirectory(tempDirectory);
565                         projectInsertPage.startInsert();
566                         nodeMenu.setEnabled(false);
567                         optionsPreferencesAction.setEnabled(false);
568                 } else if ("page.project.insert".equals(pageName)) {
569                         showPage(PageType.PAGE_PROJECTS);
570                         nodeMenu.setEnabled(true);
571                         optionsPreferencesAction.setEnabled(true);
572                 } else if ("page.preferences".equals(pageName)) {
573                         showPage(PageType.PAGE_PROJECTS);
574                         optionsPreferencesAction.setEnabled(true);
575                 }
576         }
577
578         /**
579          * {@inheritDoc}
580          */
581         public void wizardPreviousPressed(TWizard wizard) {
582                 String pageName = wizard.getPage().getName();
583                 if ("page.project".equals(pageName)) {
584                         showPage(PageType.PAGE_NODE_MANAGER);
585                 } else if ("page.project.files".equals(pageName)) {
586                         showPage(PageType.PAGE_PROJECTS);
587                 } else if ("page.project.insert".equals(pageName)) {
588                         showPage(PageType.PAGE_PROJECT_FILES);
589                 }
590         }
591
592         /**
593          * {@inheritDoc}
594          */
595         public void wizardQuitPressed(TWizard wizard) {
596                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.question"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
597                         if (saveConfiguration()) {
598                                 System.exit(0);
599                         }
600                         if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.config-not-saved"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
601                                 System.exit(0);
602                         }
603                 }
604         }
605
606         //
607         // INTERFACE NodeManagerListener
608         //
609
610         /**
611          * {@inheritDoc}
612          */
613         public void nodesUpdated(Node[] nodes) {
614                 nodeMenu.removeAll();
615                 ButtonGroup nodeButtonGroup = new ButtonGroup();
616                 Node newSelectedNode = null;
617                 for (Node node : nodes) {
618                         JRadioButtonMenuItem nodeMenuItem = new JRadioButtonMenuItem(node.getName());
619                         nodeMenuItem.putClientProperty("Node", node);
620                         nodeMenuItem.addActionListener(this);
621                         nodeButtonGroup.add(nodeMenuItem);
622                         if (node.equals(selectedNode)) {
623                                 newSelectedNode = node;
624                                 nodeMenuItem.setSelected(true);
625                         }
626                         nodeMenu.add(nodeMenuItem);
627                 }
628                 nodeMenu.addSeparator();
629                 nodeMenu.add(manageNodeAction);
630                 selectedNode = newSelectedNode;
631                 freenetInterface.setNode(selectedNode);
632         }
633
634         /**
635          * {@inheritDoc}
636          */
637         public void actionPerformed(ActionEvent e) {
638                 Object source = e.getSource();
639                 if (source instanceof JRadioButtonMenuItem) {
640                         JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) source;
641                         Node node = (Node) menuItem.getClientProperty("Node");
642                         selectedNode = node;
643                         freenetInterface.setNode(selectedNode);
644                 }
645         }
646
647         //
648         // INTERFACE UpdateListener
649         //
650
651         /**
652          * {@inheritDoc}
653          */
654         public void foundUpdateData(Version foundVersion, long versionTimestamp) {
655                 logger.log(Level.FINEST, "Found version {0} from {1,date}.", new Object[] { foundVersion, versionTimestamp });
656                 if (foundVersion.compareTo(VERSION) > 0) {
657                         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);
658                 }
659         }
660
661         //
662         // MAIN METHOD
663         //
664
665         /**
666          * Main method that is called by the VM.
667          *
668          * @param args
669          *            The command-line arguments
670          */
671         public static void main(String[] args) {
672                 /* initialize logger. */
673                 Logger logger = Logger.getLogger("de.todesbaum");
674                 Handler handler = new ConsoleHandler();
675                 logger.addHandler(handler);
676                 String configFilename = null;
677                 boolean nextIsConfigFilename = false;
678                 for (String argument : args) {
679                         if (nextIsConfigFilename) {
680                                 configFilename = argument;
681                                 nextIsConfigFilename = false;
682                         }
683                         if ("--help".equals(argument)) {
684                                 printHelp();
685                                 return;
686                         } else if ("--debug".equals(argument)) {
687                                 logger.setLevel(Level.ALL);
688                                 handler.setLevel(Level.ALL);
689                         } else if ("--config-file".equals(argument)) {
690                                 nextIsConfigFilename = true;
691                         }
692                 }
693                 if (nextIsConfigFilename) {
694                         System.out.println("--config-file needs parameter!");
695                         return;
696                 }
697                 new Main(configFilename);
698         }
699
700         /**
701          * Prints a small syntax help.
702          */
703         private static void printHelp() {
704                 System.out.println("--help\tshows this cruft");
705                 System.out.println("--debug\tenables some debug output");
706                 System.out.println("--config-file <file>\tuse specified configuration file");
707         }
708
709 }