Make update URL a bit easier to update.
[jSite.git] / src / de / todesbaum / jsite / gui / UpdateChecker.java
index 46b16c9..b707f08 100644 (file)
 
 package de.todesbaum.jsite.gui;
 
+import java.awt.BorderLayout;
 import java.awt.event.ActionEvent;
+import java.io.IOException;
+import java.text.MessageFormat;
 
 import javax.swing.AbstractAction;
 import javax.swing.Action;
+import javax.swing.JButton;
 import javax.swing.JDialog;
 import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.JOptionPane;
 import javax.swing.JPanel;
+import javax.swing.JProgressBar;
 
 import de.todesbaum.jsite.application.Freenet7Interface;
 import de.todesbaum.jsite.i18n.I18n;
+import de.todesbaum.jsite.i18n.I18nContainer;
+import de.todesbaum.util.freenet.fcp2.Connection;
 
 /**
  * Checks for newer versions of jSite.
  *
  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
  */
-public class UpdateChecker extends JDialog {
+public class UpdateChecker {
+
+       /** The edition for the update check URL. */
+       private static final int UPDATE_EDITION = 2;
 
        /** The URL for update checks. */
        @SuppressWarnings("unused")
-       private static final String UPDATE_KEY = "USK@e3myoFyp5avg6WYN16ImHri6J7Nj8980Fm~aQe4EX1U,QvbWT0ImE0TwLODTl7EoJx2NBnwDxTbLTE6zkB-eGPs,AQACAAE/jSite/0/currentVersion.txt";
+       private static final String UPDATE_KEY = "USK@e3myoFyp5avg6WYN16ImHri6J7Nj8980Fm~aQe4EX1U,QvbWT0ImE0TwLODTl7EoJx2NBnwDxTbLTE6zkB-eGPs,AQACAAE/jSite/" + UPDATE_EDITION + "/jSite.properties";
+
+       /** Object used for synchronization. */
+       private final Object syncObject = new Object();
+
+       /** The parent of the dialog. */
+       private final JFrame parent;
 
        /** The freenet interface. */
-       @SuppressWarnings("unused")
        private final Freenet7Interface freenetInterface;
 
        /** The cancel action. */
-       @SuppressWarnings("unused")
        private Action cancelAction;
 
+       /** Whether the busy dialog has been cancelled. */
+       private boolean cancelled;
+
        /**
         * Creates a new update checker that uses the given frame as its parent and
         * communications via the given freenet interface.
@@ -59,8 +78,9 @@ public class UpdateChecker extends JDialog {
         *            The freenet interface
         */
        public UpdateChecker(JFrame parent, Freenet7Interface freenetInterface) {
-               super(parent);
+               this.parent = parent;
                this.freenetInterface = freenetInterface;
+               cancelled = false;
                initActions();
        }
 
@@ -72,7 +92,20 @@ public class UpdateChecker extends JDialog {
         * Checks for updates, showing a dialog with an indeterminate progress bar.
         */
        public void checkForUpdates() {
-               /* TODO */
+               JDialog busyDialog = showBusyDialog();
+               Connection connection = freenetInterface.getConnection("jSite-update-check");
+               try {
+                       if (!connection.connect()) {
+                               busyDialog.setVisible(false);
+                               JOptionPane.showMessageDialog(parent, I18n.getMessage(""), I18n.getMessage(""), JOptionPane.ERROR_MESSAGE);
+                               return;
+                       }
+               } catch (IOException ioe1) {
+                       busyDialog.setVisible(false);
+                       JOptionPane.showMessageDialog(parent, MessageFormat.format(I18n.getMessage(""), ioe1.getMessage()), I18n.getMessage(""), JOptionPane.ERROR_MESSAGE);
+               } finally {
+                       connection.disconnect();
+               }
        }
 
        //
@@ -88,21 +121,77 @@ public class UpdateChecker extends JDialog {
                        /**
                         * {@inheritDoc}
                         */
+                       @SuppressWarnings("synthetic-access")
                        public void actionPerformed(ActionEvent actionEvent) {
-                               /* TODO */
+                               synchronized (syncObject) {
+                                       cancelled = true;
+                               }
                        }
                };
        }
 
        /**
+        * Shows a “please wait” dialog.
+        *
+        * @return The dialog
+        */
+       private JDialog showBusyDialog() {
+               BusyPanel busyPanel = new BusyPanel();
+               JButton cancelButton = new JButton(cancelAction);
+               JOptionPane optionPane = new JOptionPane(busyPanel, JOptionPane.INFORMATION_MESSAGE, 0, null, new Object[] { cancelButton });
+               final JDialog busyDialog = optionPane.createDialog(parent, I18n.getMessage(""));
+               new Thread(new Runnable() {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       public void run() {
+                               busyDialog.setVisible(true);
+                       }
+               }).start();
+               return busyDialog;
+       }
+
+       /**
         * A panel that shows a busy progress bar and a “please wait” message.
         *
         * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
         */
-       @SuppressWarnings("unused")
        private class BusyPanel extends JPanel {
 
-               /* TODO */
+               /**
+                * Creates a new busy panel.
+                */
+               public BusyPanel() {
+                       super(new BorderLayout(12, 12));
+                       initComponents();
+               }
+
+               //
+               // PRIVATE METHODS
+               //
+
+               /**
+                * Initializes all components of this panel.
+                */
+               private void initComponents() {
+                       final JLabel label = new JLabel(I18n.getMessage("")); /* TODO */
+                       JProgressBar progressBar = new JProgressBar();
+                       progressBar.setIndeterminate(true);
+
+                       add(label, BorderLayout.PAGE_START);
+                       add(progressBar, BorderLayout.PAGE_END);
+
+                       I18nContainer.getInstance().registerRunnable(new Runnable() {
+
+                               /**
+                                * {@inheritDoc}
+                                */
+                               public void run() {
+                                       label.setText(I18n.getMessage("")); /* TODO */
+                               }
+                       });
+               }
 
        }