001/*
002 * IzPack - Copyright 2001-2005 Julien Ponge, All Rights Reserved.
003 * 
004 * http://www.izforge.com/izpack/
005 * http://developer.berlios.de/projects/izpack/
006 * 
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 * 
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *     
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package com.izforge.izpack.panels;
021
022import java.awt.Dimension;
023import java.awt.GridBagConstraints;
024import java.awt.GridBagLayout;
025
026import javax.swing.JLabel;
027import javax.swing.JOptionPane;
028import javax.swing.JProgressBar;
029import javax.swing.JSeparator;
030
031import com.izforge.izpack.gui.LabelFactory;
032import com.izforge.izpack.installer.InstallData;
033import com.izforge.izpack.installer.InstallerFrame;
034import com.izforge.izpack.installer.IzPanel;
035import com.izforge.izpack.util.AbstractUIProgressHandler;
036
037/**
038 * The install panel class. Launches the actual installation job.
039 * 
040 * @author Julien Ponge
041 */
042public class InstallPanel extends IzPanel implements AbstractUIProgressHandler
043{
044
045    private static final long serialVersionUID = 3257282547959410992L;
046
047    /** The layout. */
048    private GridBagLayout layout;
049
050    /** The layout constraints. */
051    private GridBagConstraints gbConstraints;
052
053    /** The tip label. */
054    protected JLabel tipLabel;
055
056    /** The operation label . */
057    protected JLabel packOpLabel;
058
059    /** The operation label . */
060    protected JLabel overallOpLabel;
061
062    /** The pack progress bar. */
063    protected JProgressBar packProgressBar;
064
065    /** The progress bar. */
066    protected JProgressBar overallProgressBar;
067
068    /** True if the installation has been done. */
069    private volatile boolean validated = false;
070
071    /** How many packs we are going to install. */
072    private int noOfPacks = 0;
073
074    /**
075     * The constructor.
076     * 
077     * @param parent The parent window.
078     * @param idata The installation data.
079     */
080    public InstallPanel(InstallerFrame parent, InstallData idata)
081    {
082        super(parent, idata);
083
084        // We initialize our layout
085        layout = new GridBagLayout();
086        gbConstraints = new GridBagConstraints();
087        setLayout(layout);
088
089        int row = 1;
090
091        this.tipLabel = LabelFactory.create(parent.langpack.getString("InstallPanel.tip"),
092                parent.icons.getImageIcon("information"), JLabel.TRAILING);
093        parent.buildConstraints(gbConstraints, 0, row++, 2, 1, 1.0, 0.0);
094        gbConstraints.fill = GridBagConstraints.NONE;
095        gbConstraints.anchor = GridBagConstraints.NORTHWEST;
096        layout.addLayoutComponent(this.tipLabel, gbConstraints);
097        add(this.tipLabel);
098
099        this.packOpLabel = LabelFactory.create(" ", JLabel.TRAILING);
100        parent.buildConstraints(gbConstraints, 0, row++, 2, 1, 1.0, 0.0);
101        gbConstraints.anchor = GridBagConstraints.SOUTHWEST;
102        layout.addLayoutComponent(this.packOpLabel, gbConstraints);
103        add(this.packOpLabel);
104
105        this.packProgressBar = new JProgressBar();
106        this.packProgressBar.setStringPainted(true);
107        this.packProgressBar.setString(parent.langpack.getString("InstallPanel.begin"));
108        this.packProgressBar.setValue(0);
109        parent.buildConstraints(gbConstraints, 0, row++, 2, 1, 1.0, 0.0);
110        gbConstraints.anchor = GridBagConstraints.NORTH;
111        gbConstraints.fill = GridBagConstraints.HORIZONTAL;
112        layout.addLayoutComponent(this.packProgressBar, gbConstraints);
113        add(this.packProgressBar);
114
115        // make sure there is some space between the progress bars
116        JSeparator sep = new JSeparator();
117        Dimension dim = new Dimension(0, 10);
118        sep.setPreferredSize(dim);
119        sep.setMinimumSize(dim);
120        sep.setMaximumSize(dim);
121        parent.buildConstraints(gbConstraints, 0, row++, 2, 1, 1.0, 0.0);
122        layout.addLayoutComponent(sep, gbConstraints);
123        add(sep);
124
125        this.overallOpLabel = LabelFactory.create(parent.langpack
126                .getString("InstallPanel.progress"), parent.icons.getImageIcon("information"),
127                JLabel.TRAILING);
128        parent.buildConstraints(gbConstraints, 0, row++, 2, 1, 1.0, 0.0);
129        gbConstraints.anchor = GridBagConstraints.NORTHWEST;
130        gbConstraints.fill = GridBagConstraints.NONE;
131        layout.addLayoutComponent(this.overallOpLabel, gbConstraints);
132        add(this.overallOpLabel);
133
134        this.overallProgressBar = new JProgressBar();
135        this.overallProgressBar.setStringPainted(true);
136        this.overallProgressBar.setString("");
137        this.overallProgressBar.setValue(0);
138        parent.buildConstraints(gbConstraints, 0, row++, 2, 1, 1.0, 0.0);
139        gbConstraints.anchor = GridBagConstraints.NORTH;
140        gbConstraints.fill = GridBagConstraints.HORIZONTAL;
141        layout.addLayoutComponent(this.overallProgressBar, gbConstraints);
142        add(this.overallProgressBar);
143    }
144
145    /**
146     * Indicates wether the panel has been validated or not.
147     * 
148     * @return The validation state.
149     */
150    public boolean isValidated()
151    {
152        return this.validated;
153    }
154
155    /** The unpacker starts. */
156    public void startAction(String name, int noOfJobs)
157    {
158        parent.blockGUI();
159        // figure out how many packs there are to install
160        this.noOfPacks = noOfJobs;
161        this.overallProgressBar.setMinimum(0);
162        this.overallProgressBar.setMaximum(this.noOfPacks);
163        this.overallProgressBar.setString("0 / " + Integer.toString(this.noOfPacks));
164    }
165
166    /**
167     * An error was encountered.
168     * 
169     * @param error The error text.
170     */
171    public void emitError(String title, String error)
172    {
173        this.packOpLabel.setText(error);
174        idata.installSuccess = false;
175        JOptionPane.showMessageDialog(this, error, parent.langpack.getString("installer.error"),
176                JOptionPane.ERROR_MESSAGE);
177    }
178
179    /** The unpacker stops. */
180    public void stopAction()
181    {
182        parent.releaseGUI();
183        parent.lockPrevButton();
184        // With custom actions it is possible, that the current value
185        // is not max - 1. Therefore we use always max for both
186        // progress bars to signal finish state.
187        this.overallProgressBar.setValue(this.overallProgressBar.getMaximum());
188        int ppbMax = packProgressBar.getMaximum();
189        if (ppbMax < 1)
190        {
191            ppbMax = 1;
192            packProgressBar.setMaximum(ppbMax);
193        }
194        this.packProgressBar.setValue(ppbMax);
195
196        this.packProgressBar.setString(parent.langpack.getString("InstallPanel.finished"));
197        this.packProgressBar.setEnabled(false);
198        String no_of_packs = Integer.toString(this.noOfPacks);
199        this.overallProgressBar.setString(no_of_packs + " / " + no_of_packs);
200        this.overallProgressBar.setEnabled(false);
201        this.packOpLabel.setText(" ");
202        this.packOpLabel.setEnabled(false);
203        idata.canClose = true;
204        this.validated = true;
205        if (idata.panels.indexOf(this) != (idata.panels.size() - 1)) parent.unlockNextButton();
206    }
207
208    /**
209     * Normal progress indicator.
210     * 
211     * @param val The progression value.
212     * @param msg The progression message.
213     */
214    public void progress(int val, String msg)
215    {
216        this.packProgressBar.setValue(val + 1);
217        packOpLabel.setText(msg);
218    }
219
220    /**
221     * Pack changing.
222     * 
223     * @param packName The pack name.
224     * @param stepno The number of the pack.
225     * @param max The new maximum progress.
226     */
227    public void nextStep(String packName, int stepno, int max)
228    {
229        this.packProgressBar.setValue(0);
230        this.packProgressBar.setMinimum(0);
231        this.packProgressBar.setMaximum(max);
232        this.packProgressBar.setString(packName);
233        this.overallProgressBar.setValue(stepno - 1);
234        this.overallProgressBar.setString(Integer.toString(stepno) + " / "
235                + Integer.toString(this.noOfPacks));
236    }
237
238    /** Called when the panel becomes active. */
239    public void panelActivate()
240    {
241        // We clip the panel
242        Dimension dim = parent.getPanelsContainerSize();
243        dim.width = dim.width - (dim.width / 4);
244        dim.height = 150;
245        setMinimumSize(dim);
246        setMaximumSize(dim);
247        setPreferredSize(dim);
248        parent.lockNextButton();
249
250        parent.install(this);
251    }
252
253}