svn-gvsig-desktop / tags / v2_0_0_Build_2050 / 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 @ 38692
History | View | Annotate | Download (9.43 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")); |
164 |
// super(panel.getText("_downloading"));
|
165 |
this.panel = panel;
|
166 |
} |
167 |
|
168 |
@Override
|
169 |
public synchronized void run() { |
170 |
List<PackageInfo> installersToInstall = this.panel.abstractInstallPackageWizard |
171 |
.getInstallersToInstall(); |
172 |
this.panel.startDownload.setEnabled(false); |
173 |
this.panel.abstractInstallPackageWizard.setBackButtonEnabled(false); |
174 |
InstallPackageService installerService = this.panel.abstractInstallPackageWizard
|
175 |
.getInstallerExecutionService(); |
176 |
SimpleTaskStatus taskStatus = (SimpleTaskStatus) this
|
177 |
.getTaskStatus(); |
178 |
|
179 |
List<String> not_downloaded = new ArrayList<String>(); |
180 |
|
181 |
for (int i = 0; i < installersToInstall.size(); i++) { |
182 |
taskStatus.setCurValue(0);
|
183 |
PackageInfo installerInfo = installersToInstall.get(i); |
184 |
taskStatus.message(installerInfo.getName() + " " + (i + 1) |
185 |
+ " / " + installersToInstall.size());
|
186 |
if (installerInfo.getDownloadURL() != null) { |
187 |
try {
|
188 |
installerService.downloadPackage(installerInfo, |
189 |
taskStatus); |
190 |
} catch (Exception e) { |
191 |
|
192 |
not_downloaded.add(installerInfo.getName()); |
193 |
String msg = panel.swingInstallerManager
|
194 |
.getText("_Cant_download_package_files")
|
195 |
+ " " + installerInfo.getName();
|
196 |
logger.info(msg, e); |
197 |
} |
198 |
} |
199 |
|
200 |
if (not_downloaded.size() > 0) { |
201 |
break;
|
202 |
} |
203 |
} |
204 |
|
205 |
this.panel.abstractInstallPackageWizard.setBackButtonEnabled(true); |
206 |
|
207 |
if (not_downloaded.size() == 0) { |
208 |
// Set the finished text
|
209 |
taskStatus.message(panel.getText("_finished"));
|
210 |
taskStatus.terminate(); |
211 |
taskStatus.remove(); |
212 |
this.panel.abstractInstallPackageWizard.setNextButtonEnabled(true); |
213 |
} else {
|
214 |
taskStatus.cancelRequest(); |
215 |
taskStatus.cancel(); |
216 |
// taskStatus.setCurValue(0);
|
217 |
taskStatus.remove(); |
218 |
this.panel.abstractInstallPackageWizard.setNextButtonEnabled(false); |
219 |
showNotDownloaded(this.panel, not_downloaded);
|
220 |
} |
221 |
|
222 |
} |
223 |
|
224 |
} |
225 |
|
226 |
|
227 |
private static synchronized void showNotDownloaded( |
228 |
final Component parentPanel, final List<String> names) { |
229 |
|
230 |
int sz = names.size();
|
231 |
String list_str = ""; |
232 |
for (int i=0; i<sz; i++) { |
233 |
list_str = list_str + "\n - " + names.get(i);
|
234 |
} |
235 |
|
236 |
list_str = Messages.getText("_These_packages_not_downloaded")
|
237 |
+ ":\n" + list_str + "\n"; |
238 |
final String list_str_copy = list_str; |
239 |
if (!SwingUtilities.isEventDispatchThread()) { |
240 |
try {
|
241 |
SwingUtilities.invokeLater(new Runnable() { |
242 |
public void run() { |
243 |
|
244 |
JOptionPane.showMessageDialog(
|
245 |
parentPanel, list_str_copy, |
246 |
Messages.getText("_Download_error"),
|
247 |
JOptionPane.ERROR_MESSAGE);
|
248 |
|
249 |
} |
250 |
}); |
251 |
} catch (Throwable e) { |
252 |
logger.info("Error while showing message dialog. ", e);
|
253 |
} |
254 |
|
255 |
} else {
|
256 |
JOptionPane.showMessageDialog(
|
257 |
parentPanel, list_str, Messages.getText("_Download_error"),
|
258 |
JOptionPane.ERROR_MESSAGE);
|
259 |
} |
260 |
|
261 |
} |
262 |
|
263 |
|
264 |
|
265 |
private static synchronized int showOptionDialog( |
266 |
final Component parentComponent, |
267 |
final String message, |
268 |
final String title, |
269 |
final int optionType, |
270 |
final int messageType) { |
271 |
|
272 |
if (!SwingUtilities.isEventDispatchThread()) { |
273 |
try {
|
274 |
final int[] resp = new int[1]; |
275 |
SwingUtilities.invokeAndWait(new Runnable() { |
276 |
|
277 |
public void run() { |
278 |
resp[0] = showOptionDialog(
|
279 |
parentComponent, message, title, optionType, messageType); |
280 |
|
281 |
} |
282 |
}); |
283 |
|
284 |
return resp[0]; |
285 |
} catch (Throwable e) { |
286 |
logger.info("Error while showing message dialog. ", e);
|
287 |
} |
288 |
} |
289 |
|
290 |
return JOptionPane.showOptionDialog( |
291 |
parentComponent, message, title, |
292 |
optionType, messageType, null, null, null); |
293 |
|
294 |
} |
295 |
|
296 |
|
297 |
|
298 |
} |