Increase version number to 0.7.1.
[jSite.git] / src / de / todesbaum / jsite / main / Main.java
index f1948f3..a2ba940 100644 (file)
@@ -24,11 +24,16 @@ import java.awt.event.ActionListener;
 import java.io.File;
 import java.io.IOException;
 import java.text.MessageFormat;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
 import java.util.Map.Entry;
+import java.util.logging.ConsoleHandler;
+import java.util.logging.Handler;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import javax.swing.AbstractAction;
 import javax.swing.Action;
@@ -47,6 +52,8 @@ import de.todesbaum.jsite.application.FileOption;
 import de.todesbaum.jsite.application.Freenet7Interface;
 import de.todesbaum.jsite.application.Node;
 import de.todesbaum.jsite.application.Project;
+import de.todesbaum.jsite.application.UpdateChecker;
+import de.todesbaum.jsite.application.UpdateListener;
 import de.todesbaum.jsite.gui.NodeManagerListener;
 import de.todesbaum.jsite.gui.NodeManagerPage;
 import de.todesbaum.jsite.gui.ProjectFilesPage;
@@ -60,32 +67,88 @@ import de.todesbaum.util.swing.TWizardPage;
 import de.todesbaum.util.swing.WizardListener;
 
 /**
+ * The main class that ties together everything.
+ *
  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
  */
