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 / ProgressWizard.java @ 41718

History | View | Annotate | Download (6.7 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
/*
25
 * AUTHORS (In addition to CIT):
26
 * 2010 {Prodevelop}   {Task}
27
 */
28
package org.gvsig.installer.swing.impl.execution.wizard;
29

    
30
import java.lang.reflect.InvocationTargetException;
31
import java.util.Collections;
32
import java.util.Comparator;
33
import java.util.List;
34
import java.util.logging.Level;
35
import java.util.logging.Logger;
36
import javax.swing.JOptionPane;
37

    
38
import javax.swing.JPanel;
39
import javax.swing.SwingUtilities;
40

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

    
53
/**
54
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
55
 */
56
public class ProgressWizard extends DefaultProgressPanel implements OptionPanel {
57

    
58
    private static final long serialVersionUID = 8531884535246881448L;
59
    private AbstractInstallPackageWizard installerExecutionWizard;
60
    private DefaultSwingInstallerManager swingInstallerManager;
61

    
62
    public ProgressWizard(DefaultInstallPackageWizard installerExecutionWizard) {
63
        super();
64
        this.installerExecutionWizard = installerExecutionWizard;
65
        swingInstallerManager = (DefaultSwingInstallerManager) SwingInstallerLocator
66
                .getSwingInstallerManager();
67
    }
68

    
69
    public JPanel getJPanel() {
70
        return this;
71
    }
72

    
73
    public String getPanelTitle() {
74
        return swingInstallerManager.getText("_progress");
75
    }
76

    
77
    public void lastPanel() {
78
        installerExecutionWizard.setFinishButtonVisible(false);
79
        installerExecutionWizard.setCancelButtonEnabled(true);
80
    }
81

    
82
    public void nextPanel() {
83
        // Nothing to do, no next panel
84
    }
85

    
86
    public void updatePanel() {
87
        installerExecutionWizard.setFinishButtonVisible(false);
88
        installerExecutionWizard.setCancelButtonEnabled(false);
89

    
90
        final ProgressWizard wizard = this;
91

    
92
        UpdatePanel task = new UpdatePanel(wizard);
93
        wizard.installerExecutionWizard.addCancellableTask(task);
94
        bind(task.getTaskStatus());
95
        task.setDaemon(true);
96
        task.start();
97
    }
98

    
99
    private static class UpdatePanel extends AbstractMonitorableTask {
100

    
101
        private final ProgressWizard panel;
102

    
103
        public UpdatePanel(ProgressWizard panel) {
104
            super(panel.swingInstallerManager
105
                    .getText("_Install_package"));
106
            this.panel = panel;
107
        }
108

    
109
        private void showWarning(final String msg) {
110
            if( !SwingUtilities.isEventDispatchThread() )  {
111
                try {
112
                    SwingUtilities.invokeAndWait(new Runnable() {
113
                        public void run() {
114
                        }
115
                    });
116
                } catch (Exception ex) {
117
                    logger.warn("Cant show message to the user. "+msg);
118
                }
119
                return;
120
            }
121
            JOptionPane.showMessageDialog(panel, msg, "Warning", JOptionPane.WARNING_MESSAGE);
122
        }
123
        
124
        @Override
125
        public synchronized void run() {
126

    
127
            int errcount = 0;
128
            try {
129
                logger.info("Package installation iniitiated");
130
                SimpleTaskStatus taskStatus = (SimpleTaskStatus) this
131
                        .getTaskStatus();
132
                // bundle of packages
133
                InstallPackageService installerExecutionService = this.panel.installerExecutionWizard
134
                        .getInstallerExecutionService();
135
                // packages to install
136
                List<PackageInfo> installersToInstall = this.panel.installerExecutionWizard
137
                        .getInstallersToInstall();
138
                Collections.sort(installersToInstall, new Comparator<PackageInfo>() {
139
                    public int compare(PackageInfo o1, PackageInfo o2) {
140
                        return o1.getCode().compareTo(o2.getCode());
141
                    }
142
                });
143
                taskStatus.setRangeOfValues(0, installersToInstall.size());
144
                for ( int i = 0; i < installersToInstall.size(); i++ ) {
145
                    PackageInfo installerInfo = installersToInstall.get(i);
146
                    taskStatus.message(installerInfo.getName());
147
                    try {
148
                        installerExecutionService.installPackage(
149
                                panel.installerExecutionWizard.getApplicationFolder(),
150
                                installerInfo
151
                        );
152
                    } catch (Throwable th) {
153
                        logger.warn("Can't install package '" + installerInfo.getCode() + "'.", th);
154
                        errcount++;
155
                    }
156
                    taskStatus.setCurValue(i + 1);
157
                }
158
                this.panel.installerExecutionWizard.setFinishButtonVisible(true);
159
                if( errcount>0 ) {
160
                    showWarning("Failed to install "+errcount+" packages.");
161
                }
162
                taskStatus.message(panel.swingInstallerManager.getText("_Finished"));
163

    
164
                taskStatus.terminate();
165
                taskStatus.remove();
166
                logger.info("Package installation finished");
167
            } catch (Throwable th) {
168
                logger.warn("Problems install packages.",th);
169
                showWarning(panel.swingInstallerManager.getText("_Cant_install_packege"));
170
            }
171

    
172
        }
173

    
174
    }
175
}