8f46c150ef9bb06e649c2d0968e097a076f72992
[jSite.git] / src / de / todesbaum / jsite / gui / UpdateChecker.java
1 /*
2  * jSite-remote - UpdateChecker.java -
3  * Copyright © 2008 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package de.todesbaum.jsite.gui;
21
22 import java.awt.BorderLayout;
23 import java.awt.event.ActionEvent;
24 import java.io.IOException;
25 import java.text.MessageFormat;
26
27 import javax.swing.AbstractAction;
28 import javax.swing.Action;
29 import javax.swing.JButton;
30 import javax.swing.JDialog;
31 import javax.swing.JFrame;
32 import javax.swing.JLabel;
33 import javax.swing.JOptionPane;
34 import javax.swing.JPanel;
35 import javax.swing.JProgressBar;
36
37 import de.todesbaum.jsite.application.Freenet7Interface;
38 import de.todesbaum.jsite.i18n.I18n;
39 import de.todesbaum.jsite.i18n.I18nContainer;
40 import de.todesbaum.util.freenet.fcp2.Connection;
41
42 /**
43  * Checks for newer versions of jSite.
44  *
45  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
46  */
47 public class UpdateChecker {
48
49         /** The URL for update checks. */
50         @SuppressWarnings("unused")
51         private static final String UPDATE_KEY = "USK@e3myoFyp5avg6WYN16ImHri6J7Nj8980Fm~aQe4EX1U,QvbWT0ImE0TwLODTl7EoJx2NBnwDxTbLTE6zkB-eGPs,AQACAAE/jSite/0/currentVersion.txt";
52
53         /** Object used for synchronization. */
54         private final Object syncObject = new Object();
55
56         /** The parent of the dialog. */
57         private final JFrame parent;
58
59         /** The freenet interface. */
60         private final Freenet7Interface freenetInterface;
61
62         /** The cancel action. */
63         private Action cancelAction;
64
65         /** Whether the busy dialog has been cancelled. */
66         private boolean cancelled;
67
68         /**
69          * Creates a new update checker that uses the given frame as its parent and
70          * communications via the given freenet interface.
71          *
72          * @param parent
73          *            The parent of the dialog
74          * @param freenetInterface
75          *            The freenet interface
76          */
77         public UpdateChecker(JFrame parent, Freenet7Interface freenetInterface) {
78                 this.parent = parent;
79                 this.freenetInterface = freenetInterface;
80                 cancelled = false;
81                 initActions();
82         }
83
84         //
85         // ACTIONS
86         //
87
88         /**
89          * Checks for updates, showing a dialog with an indeterminate progress bar.
90          */
91         public void checkForUpdates() {
92                 JDialog busyDialog = showBusyDialog();
93                 Connection connection = freenetInterface.getConnection("jSite-update-check");
94                 try {
95                         if (!connection.connect()) {
96                                 busyDialog.setVisible(false);
97                                 JOptionPane.showMessageDialog(parent, I18n.getMessage(""), I18n.getMessage(""), JOptionPane.ERROR_MESSAGE);
98                                 return;
99                         }
100                 } catch (IOException ioe1) {
101                         busyDialog.setVisible(false);
102                         JOptionPane.showMessageDialog(parent, MessageFormat.format(I18n.getMessage(""), ioe1.getMessage()), I18n.getMessage(""), JOptionPane.ERROR_MESSAGE);
103                 } finally {
104                         connection.disconnect();
105                 }
106         }
107
108         //
109         // PRIVATE METHODS
110         //
111
112         /**
113          * Initializes all actions.
114          */
115         private void initActions() {
116                 cancelAction = new AbstractAction(I18n.getMessage("")) {
117
118                         /**
119                          * {@inheritDoc}
120                          */
121                         @SuppressWarnings("synthetic-access")
122                         public void actionPerformed(ActionEvent actionEvent) {
123                                 synchronized (syncObject) {
124                                         cancelled = true;
125                                 }
126                         }
127                 };
128         }
129
130         /**
131          * Shows a “please wait” dialog.
132          *
133          * @return The dialog
134          */
135         private JDialog showBusyDialog() {
136                 BusyPanel busyPanel = new BusyPanel();
137                 JButton cancelButton = new JButton(cancelAction);
138                 JOptionPane optionPane = new JOptionPane(busyPanel, JOptionPane.INFORMATION_MESSAGE, 0, null, new Object[] { cancelButton });
139                 final JDialog busyDialog = optionPane.createDialog(parent, I18n.getMessage(""));
140                 new Thread(new Runnable() {
141
142                         /**
143                          * {@inheritDoc}
144                          */
145                         public void run() {
146                                 busyDialog.setVisible(true);
147                         }
148                 }).start();
149                 return busyDialog;
150         }
151
152         /**
153          * A panel that shows a busy progress bar and a “please wait” message.
154          *
155          * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
156          */
157         private class BusyPanel extends JPanel {
158
159                 /**
160                  * Creates a new busy panel.
161                  */
162                 public BusyPanel() {
163                         super(new BorderLayout(12, 12));
164                         initComponents();
165                 }
166
167                 //
168                 // PRIVATE METHODS
169                 //
170
171                 /**
172                  * Initializes all components of this panel.
173                  */
174                 private void initComponents() {
175                         final JLabel label = new JLabel(I18n.getMessage("")); /* TODO */
176                         JProgressBar progressBar = new JProgressBar();
177                         progressBar.setIndeterminate(true);
178
179                         add(label, BorderLayout.PAGE_START);
180                         add(progressBar, BorderLayout.PAGE_END);
181
182                         I18nContainer.getInstance().registerRunnable(new Runnable() {
183
184                                 /**
185                                  * {@inheritDoc}
186                                  */
187                                 public void run() {
188                                         label.setText(I18n.getMessage("")); /* TODO */
189                                 }
190                         });
191                 }
192
193         }
194
195 }