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.GridBagConstraints;
023import java.awt.GridBagLayout;
024import java.awt.Insets;
025import java.awt.event.ActionEvent;
026import java.awt.event.ActionListener;
027import java.io.BufferedOutputStream;
028import java.io.File;
029import java.io.FileOutputStream;
030
031import javax.swing.Box;
032import javax.swing.BoxLayout;
033import javax.swing.JButton;
034import javax.swing.JFileChooser;
035import javax.swing.JLabel;
036import javax.swing.JOptionPane;
037import javax.swing.JPanel;
038
039import com.izforge.izpack.gui.ButtonFactory;
040import com.izforge.izpack.gui.LabelFactory;
041import com.izforge.izpack.installer.InstallData;
042import com.izforge.izpack.installer.InstallerFrame;
043import com.izforge.izpack.installer.IzPanel;
044import com.izforge.izpack.util.VariableSubstitutor;
045
046/**
047 * The finish panel class.
048 * 
049 * @author Julien Ponge
050 */
051public class FinishPanel extends IzPanel implements ActionListener
052{
053
054    private static final long serialVersionUID = 3257282535107998009L;
055
056    /** The layout. */
057    private BoxLayout layout;
058
059    /** The automated installers generation button. */
060    private JButton autoButton;
061
062    /** The center panel. */
063    private JPanel centerPanel;
064
065    /** The variables substitutor. */
066    private VariableSubstitutor vs;
067
068    /**
069     * The constructor.
070     * 
071     * @param parent The parent.
072     * @param idata The installation data.
073     */
074    public FinishPanel(InstallerFrame parent, InstallData idata)
075    {
076        super(parent, idata);
077
078        vs = new VariableSubstitutor(idata.getVariables());
079
080        // The 'super' layout
081        GridBagLayout superLayout = new GridBagLayout();
082        setLayout(superLayout);
083        GridBagConstraints gbConstraints = new GridBagConstraints();
084        gbConstraints.insets = new Insets(0, 0, 0, 0);
085        gbConstraints.fill = GridBagConstraints.NONE;
086        gbConstraints.anchor = GridBagConstraints.CENTER;
087
088        // We initialize our 'real' layout
089        centerPanel = new JPanel();
090        layout = new BoxLayout(centerPanel, BoxLayout.Y_AXIS);
091        centerPanel.setLayout(layout);
092        superLayout.addLayoutComponent(centerPanel, gbConstraints);
093        add(centerPanel);
094    }
095
096    /**
097     * Indicates wether the panel has been validated or not.
098     * 
099     * @return true if the panel has been validated.
100     */
101    public boolean isValidated()
102    {
103        return true;
104    }
105
106    /** Called when the panel becomes active. */
107    public void panelActivate()
108    {
109        parent.lockNextButton();
110        parent.lockPrevButton();
111        parent.setQuitButtonText(parent.langpack.getString("FinishPanel.done"));
112        if (idata.installSuccess)
113        {
114            // We set the information
115            centerPanel.add(LabelFactory.create(parent.langpack.getString("FinishPanel.success"),
116                    parent.icons.getImageIcon("information"), JLabel.TRAILING));
117            centerPanel.add(Box.createVerticalStrut(20));
118
119            if (idata.uninstallOutJar != null)
120            {
121                // We prepare a message for the uninstaller feature
122                String path = translatePath("$INSTALL_PATH") + File.separator + "Uninstaller";
123
124                centerPanel.add(LabelFactory.create(parent.langpack
125                        .getString("FinishPanel.uninst.info"), parent.icons
126                        .getImageIcon("information"), JLabel.TRAILING));
127                centerPanel.add(LabelFactory.create(path, parent.icons.getImageIcon("empty"),
128                        JLabel.TRAILING));
129            }
130
131            // We add the autoButton
132            centerPanel.add(Box.createVerticalStrut(20));
133            autoButton = ButtonFactory.createButton(parent.langpack.getString("FinishPanel.auto"),
134                    parent.icons.getImageIcon("edit"), idata.buttonsHColor);
135            autoButton.setToolTipText(parent.langpack.getString("FinishPanel.auto.tip"));
136            autoButton.addActionListener(this);
137            centerPanel.add(autoButton);
138        }
139        else
140            centerPanel.add(LabelFactory.create(parent.langpack.getString("FinishPanel.fail"),
141                    parent.icons.getImageIcon("information"), JLabel.TRAILING));
142    }
143
144    /**
145     * Actions-handling method.
146     * 
147     * @param e The event.
148     */
149    public void actionPerformed(ActionEvent e)
150    {
151        // Prepares the file chooser
152        JFileChooser fc = new JFileChooser();
153        fc.setCurrentDirectory(new File(idata.getInstallPath()));
154        fc.setMultiSelectionEnabled(false);
155        fc.addChoosableFileFilter(fc.getAcceptAllFileFilter());
156        // fc.setCurrentDirectory(new File("."));
157
158        // Shows it
159        try
160        {
161            if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION)
162            {
163                // We handle the xml data writing
164                File file = fc.getSelectedFile();
165                FileOutputStream out = new FileOutputStream(file);
166                BufferedOutputStream outBuff = new BufferedOutputStream(out, 5120);
167                parent.writeXMLTree(idata.xmlData, outBuff);
168                outBuff.flush();
169                outBuff.close();
170
171                autoButton.setEnabled(false);
172            }
173        }
174        catch (Exception err)
175        {
176            err.printStackTrace();
177            JOptionPane.showMessageDialog(this, err.toString(), parent.langpack
178                    .getString("installer.error"), JOptionPane.ERROR_MESSAGE);
179        }
180    }
181
182    /**
183     * Translates a relative path to a local system path.
184     * 
185     * @param destination The path to translate.
186     * @return The translated path.
187     */
188    private String translatePath(String destination)
189    {
190        // Parse for variables
191        destination = vs.substitute(destination, null);
192
193        // Convert the file separator characters
194        return destination.replace('/', File.separatorChar);
195    }
196}