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