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