8dd2e55c673ca5aba18383df38adb94ddcdaa4a2
[jSite.git] / src / de / todesbaum / jsite / main / Main.java
1 /*
2  * jSite - Main.java - Copyright © 2006–2012 David Roden
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 package de.todesbaum.jsite.main;
20
21 import java.awt.Component;
22 import java.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
24 import java.io.IOException;
25 import java.text.MessageFormat;
26 import java.util.Date;
27 import java.util.HashMap;
28 import java.util.Locale;
29 import java.util.Map;
30 import java.util.logging.ConsoleHandler;
31 import java.util.logging.Handler;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
34
35 import javax.swing.AbstractAction;
36 import javax.swing.Action;
37 import javax.swing.ButtonGroup;
38 import javax.swing.Icon;
39 import javax.swing.JList;
40 import javax.swing.JMenu;
41 import javax.swing.JMenuBar;
42 import javax.swing.JMenuItem;
43 import javax.swing.JOptionPane;
44 import javax.swing.JPanel;
45 import javax.swing.JRadioButtonMenuItem;
46 import javax.swing.event.ListSelectionEvent;
47 import javax.swing.event.ListSelectionListener;
48
49 import de.todesbaum.jsite.application.Freenet7Interface;
50 import de.todesbaum.jsite.application.Node;
51 import de.todesbaum.jsite.application.Project;
52 import de.todesbaum.jsite.application.ProjectInserter;
53 import de.todesbaum.jsite.application.ProjectInserter.CheckReport;
54 import de.todesbaum.jsite.application.ProjectInserter.Issue;
55 import de.todesbaum.jsite.application.UpdateChecker;
56 import de.todesbaum.jsite.application.UpdateListener;
57 import de.todesbaum.jsite.gui.NodeManagerListener;
58 import de.todesbaum.jsite.gui.NodeManagerPage;
59 import de.todesbaum.jsite.gui.PreferencesPage;
60 import de.todesbaum.jsite.gui.ProjectFilesPage;
61 import de.todesbaum.jsite.gui.ProjectInsertPage;
62 import de.todesbaum.jsite.gui.ProjectPage;
63 import de.todesbaum.jsite.i18n.I18n;
64 import de.todesbaum.jsite.i18n.I18nContainer;
65 import de.todesbaum.jsite.main.ConfigurationLocator.ConfigurationLocation;
66 import de.todesbaum.util.image.IconLoader;
67 import de.todesbaum.util.swing.TWizard;
68 import de.todesbaum.util.swing.TWizardPage;
69 import de.todesbaum.util.swing.WizardListener;
70
71 /**
72  * The main class that ties together everything.
73  *
74  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
75  */
76 public class Main implements ActionListener, ListSelectionListener, WizardListener, NodeManagerListener, UpdateListener {
77
78         /** The logger. */
79         private static final Logger logger = Logger.getLogger(Main.class.getName());
80
81         /** The version. */
82         private static final Version VERSION = new Version(0, 9, 2);
83
84         /** The configuration. */
85         private Configuration configuration;
86
87         /** The freenet interface. */
88         private Freenet7Interface freenetInterface = new Freenet7Interface();
89
90         /** The update checker. */
91         private final UpdateChecker updateChecker;
92
93         /** The jSite icon. */
94         private Icon jSiteIcon;
95
96         /**
97          * Enumeration for all possible pages.
98          *
99          * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
100          */
101         private static enum PageType {
102
103                 /** The node manager page. */
104                 PAGE_NODE_MANAGER,
105
106                 /** The project page. */
107                 PAGE_PROJECTS,
108
109                 /** The project files page. */
110                 PAGE_PROJECT_FILES,
111
112                 /** The project insert page. */
113                 PAGE_INSERT_PROJECT,
114
115                 /** The preferences page. */
116                 PAGE_PREFERENCES
117
118         }
119
120         /** The supported locales. */
121         private static final Locale[] SUPPORTED_LOCALES = new Locale[] { Locale.ENGLISH, Locale.GERMAN, Locale.FRENCH };
122
123         /** The actions that switch the language. */
124         private Map<Locale, Action> languageActions = new HashMap<Locale, Action>();
125
126         /** The “manage nodes” action. */
127         private Action manageNodeAction;
128
129         /** The “preferences” action. */
130         private Action optionsPreferencesAction;
131
132         /** The “check for updates” action. */
133         private Action checkForUpdatesAction;
134
135         /** The “about jSite” action. */
136         private Action aboutAction;
137
138         /** The wizard. */
139         private TWizard wizard;
140
141         /** The node menu. */
142         private JMenu nodeMenu;
143
144         /** The currently selected node. */
145         private Node selectedNode;
146
147         /** Mapping from page type to page. */
148         private final Map<PageType, TWizardPage> pages = new HashMap<PageType, TWizardPage>();
149
150         /** The original location of the configuration file. */
151         private ConfigurationLocation originalLocation;
152
153         /**
154          * Creates a new core with the default configuration file.
155          */
156         private Main() {
157                 this(null);
158         }
159
160         /**
161          * Creates a new core with the given configuration from the given file.
162          *
163          * @param configFilename
164          *            The name of the configuration file
165          */
166         private Main(String configFilename) {
167                 /* collect all possible configuration file locations. */
168                 ConfigurationLocator configurationLocator = new ConfigurationLocator();
169                 if (configFilename != null) {
170                         configurationLocator.setCustomLocation(configFilename);
171                 }
172
173                 originalLocation = configurationLocator.findPreferredLocation();
174                 logger.log(Level.CONFIG, "Using configuration from " + originalLocation + ".");
175                 configuration = new Configuration(configurationLocator, originalLocation);
176
177                 Locale.setDefault(configuration.getLocale());
178                 I18n.setLocale(configuration.getLocale());
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                                 optionsPreferencesAction.setEnabled(true);
216                                 wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
217                                 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
218                         }
219                 };
220                 optionsPreferencesAction = new AbstractAction(I18n.getMessage("jsite.menu.options.preferences")) {
221
222                         /**
223                          * {@inheritDoc}
224                          */
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          * Returns whether a configuration file would be overwritten when calling
363          * {@link #saveConfiguration()}.
364          *
365          * @return {@code true} if {@link #saveConfiguration()} would overwrite an
366          *         existing file, {@code false} otherwise
367          */
368         private boolean isOverwritingConfiguration() {
369                 return configuration.getConfigurationLocator().hasFile(configuration.getConfigurationDirectory());
370         }
371
372         /**
373          * Saves the configuration.
374          *
375          * @return <code>true</code> if the configuration could be saved,
376          *         <code>false</code> otherwise
377          */
378         private boolean saveConfiguration() {
379                 NodeManagerPage nodeManagerPage = (NodeManagerPage) pages.get(PageType.PAGE_NODE_MANAGER);
380                 configuration.setNodes(nodeManagerPage.getNodes());
381                 if (selectedNode != null) {
382                         configuration.setSelectedNode(selectedNode);
383                 }
384
385                 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
386                 configuration.setProjects(projectPage.getProjects());
387
388                 PreferencesPage preferencesPage = (PreferencesPage) pages.get(PageType.PAGE_PREFERENCES);
389                 configuration.setTempDirectory(preferencesPage.getTempDirectory());
390
391                 return configuration.save();
392         }
393
394         /**
395          * Finds a supported locale for the given locale.
396          *
397          * @param forLocale
398          *            The locale to find a supported locale for
399          * @return The supported locale that was found, or the default locale if no
400          *         supported locale could be found
401          */
402         private Locale findSupportedLocale(Locale forLocale) {
403                 for (Locale locale : SUPPORTED_LOCALES) {
404                         if (locale.equals(forLocale)) {
405                                 return locale;
406                         }
407                 }
408                 for (Locale locale : SUPPORTED_LOCALES) {
409                         if (locale.getCountry().equals(forLocale.getCountry()) && locale.getLanguage().equals(forLocale.getLanguage())) {
410                                 return locale;
411                         }
412                 }
413                 for (Locale locale : SUPPORTED_LOCALES) {
414                         if (locale.getLanguage().equals(forLocale.getLanguage())) {
415                                 return locale;
416                         }
417                 }
418                 return SUPPORTED_LOCALES[0];
419         }
420
421         /**
422          * Returns the version.
423          *
424          * @return The version
425          */
426         public static final Version getVersion() {
427                 return VERSION;
428         }
429
430         //
431         // ACTIONS
432         //
433
434         /**
435          * Switches the language of the interface to the given locale.
436          *
437          * @param locale
438          *            The locale to switch to
439          */
440         private void switchLanguage(Locale locale) {
441                 Locale supportedLocale = findSupportedLocale(locale);
442                 Action languageAction = languageActions.get(supportedLocale);
443                 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) languageAction.getValue("menuItem");
444                 menuItem.setSelected(true);
445                 I18n.setLocale(supportedLocale);
446                 for (Runnable i18nRunnable : I18nContainer.getInstance()) {
447                         try {
448                                 i18nRunnable.run();
449                         } catch (Throwable t) {
450                                 /* we probably shouldn't swallow this. */
451                         }
452                 }
453                 wizard.setPage(wizard.getPage());
454                 configuration.setLocale(supportedLocale);
455         }
456
457         /**
458          * Shows a dialog with general preferences.
459          */
460         private void optionsPreferences() {
461                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setConfigurationLocation(configuration.getConfigurationDirectory());
462                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setHasNextToJarConfiguration(configuration.getConfigurationLocator().isValidLocation(ConfigurationLocation.NEXT_TO_JAR_FILE));
463                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setHasCustomConfiguration(configuration.getConfigurationLocator().isValidLocation(ConfigurationLocation.CUSTOM));
464                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setUseEarlyEncode(configuration.useEarlyEncode());
465                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setPriority(configuration.getPriority());
466                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setManifestPutter(configuration.getManifestPutter());
467                 showPage(PageType.PAGE_PREFERENCES);
468                 optionsPreferencesAction.setEnabled(false);
469                 wizard.setNextEnabled(true);
470                 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
471         }
472
473         /**
474          * Shows a dialog box that shows the last version that was found by the
475          * {@link UpdateChecker}.
476          */
477         private void showLatestUpdate() {
478                 Version latestVersion = updateChecker.getLatestVersion();
479                 int versionDifference = latestVersion.compareTo(VERSION);
480                 if (versionDifference > 0) {
481                         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);
482                 } else if (versionDifference < 0) {
483                         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);
484                 } else {
485                         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);
486                 }
487         }
488
489         //
490         // INTERFACE ListSelectionListener
491         //
492
493         /**
494          * {@inheritDoc}
495          */
496         public void valueChanged(ListSelectionEvent e) {
497                 JList list = (JList) e.getSource();
498                 int selectedRow = list.getSelectedIndex();
499                 wizard.setNextEnabled(selectedRow > -1);
500         }
501
502         //
503         // INTERFACE WizardListener
504         //
505
506         /**
507          * {@inheritDoc}
508          */
509         public void wizardNextPressed(TWizard wizard) {
510                 String pageName = wizard.getPage().getName();
511                 if ("page.node-manager".equals(pageName)) {
512                         showPage(PageType.PAGE_PROJECTS);
513                 } else if ("page.project".equals(pageName)) {
514                         ProjectPage projectPage = (ProjectPage) wizard.getPage();
515                         Project project = projectPage.getSelectedProject();
516                         if ((project.getLocalPath() == null) || (project.getLocalPath().trim().length() == 0)) {
517                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.warning.no-local-path"), null, JOptionPane.ERROR_MESSAGE);
518                                 return;
519                         }
520                         if ((project.getPath() == null) || (project.getPath().trim().length() == 0)) {
521                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.warning.no-path"), null, JOptionPane.ERROR_MESSAGE);
522                                 return;
523                         }
524                         ((ProjectFilesPage) pages.get(PageType.PAGE_PROJECT_FILES)).setProject(project);
525                         ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).setProject(project);
526                         showPage(PageType.PAGE_PROJECT_FILES);
527                 } else if ("page.project.files".equals(pageName)) {
528                         ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
529                         Project project = projectPage.getSelectedProject();
530                         if (selectedNode == null) {
531                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.error.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
532                                 return;
533                         }
534                         CheckReport checkReport = ProjectInserter.validateProject(project);
535                         for (Issue issue : checkReport) {
536                                 if (issue.isFatal()) {
537                                         JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite." + issue.getErrorKey()), (Object[]) issue.getParameters()), null, JOptionPane.ERROR_MESSAGE);
538                                         return;
539                                 }
540                                 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) {
541                                         return;
542                                 }
543                         }
544                         boolean nodeRunning = false;
545                         try {
546                                 nodeRunning = freenetInterface.isNodePresent();
547                         } catch (IOException e) {
548                                 /* ignore. */
549                         }
550                         if (!nodeRunning) {
551                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.error.no-node-running"), null, JOptionPane.ERROR_MESSAGE);
552                                 return;
553                         }
554                         configuration.save();
555                         showPage(PageType.PAGE_INSERT_PROJECT);
556                         ProjectInsertPage projectInsertPage = (ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT);
557                         String tempDirectory = ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).getTempDirectory();
558                         projectInsertPage.setTempDirectory(tempDirectory);
559                         projectInsertPage.setUseEarlyEncode(configuration.useEarlyEncode());
560                         projectInsertPage.setPriority(configuration.getPriority());
561                         projectInsertPage.setManifestPutter(configuration.getManifestPutter());
562                         projectInsertPage.startInsert();
563                         nodeMenu.setEnabled(false);
564                         optionsPreferencesAction.setEnabled(false);
565                 } else if ("page.project.insert".equals(pageName)) {
566                         ProjectInsertPage projectInsertPage = (ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT);
567                         if (projectInsertPage.isRunning()) {
568                                 projectInsertPage.stopInsert();
569                         } else {
570                                 showPage(PageType.PAGE_PROJECTS);
571                                 nodeMenu.setEnabled(true);
572                                 optionsPreferencesAction.setEnabled(true);
573                         }
574                 } else if ("page.preferences".equals(pageName)) {
575                         PreferencesPage preferencesPage = (PreferencesPage) pages.get(PageType.PAGE_PREFERENCES);
576                         showPage(PageType.PAGE_PROJECTS);
577                         optionsPreferencesAction.setEnabled(true);
578                         configuration.setUseEarlyEncode(preferencesPage.useEarlyEncode());
579                         configuration.setPriority(preferencesPage.getPriority());
580                         configuration.setManifestPutter(preferencesPage.getManifestPutter());
581                         configuration.setConfigurationLocation(preferencesPage.getConfigurationLocation());
582                 }
583         }
584
585         /**
586          * {@inheritDoc}
587          */
588         public void wizardPreviousPressed(TWizard wizard) {
589                 String pageName = wizard.getPage().getName();
590                 if ("page.project".equals(pageName) || "page.preferences".equals(pageName)) {
591                         showPage(PageType.PAGE_NODE_MANAGER);
592                         optionsPreferencesAction.setEnabled(true);
593                 } else if ("page.project.files".equals(pageName)) {
594                         showPage(PageType.PAGE_PROJECTS);
595                 } else if ("page.project.insert".equals(pageName)) {
596                         showPage(PageType.PAGE_PROJECT_FILES);
597                 }
598         }
599
600         /**
601          * {@inheritDoc}
602          */
603         public void wizardQuitPressed(TWizard wizard) {
604                 if (((ProjectPage) pages.get(PageType.PAGE_PROJECTS)).wasUriCopied() || ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).wasUriCopied()) {
605                         JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.use-clipboard-now"));
606                 }
607                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.question"), I18n.getMessage("jsite.quit.question.title"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
608                         if (isOverwritingConfiguration()) {
609                                 int overwriteConfigurationAnswer = JOptionPane.showConfirmDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.quit.overwrite-configuration"), configuration.getConfigurationLocator().getFile(configuration.getConfigurationDirectory())), I18n.getMessage("jsite.quit.overwrite-configuration.title"), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
610                                 if (overwriteConfigurationAnswer == JOptionPane.YES_OPTION) {
611                                         if (saveConfiguration()) {
612                                                 System.exit(0);
613                                         }
614                                 } else if (overwriteConfigurationAnswer == JOptionPane.CANCEL_OPTION) {
615                                         return;
616                                 }
617                                 if (overwriteConfigurationAnswer == JOptionPane.NO_OPTION) {
618                                         System.exit(0);
619                                 }
620                         } else {
621                                 if (saveConfiguration()) {
622                                         System.exit(0);
623                                 }
624                         }
625                         if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.config-not-saved"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
626                                 System.exit(0);
627                         }
628                 }
629         }
630
631         //
632         // INTERFACE NodeManagerListener
633         //
634
635         /**
636          * {@inheritDoc}
637          */
638         public void nodesUpdated(Node[] nodes) {
639                 nodeMenu.removeAll();
640                 ButtonGroup nodeButtonGroup = new ButtonGroup();
641                 Node newSelectedNode = null;
642                 for (Node node : nodes) {
643                         JRadioButtonMenuItem nodeMenuItem = new JRadioButtonMenuItem(node.getName());
644                         nodeMenuItem.putClientProperty("Node", node);
645                         nodeMenuItem.addActionListener(this);
646                         nodeButtonGroup.add(nodeMenuItem);
647                         if (node.equals(selectedNode)) {
648                                 newSelectedNode = node;
649                                 nodeMenuItem.setSelected(true);
650                         }
651                         nodeMenu.add(nodeMenuItem);
652                 }
653                 nodeMenu.addSeparator();
654                 nodeMenu.add(manageNodeAction);
655                 selectedNode = newSelectedNode;
656                 freenetInterface.setNode(selectedNode);
657         }
658
659         /**
660          * {@inheritDoc}
661          */
662         public void nodeSelected(Node node) {
663                 for (Component menuItem : nodeMenu.getMenuComponents()) {
664                         if (menuItem instanceof JMenuItem) {
665                                 if (node.equals(((JMenuItem) menuItem).getClientProperty("Node"))) {
666                                         ((JMenuItem) menuItem).setSelected(true);
667                                 }
668                         }
669                 }
670                 freenetInterface.setNode(node);
671                 selectedNode = node;
672         }
673
674         //
675         // INTERFACE ActionListener
676         //
677
678         /**
679          * {@inheritDoc}
680          */
681         public void actionPerformed(ActionEvent e) {
682                 Object source = e.getSource();
683                 if (source instanceof JRadioButtonMenuItem) {
684                         JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) source;
685                         Node node = (Node) menuItem.getClientProperty("Node");
686                         selectedNode = node;
687                         freenetInterface.setNode(selectedNode);
688                 }
689         }
690
691         //
692         // INTERFACE UpdateListener
693         //
694
695         /**
696          * {@inheritDoc}
697          */
698         public void foundUpdateData(Version foundVersion, long versionTimestamp) {
699                 logger.log(Level.FINEST, "Found version {0} from {1,date}.", new Object[] { foundVersion, versionTimestamp });
700                 if (foundVersion.compareTo(VERSION) > 0) {
701                         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);
702                 }
703         }
704
705         //
706         // MAIN METHOD
707         //
708
709         /**
710          * Main method that is called by the VM.
711          *
712          * @param args
713          *            The command-line arguments
714          */
715         public static void main(String[] args) {
716                 /* initialize logger. */
717                 Logger logger = Logger.getLogger("de.todesbaum");
718                 Handler handler = new ConsoleHandler();
719                 logger.addHandler(handler);
720                 String configFilename = null;
721                 boolean nextIsConfigFilename = false;
722                 for (String argument : args) {
723                         if (nextIsConfigFilename) {
724                                 configFilename = argument;
725                                 nextIsConfigFilename = false;
726                         }
727                         if ("--help".equals(argument)) {
728                                 printHelp();
729                                 return;
730                         } else if ("--debug".equals(argument)) {
731                                 logger.setLevel(Level.ALL);
732                                 handler.setLevel(Level.ALL);
733                         } else if ("--config-file".equals(argument)) {
734                                 nextIsConfigFilename = true;
735                         }
736                 }
737                 if (nextIsConfigFilename) {
738                         System.out.println("--config-file needs parameter!");
739                         return;
740                 }
741                 new Main(configFilename);
742         }
743
744         /**
745          * Prints a small syntax help.
746          */
747         private static void printHelp() {
748                 System.out.println("--help\tshows this cruft");
749                 System.out.println("--debug\tenables some debug output");
750                 System.out.println("--config-file <file>\tuse specified configuration file");
751         }
752
753 }