Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.installer / org.gvsig.installer.swing / org.gvsig.installer.swing.impl / src / main / java / org / gvsig / installer / swing / impl / execution / wizard / DownloadProgressWizardPage.java @ 43126

History | View | Annotate | Download (7.83 KB)

1 40560 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40560 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5 40435 jjdelcerro
 *
6 43126 jjdelcerro
 * This program is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10 40435 jjdelcerro
 *
11 43126 jjdelcerro
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15 40435 jjdelcerro
 *
16 43126 jjdelcerro
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 40435 jjdelcerro
 *
20 43126 jjdelcerro
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22 40435 jjdelcerro
 */
23
package org.gvsig.installer.swing.impl.execution.wizard;
24
25
import java.util.ArrayList;
26
import java.util.List;
27
28
import javax.swing.JOptionPane;
29
import javax.swing.JPanel;
30
import javax.swing.SwingUtilities;
31
32
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
34
35
import org.gvsig.gui.beans.wizard.WizardPanelWithLogo;
36
import org.gvsig.gui.beans.wizard.panel.NotContinueWizardException;
37
import org.gvsig.gui.beans.wizard.panel.OptionPanel;
38
import org.gvsig.i18n.Messages;
39
import org.gvsig.installer.lib.api.PackageInfo;
40
import org.gvsig.installer.lib.api.execution.InstallPackageService;
41
import org.gvsig.installer.swing.api.SwingInstallerLocator;
42 43126 jjdelcerro
import org.gvsig.installer.swing.api.SwingInstallerManager;
43 40435 jjdelcerro
import org.gvsig.installer.swing.impl.DefaultSwingInstallerManager;
44 43126 jjdelcerro
import org.gvsig.installer.swing.impl.execution.DefaultInstallWizardPanel;
45
import org.gvsig.installer.swing.impl.execution.InstallWizardPanel_ext;
46 40435 jjdelcerro
import org.gvsig.installer.swing.impl.panel.DefaultProgressPanel;
47 43126 jjdelcerro
import org.gvsig.tools.swing.api.ToolsSwingLocator;
48 40435 jjdelcerro
import org.gvsig.tools.task.AbstractMonitorableTask;
49
import org.gvsig.tools.task.SimpleTaskStatus;
50
51
/**
52
 * @author gvSIG Team
53
 * @version $Id$
54 43126 jjdelcerro
 *
55 40435 jjdelcerro
 */
