Implement UpdateListener and show dialog if a newer version was found.
[jSite.git] / src / de / todesbaum / jsite / main / Main.java
index dfae154..5601329 100644 (file)
 
 package de.todesbaum.jsite.main;
 
-import java.awt.BorderLayout;
 import java.awt.event.ActionEvent;
 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;
@@ -35,14 +35,11 @@ import javax.swing.AbstractAction;
 import javax.swing.Action;
 import javax.swing.ButtonGroup;
 import javax.swing.Icon;
-import javax.swing.JDialog;
-import javax.swing.JLabel;
 import javax.swing.JList;
 import javax.swing.JMenu;
 import javax.swing.JMenuBar;
 import javax.swing.JOptionPane;
 import javax.swing.JPanel;
-import javax.swing.JProgressBar;
 import javax.swing.JRadioButtonMenuItem;
 import javax.swing.event.ListSelectionEvent;
 import javax.swing.event.ListSelectionListener;
@@ -56,6 +53,8 @@ import de.todesbaum.jsite.gui.NodeManagerPage;
 import de.todesbaum.jsite.gui.ProjectFilesPage;
 import de.todesbaum.jsite.gui.ProjectInsertPage;
 import de.todesbaum.jsite.gui.ProjectPage;
+import de.todesbaum.jsite.gui.UpdateChecker;
+import de.todesbaum.jsite.gui.UpdateListener;
 import de.todesbaum.jsite.i18n.I18n;
 import de.todesbaum.jsite.i18n.I18nContainer;
 import de.todesbaum.util.image.IconLoader;
@@ -68,13 +67,13 @@ import de.todesbaum.util.swing.WizardListener;
  *
  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
  */
-public class Main implements ActionListener, ListSelectionListener, WizardListener, NodeManagerListener {
+public class Main implements ActionListener, ListSelectionListener, WizardListener, NodeManagerListener, UpdateListener {
 
        /** Whether the debug mode is activated. */
        private static boolean debug = false;
 
-       /** The URL for update checks. */
-       private static final String UPDATE_KEY = "USK@e3myoFyp5avg6WYN16ImHri6J7Nj8980Fm~aQe4EX1U,QvbWT0ImE0TwLODTl7EoJx2NBnwDxTbLTE6zkB-eGPs,AQACAAE/jSite/0/currentVersion.txt";
+       /** The version. */
+       private static final Version VERSION = new Version(0, 6, 2);
 
        /** The configuration. */
        private Configuration configuration;
@@ -82,6 +81,9 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
        /** The freenet interface. */
        private Freenet7Interface freenetInterface = new Freenet7Interface();
 
+       /** The update checker. */
+       private final UpdateChecker updateChecker;
+
        /** The jSite icon. */
        private Icon jSiteIcon;
 
@@ -157,7 +159,7 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
                if (!configuration.createLockFile()) {
                        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) {
-                               return;
+                               throw new IllegalStateException("Lockfile override not active, refusing start.");
                        }
                        configuration.removeLockfileOnExit();
                }
@@ -171,6 +173,10 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
                jSiteIcon = IconLoader.loadIcon("/jsite-icon.png");
                wizard.setIcon(jSiteIcon);
 
+               updateChecker = new UpdateChecker(wizard, freenetInterface);
+               updateChecker.addUpdateListener(this);
+               updateChecker.start();
+
                initPages();
                showPage(PageType.PAGE_PROJECTS);
        }
@@ -202,16 +208,15 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
                        /**
                         * {@inheritDoc}
                         */
-                       @SuppressWarnings("synthetic-access")
                        public void actionPerformed(ActionEvent actionEvent) {
-                               checkForUpdates();
+                               /* TODO */
                        }
                };
                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);
                        }
                };
 
@@ -363,6 +368,15 @@ 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
        //
@@ -390,24 +404,6 @@ public class Main implements ActionListener, ListSelectionListener, WizardListen
                configuration.setLocale(supportedLocale);
        }
 
-       /**
-        * Checks for updates of jSite.
-        */
-       private void checkForUpdates() {
-               System.out.println("checkForUpdates()");
-               /* construct a small panel for the dialog. */
-               JPanel waitingDialogPanel = new JPanel(new BorderLayout(12, 12));
-               waitingDialogPanel.add(new JLabel(I18n.getMessage("")), BorderLayout.PAGE_START);
-               JProgressBar progressBar = new JProgressBar();
-               progressBar.setIndeterminate(true);
-               waitingDialogPanel.add(progressBar, BorderLayout.PAGE_END);
-               JOptionPane waitingDialog = new JOptionPane(waitingDialogPanel, JOptionPane.INFORMATION_MESSAGE, 0, null, new Object[] { "Cancel" });
-               JDialog dialog = new JDialog(wizard, true);
-               dialog.getContentPane().add(waitingDialog, BorderLayout.CENTER);
-               dialog.pack();
-               dialog.setVisible(true);
-       }
-
        //
        // INTERFACE ListSelectionListener
        //
@@ -575,6 +571,19 @@ 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
        //