Merge branch 'italian-translation' into next
[jSite.git] / src / main / java / de / todesbaum / jsite / main / Main.java
1 /*
2  * jSite - Main.java - Copyright © 2006–2014 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 net.pterodactylus.util.image.IconLoader;
50 import de.todesbaum.jsite.application.Freenet7Interface;
51 import de.todesbaum.jsite.application.Node;
52 import de.todesbaum.jsite.application.Project;
53 import de.todesbaum.jsite.application.ProjectInserter;
54 import de.todesbaum.jsite.application.ProjectInserter.CheckReport;
55 import de.todesbaum.jsite.application.ProjectInserter.Issue;
56 import de.todesbaum.jsite.application.UpdateChecker;
57 import de.todesbaum.jsite.application.UpdateListener;
58 import de.todesbaum.jsite.application.WebOfTrustInterface;
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.ConfigurationLocator.ConfigurationLocation;
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, 12);
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 web of trust interface. */
95         private final WebOfTrustInterface webOfTrustInterface;
96
97         /** The jSite icon. */
98         private Icon jSiteIcon;
99
100         /**
101          * Enumeration for all possible pages.
102          *
103          * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
104          */
105         private static enum PageType {
106
107                 /** The node manager page. */
108                 PAGE_NODE_MANAGER,
109
110                 /** The project page. */
111                 PAGE_PROJECTS,
112
113                 /** The project files page. */
114                 PAGE_PROJECT_FILES,
115
116                 /** The project insert page. */
117                 PAGE_INSERT_PROJECT,
118
119                 /** The preferences page. */
120                 PAGE_PREFERENCES
121
122         }
123
124         /** The supported locales. */
125         private static final Locale[] SUPPORTED_LOCALES = new Locale[] {
126                         Locale.ENGLISH,
127                         Locale.GERMAN,
128                         Locale.FRENCH,
129                         Locale.ITALIAN,
130                         new Locale("pl"),
131                         new Locale("fi")
132         };
133
134         /** The actions that switch the language. */
135         private Map<Locale, Action> languageActions = new HashMap<Locale, Action>();
136
137         /** The “manage nodes” action. */
138         private Action manageNodeAction;
139
140         /** The “preferences” action. */
141         private Action optionsPreferencesAction;
142
143         /** The “check for updates” action. */
144         private Action checkForUpdatesAction;
145
146         /** The “about jSite” action. */
147         private Action aboutAction;
148
149         /** The wizard. */
150         private TWizard wizard;
151
152         /** The node menu. */
153         private JMenu nodeMenu;
154
155         /** The currently selected node. */
156         private Node selectedNode;
157
158         /** Mapping from page type to page. */
159         private final Map<PageType, TWizardPage> pages = new HashMap<PageType, TWizardPage>();
160
161         /** The original location of the configuration file. */
162         private ConfigurationLocation originalLocation;
163
164         /**
165          * Creates a new core with the default configuration file.
166          */
167         private Main() {
168                 this(null);
169         }
170
171         /**
172          * Creates a new core with the given configuration from the given file.
173          *
174          * @param configFilename
175          *            The name of the configuration file
176          */
177         private Main(String configFilename) {
178                 /* collect all possible configuration file locations. */
179                 ConfigurationLocator configurationLocator = new ConfigurationLocator();
180                 if (configFilename != null) {
181                         configurationLocator.setCustomLocation(configFilename);
182                 }
183
184                 originalLocation = configurationLocator.findPreferredLocation();
185                 logger.log(Level.CONFIG, "Using configuration from " + originalLocation + ".");
186                 configuration = new Configuration(configurationLocator, originalLocation);
187
188                 Locale.setDefault(configuration.getLocale());
189                 I18n.setLocale(configuration.getLocale());
190                 wizard = new TWizard();
191                 createActions();
192                 wizard.setJMenuBar(createMenuBar());
193                 wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
194                 wizard.setPreviousEnabled(false);
195                 wizard.setNextEnabled(true);
196                 wizard.addWizardListener(this);
197                 jSiteIcon = IconLoader.loadIcon("/jsite-icon.png");
198                 wizard.setIcon(jSiteIcon);
199
200                 updateChecker = new UpdateChecker(freenetInterface, getVersion());
201                 updateChecker.addUpdateListener(this);
202                 updateChecker.start();
203
204                 webOfTrustInterface = new WebOfTrustInterface(freenetInterface);
205
206                 initPages();
207                 showPage(PageType.PAGE_PROJECTS);
208         }
209
210         /**
211          * Creates all actions.
212          */
213         private void createActions() {
214                 for (final Locale locale : SUPPORTED_LOCALES) {
215                         languageActions.put(locale, new AbstractAction(I18n.getMessage("jsite.menu.language." + locale.getLanguage()), IconLoader.loadIcon("/flag-" + locale.getLanguage() + ".png")) {
216
217                                 @Override
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                         @Override
227                         @SuppressWarnings("synthetic-access")
228                         public void actionPerformed(ActionEvent actionEvent) {
229                                 showPage(PageType.PAGE_NODE_MANAGER);
230                                 optionsPreferencesAction.setEnabled(true);
231                                 wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
232                                 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
233                         }
234                 };
235                 optionsPreferencesAction = new AbstractAction(I18n.getMessage("jsite.menu.options.preferences")) {
236
237                         /**
238                          * {@inheritDoc}
239                          */
240                         @Override
241                         @SuppressWarnings("synthetic-access")
242                         public void actionPerformed(ActionEvent actionEvent) {
243                                 optionsPreferences();
244                         }
245                 };
246                 checkForUpdatesAction = new AbstractAction(I18n.getMessage("jsite.menu.help.check-for-updates")) {
247
248                         /**
249                          * {@inheritDoc}
250                          */
251                         @Override
252                         @SuppressWarnings("synthetic-access")
253                         public void actionPerformed(ActionEvent actionEvent) {
254                                 showLatestUpdate();
255                         }
256                 };
257                 aboutAction = new AbstractAction(I18n.getMessage("jsite.menu.help.about")) {
258
259                         @Override
260                         @SuppressWarnings("synthetic-access")
261                         public void actionPerformed(ActionEvent e) {
262                                 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.about.message"), getVersion().toString()), null, JOptionPane.INFORMATION_MESSAGE, jSiteIcon);
263                         }
264                 };
265
266                 I18nContainer.getInstance().registerRunnable(new Runnable() {
267
268                         @Override
269                         @SuppressWarnings("synthetic-access")
270                         public void run() {
271                                 manageNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.nodes.manage-nodes"));
272                                 optionsPreferencesAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.options.preferences"));
273                                 checkForUpdatesAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.help.check-for-updates"));
274                                 aboutAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.help.about"));
275                         }
276                 });
277         }
278
279         /**
280          * Creates the menu bar.
281          *
282          * @return The menu bar
283          */
284         private JMenuBar createMenuBar() {
285                 JMenuBar menuBar = new JMenuBar();
286                 final JMenu languageMenu = new JMenu(I18n.getMessage("jsite.menu.languages"));
287                 menuBar.add(languageMenu);
288                 ButtonGroup languageButtonGroup = new ButtonGroup();
289                 for (Locale locale : SUPPORTED_LOCALES) {
290                         Action languageAction = languageActions.get(locale);
291                         JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(languageActions.get(locale));
292                         if (locale.equals(Locale.getDefault())) {
293                                 menuItem.setSelected(true);
294                         }
295                         languageAction.putValue("menuItem", menuItem);
296                         languageButtonGroup.add(menuItem);
297                         languageMenu.add(menuItem);
298                 }
299                 nodeMenu = new JMenu(I18n.getMessage("jsite.menu.nodes"));
300                 menuBar.add(nodeMenu);
301                 selectedNode = configuration.getSelectedNode();
302                 nodesUpdated(configuration.getNodes());
303
304                 final JMenu optionsMenu = new JMenu(I18n.getMessage("jsite.menu.options"));
305                 menuBar.add(optionsMenu);
306                 optionsMenu.add(optionsPreferencesAction);
307
308                 /* evil hack to right-align the help menu */
309                 JPanel panel = new JPanel();
310                 panel.setOpaque(false);
311                 menuBar.add(panel);
312
313                 final JMenu helpMenu = new JMenu(I18n.getMessage("jsite.menu.help"));
314                 menuBar.add(helpMenu);
315                 helpMenu.add(checkForUpdatesAction);
316                 helpMenu.add(aboutAction);
317
318                 I18nContainer.getInstance().registerRunnable(new Runnable() {
319
320                         @Override
321                         @SuppressWarnings("synthetic-access")
322                         public void run() {
323                                 languageMenu.setText(I18n.getMessage("jsite.menu.languages"));
324                                 nodeMenu.setText(I18n.getMessage("jsite.menu.nodes"));
325                                 optionsMenu.setText(I18n.getMessage("jsite.menu.options"));
326                                 helpMenu.setText(I18n.getMessage("jsite.menu.help"));
327                                 for (Map.Entry<Locale, Action> languageActionEntry : languageActions.entrySet()) {
328                                         languageActionEntry.getValue().putValue(Action.NAME, I18n.getMessage("jsite.menu.language." + languageActionEntry.getKey().getLanguage()));
329                                 }
330                         }
331                 });
332
333                 return menuBar;
334         }
335
336         /**
337          * Initializes all pages.
338          */
339         private void initPages() {
340                 NodeManagerPage nodeManagerPage = new NodeManagerPage(wizard);
341                 nodeManagerPage.setName("page.node-manager");
342                 nodeManagerPage.addNodeManagerListener(this);
343                 nodeManagerPage.setNodes(configuration.getNodes());
344                 pages.put(PageType.PAGE_NODE_MANAGER, nodeManagerPage);
345
346                 ProjectPage projectPage = new ProjectPage(wizard);
347                 projectPage.setName("page.project");
348                 projectPage.setProjects(configuration.getProjects());
349                 projectPage.setFreenetInterface(freenetInterface);
350                 projectPage.setWebOfTrustInterface(webOfTrustInterface);
351                 projectPage.addListSelectionListener(this);
352                 pages.put(PageType.PAGE_PROJECTS, projectPage);
353
354                 ProjectFilesPage projectFilesPage = new ProjectFilesPage(wizard);
355                 projectFilesPage.setName("page.project.files");
356                 pages.put(PageType.PAGE_PROJECT_FILES, projectFilesPage);
357
358                 ProjectInsertPage projectInsertPage = new ProjectInsertPage(wizard);
359                 projectInsertPage.setName("page.project.insert");
360                 projectInsertPage.setFreenetInterface(freenetInterface);
361                 pages.put(PageType.PAGE_INSERT_PROJECT, projectInsertPage);
362
363                 PreferencesPage preferencesPage = new PreferencesPage(wizard);
364                 preferencesPage.setName("page.preferences");
365                 preferencesPage.setTempDirectory(configuration.getTempDirectory());
366                 pages.put(PageType.PAGE_PREFERENCES, preferencesPage);
367         }
368
369         /**
370          * Shows the page with the given type.
371          *
372          * @param pageType
373          *            The page type to show
374          */
375         private void showPage(PageType pageType) {
376                 wizard.setPreviousEnabled(pageType.ordinal() > 0);
377                 wizard.setNextEnabled(pageType.ordinal() < (pages.size() - 1));
378                 wizard.setPage(pages.get(pageType));
379                 wizard.setTitle(pages.get(pageType).getHeading() + " - jSite");
380         }
381
382         /**
383          * Returns whether a configuration file would be overwritten when calling
384          * {@link #saveConfiguration()}.
385          *
386          * @return {@code true} if {@link #saveConfiguration()} would overwrite an
387          *         existing file, {@code false} otherwise
388          */
389         private boolean isOverwritingConfiguration() {
390                 return configuration.getConfigurationLocator().hasFile(configuration.getConfigurationDirectory());
391         }
392
393         /**
394          * Saves the configuration.
395          *
396          * @return <code>true</code> if the configuration could be saved,
397          *         <code>false</code> otherwise
398          */
399         private boolean saveConfiguration() {
400                 NodeManagerPage nodeManagerPage = (NodeManagerPage) pages.get(PageType.PAGE_NODE_MANAGER);
401                 configuration.setNodes(nodeManagerPage.getNodes());
402                 if (selectedNode != null) {
403                         configuration.setSelectedNode(selectedNode);
404                 }
405
406                 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
407                 configuration.setProjects(projectPage.getProjects());
408
409                 PreferencesPage preferencesPage = (PreferencesPage) pages.get(PageType.PAGE_PREFERENCES);
410                 configuration.setTempDirectory(preferencesPage.getTempDirectory());
411
412                 return configuration.save();
413         }
414
415         /**
416          * Finds a supported locale for the given locale.
417          *
418          * @param forLocale
419          *            The locale to find a supported locale for
420          * @return The supported locale that was found, or the default locale if no
421          *         supported locale could be found
422          */
423         private static Locale findSupportedLocale(Locale forLocale) {
424                 for (Locale locale : SUPPORTED_LOCALES) {
425                         if (locale.equals(forLocale)) {
426                                 return locale;
427                         }
428                 }
429                 for (Locale locale : SUPPORTED_LOCALES) {
430                         if (locale.getCountry().equals(forLocale.getCountry()) && locale.getLanguage().equals(forLocale.getLanguage())) {
431                                 return locale;
432                         }
433                 }
434                 for (Locale locale : SUPPORTED_LOCALES) {
435                         if (locale.getLanguage().equals(forLocale.getLanguage())) {
436                                 return locale;
437                         }
438                 }
439                 return SUPPORTED_LOCALES[0];
440         }
441
442         /**
443          * Returns the version.
444          *
445          * @return The version
446          */
447         public static final Version getVersion() {
448                 return VERSION;
449         }
450
451         //
452         // ACTIONS
453         //
454
455         /**
456          * Switches the language of the interface to the given locale.
457          *
458          * @param locale
459          *            The locale to switch to
460          */
461         private void switchLanguage(Locale locale) {
462                 Locale supportedLocale = findSupportedLocale(locale);
463                 Action languageAction = languageActions.get(supportedLocale);
464                 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) languageAction.getValue("menuItem");
465                 menuItem.setSelected(true);
466                 I18n.setLocale(supportedLocale);
467                 for (Runnable i18nRunnable : I18nContainer.getInstance()) {
468                         try {
469                                 i18nRunnable.run();
470                         } catch (Throwable t) {
471                                 /* we probably shouldn't swallow this. */
472                         }
473                 }
474                 wizard.setPage(wizard.getPage());
475                 configuration.setLocale(supportedLocale);
476         }
477
478         /**
479          * Shows a dialog with general preferences.
480          */
481         private void optionsPreferences() {
482                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setConfigurationLocation(configuration.getConfigurationDirectory());
483                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setHasNextToJarConfiguration(configuration.getConfigurationLocator().isValidLocation(ConfigurationLocation.NEXT_TO_JAR_FILE));
484                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setHasCustomConfiguration(configuration.getConfigurationLocator().isValidLocation(ConfigurationLocation.CUSTOM));
485                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setUseEarlyEncode(configuration.useEarlyEncode());
486                 ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).setPriority(configuration.getPriority());
487                 showPage(PageType.PAGE_PREFERENCES);
488                 optionsPreferencesAction.setEnabled(false);
489                 wizard.setNextEnabled(true);
490                 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
491         }
492
493         /**
494          * Shows a dialog box that shows the last version that was found by the
495          * {@link UpdateChecker}.
496          */
497         private void showLatestUpdate() {
498                 Version latestVersion = updateChecker.getLatestVersion();
499                 int versionDifference = latestVersion.compareTo(VERSION);
500                 if (versionDifference > 0) {
501                         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);
502                 } else if (versionDifference < 0) {
503                         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);
504                 } else {
505                         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);
506                 }
507         }
508
509         /**
510          * Quits jSite, stopping all background services.
511          */
512         private void quit() {
513                 updateChecker.stop();
514                 System.exit(0);
515         }
516
517         //
518         // INTERFACE ListSelectionListener
519         //
520
521         /**
522          * {@inheritDoc}
523          */
524         @Override
525         public void valueChanged(ListSelectionEvent e) {
526                 JList list = (JList) e.getSource();
527                 int selectedRow = list.getSelectedIndex();
528                 wizard.setNextEnabled(selectedRow > -1);
529         }
530
531         //
532         // INTERFACE WizardListener
533         //
534
535         /**
536          * {@inheritDoc}
537          */
538         @Override
539         public void wizardNextPressed(TWizard wizard) {
540                 String pageName = wizard.getPage().getName();
541                 if ("page.node-manager".equals(pageName)) {
542                         showPage(PageType.PAGE_PROJECTS);
543                 } else if ("page.project".equals(pageName)) {
544                         ProjectPage projectPage = (ProjectPage) wizard.getPage();
545                         Project project = projectPage.getSelectedProject();
546                         if ((project.getLocalPath() == null) || (project.getLocalPath().trim().length() == 0)) {
547                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.warning.no-local-path"), null, JOptionPane.ERROR_MESSAGE);
548                                 return;
549                         }
550                         if ((project.getPath() == null) || (project.getPath().trim().length() == 0)) {
551                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.warning.no-path"), null, JOptionPane.ERROR_MESSAGE);
552                                 return;
553                         }
554                         ((ProjectFilesPage) pages.get(PageType.PAGE_PROJECT_FILES)).setProject(project);
555                         ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).setProject(project);
556                         showPage(PageType.PAGE_PROJECT_FILES);
557                 } else if ("page.project.files".equals(pageName)) {
558                         ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
559                         Project project = projectPage.getSelectedProject();
560                         if (selectedNode == null) {
561                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.error.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
562                                 return;
563                         }
564                         CheckReport checkReport = ProjectInserter.validateProject(project);
565                         for (Issue issue : checkReport) {
566                                 if (issue.isFatal()) {
567                                         JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite." + issue.getErrorKey()), (Object[]) issue.getParameters()), null, JOptionPane.ERROR_MESSAGE);
568                                         return;
569                                 }
570                                 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) {
571                                         return;
572                                 }
573                         }
574                         boolean nodeRunning = false;
575                         try {
576                                 nodeRunning = freenetInterface.isNodePresent();
577                         } catch (IOException e) {
578                                 /* ignore. */
579                         }
580                         if (!nodeRunning) {
581                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.error.no-node-running"), null, JOptionPane.ERROR_MESSAGE);
582                                 return;
583                         }
584                         configuration.save();
585                         showPage(PageType.PAGE_INSERT_PROJECT);
586                         ProjectInsertPage projectInsertPage = (ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT);
587                         String tempDirectory = ((PreferencesPage) pages.get(PageType.PAGE_PREFERENCES)).getTempDirectory();
588                         projectInsertPage.setTempDirectory(tempDirectory);
589                         projectInsertPage.setUseEarlyEncode(configuration.useEarlyEncode());
590                         projectInsertPage.setPriority(configuration.getPriority());
591                         projectInsertPage.startInsert();
592                         nodeMenu.setEnabled(false);
593                         optionsPreferencesAction.setEnabled(false);
594                 } else if ("page.project.insert".equals(pageName)) {
595                         ProjectInsertPage projectInsertPage = (ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT);
596                         if (projectInsertPage.isRunning()) {
597                                 projectInsertPage.stopInsert();
598                         } else {
599                                 showPage(PageType.PAGE_PROJECTS);
600                                 nodeMenu.setEnabled(true);
601                                 optionsPreferencesAction.setEnabled(true);
602                         }
603                 } else if ("page.preferences".equals(pageName)) {
604                         PreferencesPage preferencesPage = (PreferencesPage) pages.get(PageType.PAGE_PREFERENCES);
605                         showPage(PageType.PAGE_PROJECTS);
606                         optionsPreferencesAction.setEnabled(true);
607                         configuration.setUseEarlyEncode(preferencesPage.useEarlyEncode());
608                         configuration.setPriority(preferencesPage.getPriority());
609                         configuration.setConfigurationLocation(preferencesPage.getConfigurationLocation());
610                 }
611         }
612
613         /**
614          * {@inheritDoc}
615          */
616         @Override
617         public void wizardPreviousPressed(TWizard wizard) {
618                 String pageName = wizard.getPage().getName();
619                 if ("page.project".equals(pageName) || "page.preferences".equals(pageName)) {
620                         showPage(PageType.PAGE_NODE_MANAGER);
621                         optionsPreferencesAction.setEnabled(true);
622                 } else if ("page.project.files".equals(pageName)) {
623                         showPage(PageType.PAGE_PROJECTS);
624                 } else if ("page.project.insert".equals(pageName)) {
625                         showPage(PageType.PAGE_PROJECT_FILES);
626                 }
627         }
628
629         /**
630          * {@inheritDoc}
631          */
632         @Override
633         public void wizardQuitPressed(TWizard wizard) {
634                 if (((ProjectPage) pages.get(PageType.PAGE_PROJECTS)).wasUriCopied() || ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).wasUriCopied()) {
635                         JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.use-clipboard-now"));
636                 }
637                 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) {
638                         if (isOverwritingConfiguration() && !originalLocation.equals(configuration.getConfigurationDirectory())) {
639                                 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);
640                                 if (overwriteConfigurationAnswer == JOptionPane.YES_OPTION) {
641                                         if (saveConfiguration()) {
642                                                 quit();
643                                         }
644                                 } else if (overwriteConfigurationAnswer == JOptionPane.CANCEL_OPTION) {
645                                         return;
646                                 }
647                                 if (overwriteConfigurationAnswer == JOptionPane.NO_OPTION) {
648                                         quit();
649                                 }
650                         } else {
651                                 if (saveConfiguration()) {
652                                         quit();
653                                 }
654                         }
655                         if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.config-not-saved"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
656                                 quit();
657                         }
658                 }
659         }
660
661         //
662         // INTERFACE NodeManagerListener
663         //
664
665         /**
666          * {@inheritDoc}
667          */
668         @Override
669         public void nodesUpdated(Node[] nodes) {
670                 nodeMenu.removeAll();
671                 ButtonGroup nodeButtonGroup = new ButtonGroup();
672                 Node newSelectedNode = null;
673                 for (Node node : nodes) {
674                         JRadioButtonMenuItem nodeMenuItem = new JRadioButtonMenuItem(node.getName());
675                         nodeMenuItem.putClientProperty("Node", node);
676                         nodeMenuItem.addActionListener(this);
677                         nodeButtonGroup.add(nodeMenuItem);
678                         if (node.equals(selectedNode)) {
679                                 newSelectedNode = node;
680                                 nodeMenuItem.setSelected(true);
681                         }
682                         nodeMenu.add(nodeMenuItem);
683                 }
684                 nodeMenu.addSeparator();
685                 nodeMenu.add(manageNodeAction);
686                 selectedNode = newSelectedNode;
687                 freenetInterface.setNode(selectedNode);
688         }
689
690         /**
691          * {@inheritDoc}
692          */
693         @Override
694         public void nodeSelected(Node node) {
695                 for (Component menuItem : nodeMenu.getMenuComponents()) {
696                         if (menuItem instanceof JMenuItem) {
697                                 if (node.equals(((JMenuItem) menuItem).getClientProperty("Node"))) {
698                                         ((JMenuItem) menuItem).setSelected(true);
699                                 }
700                         }
701                 }
702                 freenetInterface.setNode(node);
703                 selectedNode = node;
704         }
705
706         //
707         // INTERFACE ActionListener
708         //
709
710         /**
711          * {@inheritDoc}
712          */
713         @Override
714         public void actionPerformed(ActionEvent e) {
715                 Object source = e.getSource();
716                 if (source instanceof JRadioButtonMenuItem) {
717                         JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) source;
718                         Node node = (Node) menuItem.getClientProperty("Node");
719                         selectedNode = node;
720                         freenetInterface.setNode(selectedNode);
721                 }
722         }
723
724         //
725         // INTERFACE UpdateListener
726         //
727
728         /**
729          * {@inheritDoc}
730          */
731         @Override
732         public void foundUpdateData(Version foundVersion, long versionTimestamp) {
733                 logger.log(Level.FINEST, "Found version {0} from {1,date}.", new Object[] { foundVersion, versionTimestamp });
734                 if (foundVersion.compareTo(VERSION) > 0) {
735                         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);
736                 }
737         }
738
739         //
740         // MAIN METHOD
741         //
742
743         /**
744          * Main method that is called by the VM.
745          *
746          * @param args
747          *            The command-line arguments
748          */
749         public static void main(String[] args) {
750                 /* initialize logger. */
751                 Logger logger = Logger.getLogger("de.todesbaum");
752                 Handler handler = new ConsoleHandler();
753                 logger.addHandler(handler);
754                 String configFilename = null;
755                 boolean nextIsConfigFilename = false;
756                 for (String argument : args) {
757                         if (nextIsConfigFilename) {
758                                 configFilename = argument;
759                                 nextIsConfigFilename = false;
760                         }
761                         if ("--help".equals(argument)) {
762                                 printHelp();
763                                 return;
764                         } else if ("--debug".equals(argument)) {
765                                 logger.setLevel(Level.ALL);
766                                 handler.setLevel(Level.ALL);
767                         } else if ("--config-file".equals(argument)) {
768                                 nextIsConfigFilename = true;
769                         }
770                 }
771                 if (nextIsConfigFilename) {
772                         System.out.println("--config-file needs parameter!");
773                         return;
774                 }
775                 new Main(configFilename);
776         }
777
778         /**
779          * Prints a small syntax help.
780          */
781         private static void printHelp() {
782                 System.out.println("--help\tshows this cruft");
783                 System.out.println("--debug\tenables some debug output");
784                 System.out.println("--config-file <file>\tuse specified configuration file");
785         }
786
787 }