56 43126 jjdelcerro
public class DownloadProgressWizardPage extends DefaultProgressPanel implements
57
        OptionPanel {
58 40435 jjdelcerro
59 43126 jjdelcerro
    private static final long serialVersionUID = -7064334977678611609L;
60
    private static final Logger logger = LoggerFactory.getLogger(DownloadProgressWizardPage.class);
61 40435 jjdelcerro
62 43126 jjdelcerro
    private final InstallWizardPanel_ext wizardPanel;
63 40435 jjdelcerro
64 43126 jjdelcerro
    public DownloadProgressWizardPage(InstallWizardPanel_ext wizardPanel) {
65
        super();
66
        this.wizardPanel = wizardPanel;
67
    }
68 40435 jjdelcerro
69 43126 jjdelcerro
    @Override
70
    public JPanel getJPanel() {
71
        return this;
72
    }
73 40435 jjdelcerro
74 43126 jjdelcerro
    @Override
75
    public String getPanelTitle() {
76
        return wizardPanel.getTranslation("_download_progress");
77
    }
78 40435 jjdelcerro
79 43126 jjdelcerro
    @Override
80
    public void lastPanel() {
81
        wizardPanel.setFinishButtonEnabled(false);
82
        wizardPanel.setCancelButtonEnabled(true);
83
    }
84 40435 jjdelcerro
85 43126 jjdelcerro
    @Override
86
    public void nextPanel() throws NotContinueWizardException {
87
    }
88 40435 jjdelcerro
89 43126 jjdelcerro
    @Override
90
    public void updatePanel() {
91
        if( wizardPanel.getDirection() == WizardPanelWithLogo.ACTION_PREVIOUS ) {
92
            wizardPanel.skip();
93
            return;
94
        }
95
        boolean AreTherePackagesToDownload = false;
96 40435 jjdelcerro
97 43126 jjdelcerro
        this.wizardPanel.setNextButtonEnabled(false);
98
        this.wizardPanel.setBackButtonEnabled(false);
99
        this.wizardPanel.setFinishButtonEnabled(false);
100 40435 jjdelcerro
101 43126 jjdelcerro
        InstallPackageService service = wizardPanel.getInstallerExecutionService();
102
        service.getPackageCount();
103 40435 jjdelcerro
104 43126 jjdelcerro
        List<PackageInfo> packagesToInstall = wizardPanel.getPackagesToInstall();
105
        for (PackageInfo packageToInstall : packagesToInstall) {
106
            if( packageToInstall.getDownloadURL() != null ) {
107
                AreTherePackagesToDownload = true;
108
                break;
109
            }
110
        }
111 40435 jjdelcerro
112 43126 jjdelcerro
        if (!AreTherePackagesToDownload) {
113
            wizardPanel.skip();
114
        }
115
116
        UpdatePanel task = new UpdatePanel(this);
117
        wizardPanel.addCancellableTask(task);
118
        bind(task.getTaskStatus());
119
        task.setDaemon(true);
120
        task.start();
121
122
    }
123 40435 jjdelcerro
124 43126 jjdelcerro
    private String getText(String key) {
125
        return this.wizardPanel.getTranslation(key);
126
    }
127 40435 jjdelcerro
128 43126 jjdelcerro
    private static class UpdatePanel extends AbstractMonitorableTask {
129 40435 jjdelcerro
130 43126 jjdelcerro
        private final DownloadProgressWizardPage panel;
131 40435 jjdelcerro
132 43126 jjdelcerro
        public UpdatePanel(DownloadProgressWizardPage panel) {
133 40435 jjdelcerro
            super(panel.getText("_downloading_files"));
134 43126 jjdelcerro
            this.panel = panel;
135
        }
136 40435 jjdelcerro
137 43126 jjdelcerro
        @Override
138 40435 jjdelcerro
        public synchronized void run() {
139 43126 jjdelcerro
            SimpleTaskStatus taskStatus = (SimpleTaskStatus) this.getTaskStatus();
140
            final List<String> not_downloaded = new ArrayList<>();
141 40435 jjdelcerro
            try {
142
143 43126 jjdelcerro
                List<PackageInfo> packagesToInstall = this.panel.wizardPanel.getPackagesToInstall();
144
                InstallPackageService installerService = this.panel.wizardPanel.getInstallerExecutionService();
145 40435 jjdelcerro
146 43126 jjdelcerro
                taskStatus.setRangeOfValues(0, packagesToInstall.size());
147
                for (int i = 0; i < packagesToInstall.size(); i++) {
148
                    taskStatus.setCurValue(i);
149
                    PackageInfo packageInfo = packagesToInstall.get(i);
150
                    taskStatus.message(packageInfo.getName());
151
                    if (packageInfo.getDownloadURL() != null) {
152 40435 jjdelcerro
                        try {
153 43126 jjdelcerro
                            installerService.downloadPackage(
154
                                    packageInfo,
155
                                    taskStatus
156
                            );
157 40435 jjdelcerro
                        } catch (Exception e) {
158 43126 jjdelcerro
                            not_downloaded.add(packageInfo.getName());
159
                            logger.info("Can't download package '"+packageInfo.getName()+"'.", e);
160 40435 jjdelcerro
                        }
161
                    }
162
163
                    if (not_downloaded.size() > 0) {
164
                        break;
165
                    }
166
                }
167 43126 jjdelcerro
                SwingUtilities.invokeAndWait(new Runnable() {
168 40435 jjdelcerro
169 43126 jjdelcerro
                    @Override
170
                    public void run() {
171
                        if (not_downloaded.isEmpty()) {
172
                            panel.wizardPanel.setNextButtonEnabled(true);
173
                            panel.wizardPanel.doAction(WizardPanelWithLogo.ACTION_NEXT);
174
                        } else {
175
                            panel.wizardPanel.setNextButtonEnabled(false);
176
                        }
177
                        panel.wizardPanel.setBackButtonEnabled(true);
178
                    }
179
                });
180 40435 jjdelcerro
181 43126 jjdelcerro
            } catch(Exception ex) {
182
                logger.warn("Problems downloading packages.", ex);
183
                ToolsSwingLocator.getThreadSafeDialogsManager().messageDialog(
184
                        panel.getText("_Cant_download_packages"),
185
                        panel.getText("_Warning"),
186
                        JOptionPane.WARNING_MESSAGE
187
                );
188 40435 jjdelcerro
            } finally {
189 43126 jjdelcerro
                if (not_downloaded.isEmpty()) {
190 40435 jjdelcerro
                    taskStatus.message(panel.getText("_finished"));
191
                } else {
192
                    taskStatus.message(panel.getText("_finished_Some_files_not_downloaded"));
193 43126 jjdelcerro
                    showNotDownloaded(not_downloaded);
194
                }
195
                taskStatus.terminate();
196
                taskStatus.remove();
197 40435 jjdelcerro
            }
198
        }
199
200 43126 jjdelcerro
    }
201 40435 jjdelcerro
202 43126 jjdelcerro
    private static synchronized void showNotDownloaded(final List<String> names) {
203 40435 jjdelcerro
204 43126 jjdelcerro
        int sz = names.size();
205
        String list_str = "";
206
        for (int i = 0; i < sz; i++) {
207
            list_str = list_str + "\n - " + names.get(i);
208 40435 jjdelcerro
        }
209
210 43126 jjdelcerro
        list_str = Messages.getText("_These_packages_not_downloaded")
211
                + ":\n" + list_str + "\n";
212
        ToolsSwingLocator.getThreadSafeDialogsManager().messageDialog(
213
                list_str,
214
                Messages.getText("_Warning"),
215
                JOptionPane.WARNING_MESSAGE
216
        );
217
    }
218
219
220 40435 jjdelcerro
}