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 40556 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40556 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6 40435 jjdelcerro
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8 40556 jjdelcerro
 * as published by the Free Software Foundation; either version 3
9 40435 jjdelcerro
 * of the License, or (at your option) any later version.
10 40556 jjdelcerro
 *
11 40435 jjdelcerro
 * 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 40556 jjdelcerro
 *
16 40435 jjdelcerro
 * 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 40556 jjdelcerro
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 40435 jjdelcerro
 * MA  02110-1301, USA.
20 40556 jjdelcerro
 *
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 40435 jjdelcerro
 */
24
25 42528 jjdelcerro
package org.gvsig.installer.swing.impl.packagebuilder.options;
26 41280 jjdelcerro
27 40435 jjdelcerro
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 44420 jjdelcerro
import org.gvsig.installer.swing.api.packagebuilder.PackageBuildder;
46
import org.gvsig.installer.swing.api.packagebuilder.PackageBuildder.BeforePackingListener;
47 42528 jjdelcerro
import org.gvsig.installer.swing.impl.packagebuilder.BasePackageWizard;
48 40435 jjdelcerro
import org.gvsig.tools.ToolsLocator;
49
import org.gvsig.tools.task.SimpleTaskStatus;
50
51 41280 jjdelcerro
public class ProgressOption implements OptionPanel {
52 40435 jjdelcerro
53 41280 jjdelcerro
    private static final Logger logger = LoggerFactory.getLogger(ProgressOption.class);
54 42528 jjdelcerro
    private final BasePackageWizard wizard;
55
    private final JProgressPanel progressPanel;
56 40435 jjdelcerro
57 42528 jjdelcerro
    public ProgressOption(BasePackageWizard wizard) {
58 40435 jjdelcerro
        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 42528 jjdelcerro
    @Override
70 40435 jjdelcerro
    public JPanel getJPanel() {
71
        return this.progressPanel;
72
    }
73
74 42528 jjdelcerro
    @Override
75 40435 jjdelcerro
    public String getPanelTitle() {
76
        return Messages.getText("_Installer_progress");
77
    }
78
79 42528 jjdelcerro
    @Override
80 40435 jjdelcerro
    public void lastPanel() {
81
        wizard.setFinishButtonEnabled(false);
82
        wizard.setCancelButtonEnabled(true);
83
    }
84
85 42528 jjdelcerro
    @Override
86 40435 jjdelcerro
    public void nextPanel() {
87
        // Do nothing
88
    }
89
90 42528 jjdelcerro
    @Override
91 40435 jjdelcerro
    public void updatePanel() {
92
93 44420 jjdelcerro
        for (BeforePackingListener beforePackingListener : this.wizard.getBeforePackingListeners()) {
94
            try {
95
                beforePackingListener.perfrom(this.wizard);
96
            } catch(Throwable th) {
97
98
            }
99
        }
100
101 40435 jjdelcerro
        OutputStream outputStream =
102 41280 jjdelcerro
            openFileOutputStream(wizard.getPackageFile());
103 40435 jjdelcerro
        if (outputStream == null) {
104
            return;
105
        }
106
        OutputStream indexOutputStream = null;
107 41280 jjdelcerro
        if (wizard.shouldCreateIndex()) {
108 40435 jjdelcerro
            indexOutputStream =
109 41280 jjdelcerro
                openFileOutputStream(wizard.getPackageIndexFile());
110 40435 jjdelcerro
            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 41280 jjdelcerro
                wizard.getPackageInfo().getName());
121 40435 jjdelcerro
122
        this.progressPanel.bind(taskStatus);
123
124
        try {
125 41284 jjdelcerro
            String downloadURL = wizard.getPackageInfo().getDownloadURLAsString();
126
            wizard.getPackageInfo().setDownloadURL("");
127
128 40435 jjdelcerro
            MakePackageService makePackageService =
129
                InstallerLocator.getInstallerManager().createMakePackage(
130 41280 jjdelcerro
                    wizard.getFolderToPackaging(), wizard.getPackageInfo());
131 40435 jjdelcerro
132
            taskStatus.message(getText("_Compressing"));
133
134
            makePackageService.preparePackage();
135
            makePackageService.createPackage(outputStream);
136
            if (indexOutputStream != null) {
137
                taskStatus.message(getText("_Creating_index"));
138 41280 jjdelcerro
                makePackageService.createPackageIndex(
139 41284 jjdelcerro
                        wizard.getDownloadURL(), indexOutputStream);
140 40435 jjdelcerro
            }
141 41284 jjdelcerro
142
            wizard.getPackageInfo().setDownloadURL(downloadURL);
143 40435 jjdelcerro
            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
}