-public class Main implements ActionListener, ListSelectionListener, WizardListener, NodeManagerListener {
+public class Main implements ActionListener, ListSelectionListener, WizardListener, NodeManagerListener, UpdateListener {
+
+       /** The version. */
+       private static final Version VERSION = new Version(0, 7, 1);
 
-       private static boolean debug = false;
+       /** The configuration. */
        private Configuration configuration;
+
+       /** The freenet interface. */
        private Freenet7Interface freenetInterface = new Freenet7Interface();
-       protected Icon jSiteIcon;
 
+       /** The update checker. */
+       private final UpdateChecker updateChecker;
+
+       /** The jSite icon. */
+       private Icon jSiteIcon;
+
+       /**
+        * Enumeration for all possible pages.
+        *
+        * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
+        */
        private static enum PageType {
-               PAGE_NODE_MANAGER, PAGE_PROJECTS, PAGE_PROJECT_FILES, PAGE_INSERT_PROJECT
+
+               /** The node manager page. */
+               PAGE_NODE_MANAGER,
+
+               /** The project page. */
+               PAGE_PROJECTS,
+
+               /** The project files page. */
+               PAGE_PROJECT_FILES,
+
+               /** The project insert page. */
+               PAGE_INSERT_PROJECT
+
        }
 
+       /** The supported locales. */
        private static final Locale[] SUPPORTED_LOCALES = new Locale[] { Locale.ENGLISH, Locale.GERMAN, Locale.FRENCH, Locale.ITALIAN, new Locale("pl") };
-       protected Map<Locale, Action> languageActions = new HashMap<Locale, Action>();
-       protected Action manageNodeAction;
-       protected Action aboutAction;
-       protected TWizard wizard;
-       protected JMenu nodeMenu;
+
+       /** The actions that switch the language. */
+       private Map<Locale, Action> languageActions = new HashMap<Locale, Action>();
+
+       /** The “manage nodes” action. */
+       private Action manageNodeAction;
+
+       /** The “check for updates” action. */
+       private Action checkForUpdatesAction;
+
+       /** The “about jSite” action. */
+       private Action aboutAction;
+
+       /** The wizard. */
+       private TWizard wizard;
+
+       /** The node menu. */
+       private JMenu nodeMenu;
+
+       /** The currently selected node. */
        private Node selectedNode;
+
+       /** Mapping from page type to page. */
        private final Map<PageType, TWizardPage> pages = new HashMap<PageType, TWizardPage>();
 
+       /**
+        * Creates a new core with the default configuration file.
+        */
        private Main() {
                this(null);
        }
 
+       /**
+        * Creates a new core with the given configuration from the given file.
+        *
+        * @param configFilename
+        *            The name of the configuration file
+        */
        private Main(String configFilename) {
                if (configFilename != null) {
                        configuration = new Configuration(configFilename);
@@ -95,8 +158,11 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
                Locale.setDefault(configuration.getLocale());
                I18n.setLocale(configuration.getLocale());
                if (!configuration.createLockFile()) {
-                       JOptionPane.showMessageDialog(null, I18n.getMessage("jsite.main.already-running"), null, JOptionPane.ERROR_MESSAGE);
-                       return;
+                       int option = JOptionPane.showOptionDialog(null, I18n.getMessage("jsite.main.already-running"), "", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[] { I18n.getMessage("jsite.main.already-running.override"), I18n.getMessage("jsite.wizard.quit") }, I18n.getMessage("jsite.wizard.quit"));
+                       if (option != 0) {
+                               throw new IllegalStateException("Lockfile override not active, refusing start.");
+                       }
+                       configuration.removeLockfileOnExit();
                }
                wizard = new TWizard();
                createActions();
@@ -108,34 +174,57 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
                jSiteIcon = IconLoader.loadIcon("/jsite-icon.png");
                wizard.setIcon(jSiteIcon);
 
+               updateChecker = new UpdateChecker(freenetInterface);
+               updateChecker.addUpdateListener(this);
+               updateChecker.start();
+
                initPages();
                showPage(PageType.PAGE_PROJECTS);
        }
 
+       /**
+        * Creates all actions.
+        */
        private void createActions() {
-               for (final Locale locale: SUPPORTED_LOCALES) {
+               for (final Locale locale : SUPPORTED_LOCALES) {
                        languageActions.put(locale, new AbstractAction(I18n.getMessage("jsite.menu.language." + locale.getLanguage()), IconLoader.loadIcon("/flag-" + locale.getLanguage() + ".png")) {
 
+                               @SuppressWarnings("synthetic-access")
                                public void actionPerformed(ActionEvent actionEvent) {
                                        switchLanguage(locale);
                                }
                        });
                }
                manageNodeAction = new AbstractAction(I18n.getMessage("jsite.menu.nodes.manage-nodes")) {
+
+                       @SuppressWarnings("synthetic-access")
                        public void actionPerformed(ActionEvent actionEvent) {
                                showPage(PageType.PAGE_NODE_MANAGER);
                                wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
                                wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
                        }
                };
+               checkForUpdatesAction = new AbstractAction(I18n.getMessage("jsite.menu.help.check-for-updates")) {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent actionEvent) {
+                               showLatestUpdate();
+                       }
+               };
                aboutAction = new AbstractAction(I18n.getMessage("jsite.menu.help.about")) {
 
+                       @SuppressWarnings("synthetic-access")
                        public void actionPerformed(ActionEvent e) {
-                               JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.about.message"), Version.getVersion()), null, JOptionPane.INFORMATION_MESSAGE, jSiteIcon);
+                               JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.about.message"), getVersion().toString()), null, JOptionPane.INFORMATION_MESSAGE, jSiteIcon);
                        }
                };
 
                I18nContainer.getInstance().registerRunnable(new Runnable() {
+
+                       @SuppressWarnings("synthetic-access")
                        public void run() {
                                manageNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.nodes.manage-nodes"));
                                aboutAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.help.about"));
@@ -143,12 +232,17 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
                });
        }
 
+       /**
+        * Creates the menu bar.
+        *
+        * @return The menu bar
+        */
        private JMenuBar createMenuBar() {
                JMenuBar menuBar = new JMenuBar();
                final JMenu languageMenu = new JMenu(I18n.getMessage("jsite.menu.languages"));
                menuBar.add(languageMenu);
                ButtonGroup languageButtonGroup = new ButtonGroup();
-               for (Locale locale: SUPPORTED_LOCALES) {
+               for (Locale locale : SUPPORTED_LOCALES) {
                        Action languageAction = languageActions.get(locale);
                        JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(languageActions.get(locale));
                        if (locale.equals(Locale.getDefault())) {
@@ -170,14 +264,17 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
 
                final JMenu helpMenu = new JMenu(I18n.getMessage("jsite.menu.help"));
                menuBar.add(helpMenu);
+               helpMenu.add(checkForUpdatesAction);
                helpMenu.add(aboutAction);
 
                I18nContainer.getInstance().registerRunnable(new Runnable() {
+
+                       @SuppressWarnings("synthetic-access")
                        public void run() {
                                languageMenu.setText(I18n.getMessage("jsite.menu.languages"));
                                nodeMenu.setText(I18n.getMessage("jsite.menu.nodes"));
                                helpMenu.setText(I18n.getMessage("jsite.menu.help"));
-                               for (Map.Entry<Locale, Action> languageActionEntry: languageActions.entrySet()) {
+                               for (Map.Entry<Locale, Action> languageActionEntry : languageActions.entrySet()) {
                                        languageActionEntry.getValue().putValue(Action.NAME, I18n.getMessage("jsite.menu.language." + languageActionEntry.getKey().getLanguage()));
                                }
                        }
@@ -186,6 +283,9 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
                return menuBar;
        }
 
+       /**
+        * Initializes all pages.
+        */
        private void initPages() {
                NodeManagerPage nodeManagerPage = new NodeManagerPage(wizard);
                nodeManagerPage.setName("page.node-manager");
@@ -205,19 +305,30 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
                pages.put(PageType.PAGE_PROJECT_FILES, projectFilesPage);
 
                ProjectInsertPage projectInsertPage = new ProjectInsertPage(wizard);
-               projectInsertPage.setDebug(debug);
                projectInsertPage.setName("page.project.insert");
                projectInsertPage.setFreenetInterface(freenetInterface);
                pages.put(PageType.PAGE_INSERT_PROJECT, projectInsertPage);
        }
 
-       protected void showPage(PageType pageType) {
+       /**
+        * Shows the page with the given type.
+        *
+        * @param pageType
+        *            The page type to show
+        */
+       private void showPage(PageType pageType) {
                wizard.setPreviousEnabled(pageType.ordinal() > 0);
                wizard.setNextEnabled(pageType.ordinal() < (pages.size() - 1));
                wizard.setPage(pages.get(pageType));
                wizard.setTitle(pages.get(pageType).getHeading() + " - jSite");
        }
 
+       /**
+        * Saves the configuration.
+        *
+        * @return <code>true</code> if the configuration could be saved,
+        *         <code>false</code> otherwise
+        */
        private boolean saveConfiguration() {
                NodeManagerPage nodeManagerPage = (NodeManagerPage) pages.get(PageType.PAGE_NODE_MANAGER);
                configuration.setNodes(nodeManagerPage.getNodes());
@@ -231,18 +342,26 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
                return configuration.save();
        }
 
+       /**
+        * Finds a supported locale for the given locale.
+        *
+        * @param forLocale
+        *            The locale to find a supported locale for
+        * @return The supported locale that was found, or the default locale if no
+        *         supported locale could be found
+        */
        private Locale findSupportedLocale(Locale forLocale) {
-               for (Locale locale: SUPPORTED_LOCALES) {
+               for (Locale locale : SUPPORTED_LOCALES) {
                        if (locale.equals(forLocale)) {
                                return locale;
                        }
                }
-               for (Locale locale: SUPPORTED_LOCALES) {
+               for (Locale locale : SUPPORTED_LOCALES) {
                        if (locale.getCountry().equals(forLocale.getCountry()) && locale.getLanguage().equals(forLocale.getLanguage())) {
                                return locale;
                        }
                }
-               for (Locale locale: SUPPORTED_LOCALES) {
+               for (Locale locale : SUPPORTED_LOCALES) {
                        if (locale.getLanguage().equals(forLocale.getLanguage())) {
                                return locale;
                        }
@@ -250,17 +369,32 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
                return SUPPORTED_LOCALES[0];
        }
 
+       /**
+        * Returns the version.
+        *
+        * @return The version
+        */
+       public static final Version getVersion() {
+               return VERSION;
+       }
+
        //
        // ACTIONS
        //
 
-       protected void switchLanguage(Locale locale) {
+       /**
+        * Switches the language of the interface to the given locale.
+        *
+        * @param locale
+        *            The locale to switch to
+        */
+       private void switchLanguage(Locale locale) {
                Locale supportedLocale = findSupportedLocale(locale);
                Action languageAction = languageActions.get(supportedLocale);
                JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) languageAction.getValue("menuItem");
                menuItem.setSelected(true);
                I18n.setLocale(supportedLocale);
-               for (Runnable i18nRunnable: I18nContainer.getInstance()) {
+               for (Runnable i18nRunnable : I18nContainer.getInstance()) {
                        try {
                                i18nRunnable.run();
                        } catch (Throwable t) {
@@ -271,6 +405,22 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
                configuration.setLocale(supportedLocale);
        }
 
+       /**
+        * Shows a dialog box that shows the last version that was found by the
+        * {@link UpdateChecker}.
+        */
+       private void showLatestUpdate() {
+               Version latestVersion = updateChecker.getLatestVersion();
+               int versionDifference = latestVersion.compareTo(VERSION);
+               if (versionDifference > 0) {
+                       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);
+               } else if (versionDifference < 0) {
+                       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);
+               } else {
+                       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);
+               }
+       }
+
        //
        // INTERFACE ListSelectionListener
        //
@@ -316,7 +466,7 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
                                JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
                                return;
                        }
-                       if (project.getIndexFile() == null) {
+                       if ((project.getIndexFile() == null) || (project.getIndexFile().length() == 0)) {
                                if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.empty-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
                                        return;
                                }
@@ -341,7 +491,7 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
                        }
                        Map<String, FileOption> fileOptions = project.getFileOptions();
                        Set<Entry<String, FileOption>> fileOptionEntries = fileOptions.entrySet();
-                       for (Entry<String, FileOption> fileOptionEntry: fileOptionEntries) {
+                       for (Entry<String, FileOption> fileOptionEntry : fileOptionEntries) {
                                FileOption fileOption = fileOptionEntry.getValue();
                                if (!fileOption.isInsert() && ((fileOption.getCustomKey().length() == 0) || "CHK@".equals(fileOption.getCustomKey()))) {
                                        JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.project-files.no-custom-key"), fileOptionEntry.getKey()), null, JOptionPane.ERROR_MESSAGE);
@@ -352,6 +502,7 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
                        try {
                                nodeRunning = freenetInterface.isNodePresent();
                        } catch (IOException e) {
+                               /* ignore. */
                        }
                        if (!nodeRunning) {
                                JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-running"), null, JOptionPane.ERROR_MESSAGE);
@@ -406,7 +557,7 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
                nodeMenu.removeAll();
                ButtonGroup nodeButtonGroup = new ButtonGroup();
                Node newSelectedNode = null;
-               for (Node node: nodes) {
+               for (Node node : nodes) {
                        JRadioButtonMenuItem nodeMenuItem = new JRadioButtonMenuItem(node.getName());
                        nodeMenuItem.putClientProperty("Node", node);
                        nodeMenuItem.addActionListener(this);
@@ -437,12 +588,36 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
        }
 
        //
+       // INTERFACE UpdateListener
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       public void foundUpdateData(Version foundVersion, long versionTimestamp) {
+               if (foundVersion.compareTo(VERSION) > 0) {
+                       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);
+               }
+       }
+
+       //
        // MAIN METHOD
        //
+
+       /**
+        * Main method that is called by the VM.
+        *
+        * @param args
+        *            The command-line arguments
+        */
        public static void main(String[] args) {
+               /* initialize logger. */
+               Logger logger = Logger.getLogger("de.todesbaum");
+               Handler handler = new ConsoleHandler();
+               logger.addHandler(handler);
                String configFilename = null;
                boolean nextIsConfigFilename = false;
-               for (String argument: args) {
+               for (String argument : args) {
                        if (nextIsConfigFilename) {
                                configFilename = argument;
                                nextIsConfigFilename = false;
@@ -451,7 +626,8 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
                                printHelp();
                                return;
                        } else if ("--debug".equals(argument)) {
-                               debug = true;
+                               logger.setLevel(Level.ALL);
+                               handler.setLevel(Level.ALL);
                        } else if ("--config-file".equals(argument)) {
                                nextIsConfigFilename = true;
                        }
@@ -463,6 +639,9 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
                new Main(configFilename);
        }
 
+       /**
+        * Prints a small syntax help.
+        */
        private static void printHelp() {
                System.out.println("--help\tshows this cruft");
                System.out.println("--debug\tenables some debug output");