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 @ 44420

History | View | Annotate | Download (5.74 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.api.packagebuilder.PackageBuildder;
46
import org.gvsig.installer.swing.api.packagebuilder.PackageBuildder.BeforePackingListener;
47
import org.gvsig.installer.swing.impl.packagebuilder.BasePackageWizard;
48
import org.gvsig.tools.ToolsLocator;
49
import org.gvsig.tools.task.SimpleTaskStatus;
50

    
51
public class ProgressOption implements OptionPanel {
52

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

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

    
65
    private String getText(String msg) {
66
        return Messages.getText(msg);
67
    }
68

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

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

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

    
85
    @Override
86
    public void nextPanel() {
87
        // Do nothing
88
    }
89

    
90
    @Override
91
    public void updatePanel() {
92

    
93
        for (BeforePackingListener beforePackingListener : this.wizard.getBeforePackingListeners()) {
94
            try {
95
                beforePackingListener.perfrom(this.wizard);
96
            } catch(Throwable th) {
97
                
98
            }
99
        }
100
        
101
        OutputStream outputStream =
102
            openFileOutputStream(wizard.getPackageFile());
103
        if (outputStream == null) {
104
            return;
105
        }
106
        OutputStream indexOutputStream = null;
107
        if (wizard.shouldCreateIndex()) {
108
            indexOutputStream =
109
                openFileOutputStream(wizard.getPackageIndexFile());
110
            if (indexOutputStream == null) {
111
                return;
112
            }
113
        }
114

    
115
        wizard.setFinishButtonEnabled(false);
116
        wizard.setCancelButtonEnabled(false);
117

    
118
        SimpleTaskStatus taskStatus =
119
            ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus(
120
                wizard.getPackageInfo().getName());
121

    
122
        this.progressPanel.bind(taskStatus);
123

    
124
        try {
125
            String downloadURL = wizard.getPackageInfo().getDownloadURLAsString();
126
            wizard.getPackageInfo().setDownloadURL("");
127
            
128
            MakePackageService makePackageService =
129
                InstallerLocator.getInstallerManager().createMakePackage(
130
                    wizard.getFolderToPackaging(), wizard.getPackageInfo());
131

    
132
            taskStatus.message(getText("_Compressing"));
133

    
134
            makePackageService.preparePackage();
135
            makePackageService.createPackage(outputStream);
136
            if (indexOutputStream != null) {
137
                taskStatus.message(getText("_Creating_index"));
138
                makePackageService.createPackageIndex(
139
                        wizard.getDownloadURL(), indexOutputStream);
140
            }
141
            
142
            wizard.getPackageInfo().setDownloadURL(downloadURL);
143
            taskStatus.terminate();
144

    
145
            // Set the finished text
146
            taskStatus.message(getText("_Finished"));
147
        } catch (MakePackageServiceException e) {
148
            logger.info("Error while creating package.", e);
149
            JOptionPane.showMessageDialog(null, getText("_cant_create_the_package")
150
                + "\n\n" + e.getLocalizedMessageStack(),
151
                getText("_create_package"), JOptionPane.ERROR_MESSAGE);
152
        }
153
        wizard.setFinishButtonEnabled(true);
154
        wizard.setCancelButtonEnabled(false);
155
    }
156

    
157
    private OutputStream openFileOutputStream(File file) {
158
        OutputStream os;
159

    
160
        try {
161
            os = new FileOutputStream(file);
162
        } catch (FileNotFoundException e1) {
163
            JOptionPane.showMessageDialog(null,
164
                getText("_cant_create_the_package") + " (" + file + ").",
165
                getText("_create_package"), JOptionPane.WARNING_MESSAGE);
166
            return null;
167
        }
168
        return os;
169
    }
170
}