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 * Copyright 2004 Tino Schwarze
008 * 
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *     http://www.apache.org/licenses/LICENSE-2.0
014 *     
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 */
021
022package com.izforge.izpack.panels;
023
024import java.awt.BorderLayout;
025import java.awt.Dimension;
026import java.awt.Font;
027import java.io.IOException;
028
029import javax.swing.BoxLayout;
030import javax.swing.JLabel;
031import javax.swing.JPanel;
032import javax.swing.JProgressBar;
033import javax.swing.JScrollPane;
034import javax.swing.JTextArea;
035import javax.swing.SwingConstants;
036import javax.swing.SwingUtilities;
037
038import net.n3.nanoxml.XMLElement;
039
040import com.izforge.izpack.installer.InstallData;
041import com.izforge.izpack.installer.InstallerFrame;
042import com.izforge.izpack.installer.IzPanel;
043import com.izforge.izpack.installer.ProcessPanelWorker;
044import com.izforge.izpack.util.AbstractUIProcessHandler;
045
046/**
047 * The process panel class.
048 * 
049 * This class allows external processes to be executed during installation.
050 * 
051 * Parts of the code have been taken from CompilePanel.java and modified a lot.
052 * 
053 * @author Tino Schwarze
054 * @author Julien Ponge
055 */
056public class ProcessPanel extends IzPanel implements AbstractUIProcessHandler
057{
058
059    /**
060     * 
061     */
062    private static final long serialVersionUID = 3258417209583155251L;
063
064    /** The operation label . */
065    protected JLabel processLabel;
066
067    /** The overall progress bar. */
068    protected JProgressBar overallProgressBar;
069
070    /** True if the compilation has been done. */
071    private boolean validated = false;
072
073    /** The processing worker. Does all the work. */
074    private ProcessPanelWorker worker;
075
076    /** Number of jobs to process. Used for progress indication. */
077    private int noOfJobs;
078
079    private int currentJob;
080
081    /** Where the output is displayed */
082    private JTextArea outputPane;
083
084    private JScrollPane outputScrollPane;
085
086    /**
087     * The constructor.
088     * 
089     * @param parent The parent window.
090     * @param idata The installation data.
091     */
092    public ProcessPanel(InstallerFrame parent, InstallData idata) throws IOException
093    {
094        super(parent, idata);
095
096        this.worker = new ProcessPanelWorker(idata, this);
097
098        JLabel heading = new JLabel();
099        Font font = heading.getFont();
100        font = font.deriveFont(Font.BOLD, font.getSize() * 2.0f);
101        heading.setFont(font);
102        heading.setHorizontalAlignment(SwingConstants.CENTER);
103        heading.setText(parent.langpack.getString("ProcessPanel.heading"));
104        heading.setVerticalAlignment(SwingConstants.TOP);
105        setLayout(new BorderLayout());
106        add(heading, BorderLayout.NORTH);
107
108        // put everything but the heading into it's own panel
109        // (to center it vertically)
110        JPanel subpanel = new JPanel();
111
112        subpanel.setAlignmentX(0.5f);
113        subpanel.setLayout(new BoxLayout(subpanel, BoxLayout.Y_AXIS));
114
115        this.processLabel = new JLabel();
116        this.processLabel.setAlignmentX(0.5f);
117        this.processLabel.setText(" ");
118        subpanel.add(this.processLabel);
119
120        this.overallProgressBar = new JProgressBar();
121        this.overallProgressBar.setAlignmentX(0.5f);
122        this.overallProgressBar.setStringPainted(true);
123        subpanel.add(this.overallProgressBar);
124
125        this.outputPane = new JTextArea();
126        this.outputPane.setEditable(false);
127        this.outputScrollPane = new JScrollPane(this.outputPane);
128        subpanel.add(this.outputScrollPane);
129
130        add(subpanel, BorderLayout.CENTER);
131    }
132
133    /**
134     * Indicates wether the panel has been validated or not.
135     * 
136     * @return The validation state.
137     */
138    public boolean isValidated()
139    {
140        return validated;
141    }
142
143    /** The compiler starts. */
144    public void startProcessing(int no_of_jobs)
145    {
146        this.noOfJobs = no_of_jobs;
147        overallProgressBar.setMaximum(noOfJobs);
148        parent.lockPrevButton();
149    }
150
151    /** The compiler stops. */
152    public void finishProcessing()
153    {
154        overallProgressBar.setValue(this.noOfJobs);
155        String no_of_jobs = Integer.toString(this.noOfJobs);
156        overallProgressBar.setString(no_of_jobs + " / " + no_of_jobs);
157        overallProgressBar.setEnabled(false);
158
159        processLabel.setText(" ");
160        processLabel.setEnabled(false);
161
162        validated = true;
163        idata.installSuccess = true;
164        if (idata.panels.indexOf(this) != (idata.panels.size() - 1)) parent.unlockNextButton();
165    }
166
167    /**
168     * Log a message.
169     * 
170     * @param message The message.
171     * @param stderr Whether the message came from stderr or stdout.
172     */
173    public void logOutput(String message, boolean stderr)
174    {
175        // TODO: make it colored
176        this.outputPane.append(message + '\n');
177
178        SwingUtilities.invokeLater(new Runnable() {
179
180            public void run()
181            {
182                outputPane.setCaretPosition(outputPane.getText().length());
183            }
184        });
185    }
186
187    /**
188     * Next job starts.
189     * 
190     * @param jobName The job name.
191     */
192    public void startProcess(String jobName)
193    {
194        processLabel.setText(jobName);
195
196        this.currentJob++;
197        overallProgressBar.setValue(this.currentJob);
198        overallProgressBar.setString(Integer.toString(this.currentJob) + " / "
199                + Integer.toString(this.noOfJobs));
200    }
201
202    public void finishProcess()
203    {
204    }
205
206    /** Called when the panel becomes active. */
207    public void panelActivate()
208    {
209        // We clip the panel
210        Dimension dim = parent.getPanelsContainerSize();
211        dim.width = dim.width - (dim.width / 4);
212        dim.height = 150;
213        setMinimumSize(dim);
214        setMaximumSize(dim);
215        setPreferredSize(dim);
216
217        parent.lockNextButton();
218
219        this.worker.startThread();
220    }
221
222    /** Create XML data for automated installation. */
223    public void makeXMLData(XMLElement panelRoot)
224    {
225        // does nothing (no state to save)
226    }
227
228}