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