svn-gvsig-desktop / tags / v2_0_0_Build_2059 / extensions / org.gvsig.installer / org.gvsig.installer.swing / org.gvsig.installer.swing.impl / src / main / java / org / gvsig / installer / swing / impl / execution / wizard / DownloadProgressWizard.java @ 39328
History | View | Annotate | Download (10.3 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
|
23 |
package org.gvsig.installer.swing.impl.execution.wizard; |
24 |
|
25 |
import java.awt.Component; |
26 |
import java.awt.GridBagConstraints; |
27 |
import java.awt.Insets; |
28 |
import java.awt.event.ActionEvent; |
29 |
import java.awt.event.ActionListener; |
30 |
import java.util.ArrayList; |
31 |
import java.util.List; |
32 |
|
33 |
import javax.swing.JButton; |
34 |
import javax.swing.JOptionPane; |
35 |
import javax.swing.JPanel; |
36 |
import javax.swing.SwingUtilities; |
37 |
|
38 |
import org.slf4j.Logger; |
39 |
import org.slf4j.LoggerFactory; |
40 |
|
41 |
import org.gvsig.gui.beans.wizard.WizardPanelWithLogo; |
42 |
import org.gvsig.gui.beans.wizard.panel.NotContinueWizardException; |
43 |
import org.gvsig.gui.beans.wizard.panel.OptionPanel; |
44 |
import org.gvsig.i18n.Messages; |
45 |
import org.gvsig.installer.lib.api.PackageInfo; |
46 |
import org.gvsig.installer.lib.api.execution.InstallPackageService; |
47 |
import org.gvsig.installer.swing.api.SwingInstallerLocator; |
48 |
import org.gvsig.installer.swing.api.execution.AbstractInstallPackageWizard; |
49 |
import org.gvsig.installer.swing.impl.DefaultSwingInstallerManager; |
50 |
import org.gvsig.installer.swing.impl.execution.DefaultInstallPackageWizard; |
51 |
import org.gvsig.installer.swing.impl.panel.DefaultProgressPanel; |
52 |
import org.gvsig.tools.task.AbstractMonitorableTask; |
53 |
import org.gvsig.tools.task.SimpleTaskStatus; |
54 |
|
55 |
/**
|
56 |
* @author gvSIG Team
|
57 |
* @version $Id$
|
58 |
*
|
59 |
*/
|
60 |
|
61 |
public class DownloadProgressWizard extends DefaultProgressPanel implements |
62 |
OptionPanel { |
63 |
|
64 |
private static final long serialVersionUID = -7064334977678611609L; |
65 |
private static Logger logger = LoggerFactory |
66 |
.getLogger(DownloadProgressWizard.class); |
67 |
private AbstractInstallPackageWizard abstractInstallPackageWizard;
|
68 |
// private abstractInstallPackageWizard abstractInstallPackageWizard;
|
69 |
public JButton startDownload; |
70 |
private int direccion = WizardPanelWithLogo.ACTION_NEXT; |
71 |
private DefaultSwingInstallerManager swingInstallerManager;
|
72 |
|
73 |
public DownloadProgressWizard(
|
74 |
AbstractInstallPackageWizard abstractInstallPackageWizard) { |
75 |
super();
|
76 |
this.abstractInstallPackageWizard = abstractInstallPackageWizard;
|
77 |
} |
78 |
|
79 |
@Override
|
80 |
protected void initComponents() { |
81 |
super.initComponents();
|
82 |
|
83 |
final DownloadProgressWizard wizard = this; |
84 |
|
85 |
swingInstallerManager = (DefaultSwingInstallerManager) SwingInstallerLocator |
86 |
.getSwingInstallerManager(); |
87 |
|
88 |
startDownload = new JButton(swingInstallerManager |
89 |
.getText("_start_download"));
|
90 |
|
91 |
java.awt.GridBagConstraints gridBagConstraints; |
92 |
gridBagConstraints = new GridBagConstraints(); |
93 |
gridBagConstraints.gridx = 0;
|
94 |
gridBagConstraints.gridy = 3;
|
95 |
gridBagConstraints.anchor = GridBagConstraints.EAST;
|
96 |
gridBagConstraints.insets = new Insets(5, 2, 2, 2); |
97 |
add(startDownload, gridBagConstraints); |
98 |
|
99 |
startDownload.addActionListener(new ActionListener() { |
100 |
|
101 |
public void actionPerformed(ActionEvent e) { |
102 |
UpdatePanel task = new UpdatePanel(wizard);
|
103 |
wizard.abstractInstallPackageWizard.addCancellableTask(task); |
104 |
bind(task.getTaskStatus()); |
105 |
task.start(); |
106 |
} |
107 |
}); |
108 |
} |
109 |
|
110 |
public JPanel getJPanel() { |
111 |
return this; |
112 |
} |
113 |
|
114 |
public String getPanelTitle() { |
115 |
return swingInstallerManager.getText("_download_progress"); |
116 |
} |
117 |
|
118 |
public void lastPanel() { |
119 |
abstractInstallPackageWizard.setFinishButtonVisible(false);
|
120 |
abstractInstallPackageWizard.setCancelButtonEnabled(true);
|
121 |
direccion = WizardPanelWithLogo.ACTION_NEXT; |
122 |
} |
123 |
|
124 |
public void nextPanel() throws NotContinueWizardException { |
125 |
direccion = WizardPanelWithLogo.ACTION_PREVIOUS; |
126 |
} |
127 |
|
128 |
public void updatePanel() { |
129 |
boolean thereIsDownloablePackage = false; |
130 |
|
131 |
InstallPackageService service = abstractInstallPackageWizard |
132 |
.getInstallerExecutionService(); |
133 |
service.getPackageCount(); |
134 |
|
135 |
for (int i = 0; i < abstractInstallPackageWizard |
136 |
.getInstallersToInstall().size(); i++) { |
137 |
|
138 |
if (abstractInstallPackageWizard.getInstallersToInstall().get(i)
|
139 |
.getDownloadURL() != null) {
|
140 |
thereIsDownloablePackage = true;
|
141 |
break;
|
142 |
} |
143 |
} |
144 |
|
145 |
if (thereIsDownloablePackage) {
|
146 |
this.abstractInstallPackageWizard.setNextButtonEnabled(false); |
147 |
this.startDownload.setEnabled(true); |
148 |
} else {
|
149 |
((DefaultInstallPackageWizard) abstractInstallPackageWizard) |
150 |
.doAction(direccion); |
151 |
} |
152 |
} |
153 |
|
154 |
private String getText(String key) { |
155 |
return this.swingInstallerManager.getText(key); |
156 |
} |
157 |
|
158 |
private static class UpdatePanel extends AbstractMonitorableTask { |
159 |
|
160 |
private final DownloadProgressWizard panel; |
161 |
|
162 |
public UpdatePanel(DownloadProgressWizard panel) {
|
163 |
super(panel.getText("_downloading_files")); |
164 |
// super(panel.getText("_downloading"));
|
165 |
this.panel = panel;
|
166 |
} |
167 |
|
168 |
@Override
|
169 |
public synchronized void run() { |
170 |
SimpleTaskStatus taskStatus = |
171 |
(SimpleTaskStatus) this.getTaskStatus();
|
172 |
|
173 |
List<String> not_downloaded = new ArrayList<String>(); |
174 |
|
175 |
try {
|
176 |
|
177 |
List<PackageInfo> installersToInstall =
|
178 |
this.panel.abstractInstallPackageWizard
|
179 |
.getInstallersToInstall(); |
180 |
this.panel.startDownload.setEnabled(false); |
181 |
this.panel.abstractInstallPackageWizard
|
182 |
.setBackButtonEnabled(false);
|
183 |
InstallPackageService installerService = |
184 |
this.panel.abstractInstallPackageWizard
|
185 |
.getInstallerExecutionService(); |
186 |
|
187 |
|
188 |
|
189 |
for (int i = 0; i < installersToInstall.size(); i++) { |
190 |
taskStatus.setCurValue(0);
|
191 |
PackageInfo installerInfo = installersToInstall.get(i); |
192 |
taskStatus.message(installerInfo.getName() + " " + (i + 1) |
193 |
+ " / " + installersToInstall.size());
|
194 |
if (installerInfo.getDownloadURL() != null) { |
195 |
try {
|
196 |
installerService.downloadPackage(installerInfo, |
197 |
taskStatus); |
198 |
} catch (Exception e) { |
199 |
|
200 |
not_downloaded.add(installerInfo.getName()); |
201 |
String msg =
|
202 |
panel.swingInstallerManager |
203 |
.getText("_Cant_download_package_files")
|
204 |
+ " " + installerInfo.getName();
|
205 |
logger.info(msg, e); |
206 |
} |
207 |
} |
208 |
|
209 |
if (not_downloaded.size() > 0) { |
210 |
break;
|
211 |
} |
212 |
} |
213 |
|
214 |
this.panel.abstractInstallPackageWizard
|
215 |
.setBackButtonEnabled(true);
|
216 |
|
217 |
} finally {
|
218 |
if (not_downloaded.size() == 0) { |
219 |
// Set the finished text
|
220 |
taskStatus.message(panel.getText("_finished"));
|
221 |
taskStatus.terminate(); |
222 |
taskStatus.remove(); |
223 |
this.panel.abstractInstallPackageWizard
|
224 |
.setNextButtonEnabled(true);
|
225 |
} else {
|
226 |
taskStatus.message(panel.getText("_finished_Some_files_not_downloaded"));
|
227 |
taskStatus.terminate(); |
228 |
taskStatus.remove(); |
229 |
this.panel.abstractInstallPackageWizard
|
230 |
.setNextButtonEnabled(false);
|
231 |
showNotDownloaded(this.panel, not_downloaded);
|
232 |
} |
233 |
|
234 |
} |
235 |
} |
236 |
|
237 |
} |
238 |
|
239 |
|
240 |
private static synchronized void showNotDownloaded( |
241 |
final Component parentPanel, final List<String> names) { |
242 |
|
243 |
int sz = names.size();
|
244 |
String list_str = ""; |
245 |
for (int i=0; i<sz; i++) { |
246 |
list_str = list_str + "\n - " + names.get(i);
|
247 |
} |
248 |
|
249 |
list_str = Messages.getText("_These_packages_not_downloaded")
|
250 |
+ ":\n" + list_str + "\n"; |
251 |
final String list_str_copy = list_str; |
252 |
if (!SwingUtilities.isEventDispatchThread()) { |
253 |
try {
|
254 |
SwingUtilities.invokeLater(new Runnable() { |
255 |
public void run() { |
256 |
|
257 |
JOptionPane.showMessageDialog(
|
258 |
parentPanel, list_str_copy, |
259 |
Messages.getText("_Download_error"),
|
260 |
JOptionPane.ERROR_MESSAGE);
|
261 |
|
262 |
} |
263 |
}); |
264 |
} catch (Throwable e) { |
265 |
logger.info("Error while showing message dialog. ", e);
|
266 |
} |
267 |
|
268 |
} else {
|
269 |
JOptionPane.showMessageDialog(
|
270 |
parentPanel, list_str, Messages.getText("_Download_error"),
|
271 |
JOptionPane.ERROR_MESSAGE);
|
272 |
} |
273 |
|
274 |
} |
275 |
|
276 |
|
277 |
|
278 |
private static synchronized int showOptionDialog( |
279 |
final Component parentComponent, |
280 |
final String message, |
281 |
final String title, |
282 |
final int optionType, |
283 |
final int messageType) { |
284 |
|
285 |
if (!SwingUtilities.isEventDispatchThread()) { |
286 |
try {
|
287 |
final int[] resp = new int[1]; |
288 |
SwingUtilities.invokeAndWait(new Runnable() { |
289 |
|
290 |
public void run() { |
291 |
resp[0] = showOptionDialog(
|
292 |
parentComponent, message, title, optionType, messageType); |
293 |
|
294 |
} |
295 |
}); |
296 |
|
297 |
return resp[0]; |
298 |
} catch (Throwable e) { |
299 |
logger.info("Error while showing message dialog. ", e);
|
300 |
} |
301 |
} |
302 |
|
303 |
return JOptionPane.showOptionDialog( |
304 |
parentComponent, message, title, |
305 |
optionType, messageType, null, null, null); |
306 |
|
307 |
} |
308 |
|
309 |
|
310 |
|
311 |
} |