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