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