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 / packagebuilder / options / ProgressOption.java @ 42528

History | View | Annotate | Download (5.32 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
package org.gvsig.installer.swing.impl.packagebuilder.options;
26

    
27
import java.io.File;
28
import java.io.FileNotFoundException;
29
import java.io.FileOutputStream;
30
import java.io.OutputStream;
31

    
32
import javax.swing.JOptionPane;
33
import javax.swing.JPanel;
34

    
35
import org.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
37

    
38
import org.gvsig.gui.beans.wizard.panel.OptionPanel;
39
import org.gvsig.i18n.Messages;
40
import org.gvsig.installer.lib.api.InstallerLocator;
41
import org.gvsig.installer.lib.api.creation.MakePackageService;
42
import org.gvsig.installer.lib.api.creation.MakePackageServiceException;
43
import org.gvsig.installer.swing.api.JProgressPanel;
44
import org.gvsig.installer.swing.api.SwingInstallerLocator;
45
import org.gvsig.installer.swing.impl.packagebuilder.BasePackageWizard;
46
import org.gvsig.tools.ToolsLocator;
47
import org.gvsig.tools.task.SimpleTaskStatus;
48

    
49
public class ProgressOption implements OptionPanel {
50

    
51
    private static final Logger logger = LoggerFactory.getLogger(ProgressOption.class);
52
    private final BasePackageWizard wizard;
53
    private final JProgressPanel progressPanel;
54

    
55
    public ProgressOption(BasePackageWizard wizard) {
56
        super();
57
        this.wizard = wizard;
58
        progressPanel =
59
            SwingInstallerLocator.getSwingInstallerManager()
60
                .createProgressPanel();
61
    }
62

    
63
    private String getText(String msg) {
64
        return Messages.getText(msg);
65
    }
66

    
67
    @Override
68
    public JPanel getJPanel() {
69
        return this.progressPanel;
70
    }
71

    
72
    @Override
73
    public String getPanelTitle() {
74
        return Messages.getText("_Installer_progress");
75
    }
76

    
77
    @Override
78
    public void lastPanel() {
79
        wizard.setFinishButtonEnabled(false);
80
        wizard.setCancelButtonEnabled(true);
81
    }
82

    
83
    @Override
84
    public void nextPanel() {
85
        // Do nothing
86
    }
87

    
88
    @Override
89
    public void updatePanel() {
90

    
91
        OutputStream outputStream =
92
            openFileOutputStream(wizard.getPackageFile());
93
        if (outputStream == null) {
94
            return;
95
        }
96
        OutputStream indexOutputStream = null;
97
        if (wizard.shouldCreateIndex()) {
98
            indexOutputStream =
99
                openFileOutputStream(wizard.getPackageIndexFile());
100
            if (indexOutputStream == null) {
101
                return;
102
            }
103
        }
104

    
105
        wizard.setFinishButtonEnabled(false);
106
        wizard.setCancelButtonEnabled(false);
107

    
108
        SimpleTaskStatus taskStatus =
109
            ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus(
110
                wizard.getPackageInfo().getName());
111

    
112
        this.progressPanel.bind(taskStatus);
113

    
114
        try {
115
            String downloadURL = wizard.getPackageInfo().getDownloadURLAsString();
116
            wizard.getPackageInfo().setDownloadURL("");
117
            
118
            MakePackageService makePackageService =
119
                InstallerLocator.getInstallerManager().createMakePackage(
120
                    wizard.getFolderToPackaging(), wizard.getPackageInfo());
121

    
122
            taskStatus.message(getText("_Compressing"));
123

    
124
            makePackageService.preparePackage();
125
            makePackageService.createPackage(outputStream);
126
            if (indexOutputStream != null) {
127
                taskStatus.message(getText("_Creating_index"));
128
                makePackageService.createPackageIndex(
129
                        wizard.getDownloadURL(), indexOutputStream);
130
            }
131
            
132
            wizard.getPackageInfo().setDownloadURL(downloadURL);
133
            taskStatus.terminate();
134

    
135
            // Set the finished text
136
            taskStatus.message(getText("_Finished"));
137
        } catch (MakePackageServiceException e) {
138
            logger.info("Error while creating package.", e);
139
            JOptionPane.showMessageDialog(null, getText("_cant_create_the_package")
140
                + "\n\n" + e.getLocalizedMessageStack(),
141
                getText("_create_package"), JOptionPane.ERROR_MESSAGE);
142
        }
143
        wizard.setFinishButtonEnabled(true);
144
        wizard.setCancelButtonEnabled(false);
145
    }
146

    
147
    private OutputStream openFileOutputStream(File file) {
148
        OutputStream os;
149

    
150
        try {
151
            os = new FileOutputStream(file);
152
        } catch (FileNotFoundException e1) {
153
            JOptionPane.showMessageDialog(null,
154
                getText("_cant_create_the_package") + " (" + file + ").",
155
                getText("_create_package"), JOptionPane.WARNING_MESSAGE);
156
            return null;
157
        }
158
        return os;
159
    }
160
}