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 / DownloadProgressWizard.java @ 40560

History | View | Annotate | Download (10.4 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.installer.swing.impl.execution.wizard;
25

    
26
import java.awt.Component;
27
import java.awt.GridBagConstraints;
28
import java.awt.Insets;
29
import java.awt.event.ActionEvent;
30
import java.awt.event.ActionListener;
31
import java.util.ArrayList;
32
import java.util.List;
33

    
34
import javax.swing.JButton;
35
import javax.swing.JOptionPane;
36
import javax.swing.JPanel;
37
import javax.swing.SwingUtilities;
38

    
39
import org.slf4j.Logger;
40
import org.slf4j.LoggerFactory;
41

    
42
import org.gvsig.gui.beans.wizard.WizardPanelWithLogo;
43
import org.gvsig.gui.beans.wizard.panel.NotContinueWizardException;
44
import org.gvsig.gui.beans.wizard.panel.OptionPanel;
45
import org.gvsig.i18n.Messages;
46
import org.gvsig.installer.lib.api.PackageInfo;
47
import org.gvsig.installer.lib.api.execution.InstallPackageService;
48
import org.gvsig.installer.swing.api.SwingInstallerLocator;
49
import org.gvsig.installer.swing.api.execution.AbstractInstallPackageWizard;
50
import org.gvsig.installer.swing.impl.DefaultSwingInstallerManager;
51
import org.gvsig.installer.swing.impl.execution.DefaultInstallPackageWizard;
52
import org.gvsig.installer.swing.impl.panel.DefaultProgressPanel;
53
import org.gvsig.tools.task.AbstractMonitorableTask;
54
import org.gvsig.tools.task.SimpleTaskStatus;
55

    
56
/**
57
 * @author gvSIG Team
58
 * @version $Id$
59
 * 
60
 */
61

    
62
public class DownloadProgressWizard extends DefaultProgressPanel implements
63
                OptionPanel {
64

    
65
        private static final long serialVersionUID = -7064334977678611609L;
66
        private static Logger logger = LoggerFactory
67
                        .getLogger(DownloadProgressWizard.class);
68
        private AbstractInstallPackageWizard abstractInstallPackageWizard;
69
        // private abstractInstallPackageWizard abstractInstallPackageWizard;
70
        public JButton startDownload;
71
        private int direccion = WizardPanelWithLogo.ACTION_NEXT;
72
        private DefaultSwingInstallerManager swingInstallerManager;
73

    
74
        public DownloadProgressWizard(
75
                        AbstractInstallPackageWizard abstractInstallPackageWizard) {
76
                super();
77
                this.abstractInstallPackageWizard = abstractInstallPackageWizard;
78
        }
79

    
80
        @Override
81
        protected void initComponents() {
82
                super.initComponents();
83

    
84
                final DownloadProgressWizard wizard = this;
85

    
86
                swingInstallerManager = (DefaultSwingInstallerManager) SwingInstallerLocator
87
                                .getSwingInstallerManager();
88

    
89
                startDownload = new JButton(swingInstallerManager
90
                                .getText("_start_download"));
91

    
92
                java.awt.GridBagConstraints gridBagConstraints;
93
                gridBagConstraints = new GridBagConstraints();
94
                gridBagConstraints.gridx = 0;
95
                gridBagConstraints.gridy = 3;
96
                gridBagConstraints.anchor = GridBagConstraints.EAST;
97
                gridBagConstraints.insets = new Insets(5, 2, 2, 2);
98
                add(startDownload, gridBagConstraints);
99

    
100
                startDownload.addActionListener(new ActionListener() {
101

    
102
                        public void actionPerformed(ActionEvent e) {
103
                                UpdatePanel task = new UpdatePanel(wizard);
104
                                wizard.abstractInstallPackageWizard.addCancellableTask(task);
105
                                bind(task.getTaskStatus());
106
                                task.start();
107
                        }
108
                });
109
        }
110

    
111
        public JPanel getJPanel() {
112
                return this;
113
        }
114

    
115
        public String getPanelTitle() {
116
                return swingInstallerManager.getText("_download_progress");
117
        }
118

    
119
        public void lastPanel() {
120
                abstractInstallPackageWizard.setFinishButtonVisible(false);
121
                abstractInstallPackageWizard.setCancelButtonEnabled(true);
122
                direccion = WizardPanelWithLogo.ACTION_NEXT;
123
        }
124

    
125
        public void nextPanel() throws NotContinueWizardException {
126
                direccion = WizardPanelWithLogo.ACTION_PREVIOUS;
127
        }
128

    
129
        public void updatePanel() {
130
                boolean thereIsDownloablePackage = false;
131

    
132
                InstallPackageService service = abstractInstallPackageWizard
133
                                .getInstallerExecutionService();
134
                service.getPackageCount();
135

    
136
                for (int i = 0; i < abstractInstallPackageWizard
137
                                .getInstallersToInstall().size(); i++) {
138

    
139
                        if (abstractInstallPackageWizard.getInstallersToInstall().get(i)
140
                                        .getDownloadURL() != null) {
141
                                thereIsDownloablePackage = true;
142
                                break;
143
                        }
144
                }
145

    
146
                if (thereIsDownloablePackage) {
147
                        this.abstractInstallPackageWizard.setNextButtonEnabled(false);
148
                        this.startDownload.setEnabled(true);
149
                } else {
150
                        ((DefaultInstallPackageWizard) abstractInstallPackageWizard)
151
                                        .doAction(direccion);
152
                }
153
        }
154

    
155
        private String getText(String key) {
156
                return this.swingInstallerManager.getText(key);
157
        }
158

    
159
        private static class UpdatePanel extends AbstractMonitorableTask {
160

    
161
                private final DownloadProgressWizard panel;
162

    
163
                public UpdatePanel(DownloadProgressWizard panel) {
164
            super(panel.getText("_downloading_files"));
165
                        // super(panel.getText("_downloading"));
166
                        this.panel = panel;
167
                }
168

    
169
                @Override
170
        public synchronized void run() {
171
            SimpleTaskStatus taskStatus =
172
                (SimpleTaskStatus) this.getTaskStatus();
173

    
174
            List<String> not_downloaded = new ArrayList<String>();
175
            
176
            try {
177

    
178
                List<PackageInfo> installersToInstall =
179
                    this.panel.abstractInstallPackageWizard
180
                        .getInstallersToInstall();
181
                this.panel.startDownload.setEnabled(false);
182
                this.panel.abstractInstallPackageWizard
183
                    .setBackButtonEnabled(false);
184
                InstallPackageService installerService =
185
                    this.panel.abstractInstallPackageWizard
186
                        .getInstallerExecutionService();
187

    
188
                
189

    
190
                for (int i = 0; i < installersToInstall.size(); i++) {
191
                    taskStatus.setCurValue(0);
192
                    PackageInfo installerInfo = installersToInstall.get(i);
193
                    taskStatus.message(installerInfo.getName() + " " + (i + 1)
194
                        + " / " + installersToInstall.size());
195
                    if (installerInfo.getDownloadURL() != null) {
196
                        try {
197
                            installerService.downloadPackage(installerInfo,
198
                                taskStatus);
199
                        } catch (Exception e) {
200

    
201
                            not_downloaded.add(installerInfo.getName());
202
                            String msg =
203
                                panel.swingInstallerManager
204
                                    .getText("_Cant_download_package_files")
205
                                    + " " + installerInfo.getName();
206
                            logger.info(msg, e);
207
                        }
208
                    }
209

    
210
                    if (not_downloaded.size() > 0) {
211
                        break;
212
                    }
213
                }
214

    
215
                this.panel.abstractInstallPackageWizard
216
                    .setBackButtonEnabled(true);
217

    
218
            } finally {
219
                if (not_downloaded.size() == 0) {
220
                    // Set the finished text
221
                    taskStatus.message(panel.getText("_finished"));
222
                    taskStatus.terminate();
223
                    taskStatus.remove();
224
                    this.panel.abstractInstallPackageWizard
225
                        .setNextButtonEnabled(true);
226
                } else {
227
                    taskStatus.message(panel.getText("_finished_Some_files_not_downloaded"));
228
                    taskStatus.terminate();
229
                    taskStatus.remove();
230
                    this.panel.abstractInstallPackageWizard
231
                        .setNextButtonEnabled(false);
232
                    showNotDownloaded(this.panel, not_downloaded);
233
                }            
234
                
235
            }
236
        }
237

    
238
        }
239
        
240
        
241
           private static synchronized void showNotDownloaded(
242
               final Component parentPanel, final List<String> names) {
243
               
244
               int sz = names.size();
245
           String list_str = ""; 
246
               for (int i=0; i<sz; i++) {
247
                   list_str = list_str + "\n - " + names.get(i);
248
               }
249
               
250
               list_str = Messages.getText("_These_packages_not_downloaded")
251
                   + ":\n" + list_str + "\n";
252
               final String list_str_copy = list_str; 
253
               if (!SwingUtilities.isEventDispatchThread()) {
254
                    try {
255
                        SwingUtilities.invokeLater(new Runnable() {
256
                            public void run() {
257
                                
258
                                JOptionPane.showMessageDialog(
259
                                    parentPanel, list_str_copy,
260
                                    Messages.getText("_Download_error"),
261
                                    JOptionPane.ERROR_MESSAGE);
262
                                
263
                            }
264
                        });
265
                    } catch (Throwable e) {
266
                        logger.info("Error while showing message dialog. ", e);
267
                    }
268
                    
269
                } else {
270
                    JOptionPane.showMessageDialog(
271
                        parentPanel, list_str, Messages.getText("_Download_error"),
272
                        JOptionPane.ERROR_MESSAGE);
273
                }
274
                
275
            }
276
           
277
           
278
        
279
        private static synchronized int showOptionDialog(
280
            final Component parentComponent,
281
            final String message,
282
            final String title,
283
            final int optionType,
284
            final int messageType) {
285

    
286
        if (!SwingUtilities.isEventDispatchThread()) {
287
            try {
288
                final int[] resp = new int[1];
289
                SwingUtilities.invokeAndWait(new Runnable() {
290

    
291
                    public void run() {
292
                        resp[0] = showOptionDialog(
293
                            parentComponent, message, title, optionType, messageType);
294
                        
295
                    }
296
                });
297
                
298
                return resp[0];
299
            } catch (Throwable e) {
300
                logger.info("Error while showing message dialog. ", e);
301
            }
302
        }
303
        
304
            return JOptionPane.showOptionDialog(
305
                parentComponent, message, title,
306
                optionType, messageType, null, null, null);
307
            
308
        }
309
        
310
        
311

    
312
}