KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > panels > ProcessPanel


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2004 Tino Schwarze
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package com.izforge.izpack.panels;
23
24 import java.awt.BorderLayout JavaDoc;
25 import java.awt.Dimension JavaDoc;
26 import java.awt.Font JavaDoc;
27 import java.io.IOException JavaDoc;
28
29 import javax.swing.BoxLayout JavaDoc;
30 import javax.swing.JLabel JavaDoc;
31 import javax.swing.JPanel JavaDoc;
32 import javax.swing.JProgressBar JavaDoc;
33 import javax.swing.JScrollPane JavaDoc;
34 import javax.swing.JTextArea JavaDoc;
35 import javax.swing.SwingConstants JavaDoc;
36 import javax.swing.SwingUtilities JavaDoc;
37
38 import net.n3.nanoxml.XMLElement;
39
40 import com.izforge.izpack.installer.InstallData;
41 import com.izforge.izpack.installer.InstallerFrame;
42 import com.izforge.izpack.installer.IzPanel;
43 import com.izforge.izpack.installer.ProcessPanelWorker;
44 import com.izforge.izpack.util.AbstractUIProcessHandler;
45
46 /**
47  * The process panel class.
48  *
49  * This class allows external processes to be executed during installation.
50  *
51  * Parts of the code have been taken from CompilePanel.java and modified a lot.
52  *
53  * @author Tino Schwarze
54  * @author Julien Ponge
55  */

56 public class ProcessPanel extends IzPanel implements AbstractUIProcessHandler
57 {
58
59     /**
60      *
61      */

62     private static final long serialVersionUID = 3258417209583155251L;
63
64     /** The operation label . */
65     protected JLabel JavaDoc processLabel;
66
67     /** The overall progress bar. */
68     protected JProgressBar JavaDoc overallProgressBar;
69
70     /** True if the compilation has been done. */
71     private boolean validated = false;
72
73     /** The processing worker. Does all the work. */
74     private ProcessPanelWorker worker;
75
76     /** Number of jobs to process. Used for progress indication. */
77     private int noOfJobs;
78
79     private int currentJob;
80
81     /** Where the output is displayed */
82     private JTextArea JavaDoc outputPane;
83
84     /**
85      * The constructor.
86      *
87      * @param parent The parent window.
88      * @param idata The installation data.
89      */

90     public ProcessPanel(InstallerFrame parent, InstallData idata) throws IOException JavaDoc
91     {
92         super(parent, idata);
93
94         this.worker = new ProcessPanelWorker(idata, this);
95
96         JLabel JavaDoc heading = new JLabel JavaDoc();
97         Font JavaDoc font = heading.getFont();
98         font = font.deriveFont(Font.BOLD, font.getSize() * 2.0f);
99         heading.setFont(font);
100         heading.setHorizontalAlignment(SwingConstants.CENTER);
101         heading.setText(parent.langpack.getString("ProcessPanel.heading"));
102         heading.setVerticalAlignment(SwingConstants.TOP);
103         setLayout(new BorderLayout JavaDoc());
104         add(heading, BorderLayout.NORTH);
105
106         // put everything but the heading into it's own panel
107
// (to center it vertically)
108
JPanel JavaDoc subpanel = new JPanel JavaDoc();
109
110         subpanel.setAlignmentX(0.5f);
111         subpanel.setLayout(new BoxLayout JavaDoc(subpanel, BoxLayout.Y_AXIS));
112
113         this.processLabel = new JLabel JavaDoc();
114         this.processLabel.setAlignmentX(0.5f);
115         this.processLabel.setText(" ");
116         subpanel.add(this.processLabel);
117
118         this.overallProgressBar = new JProgressBar JavaDoc();
119         this.overallProgressBar.setAlignmentX(0.5f);
120         this.overallProgressBar.setStringPainted(true);
121         subpanel.add(this.overallProgressBar);
122
123         this.outputPane = new JTextArea JavaDoc();
124         this.outputPane.setEditable(false);
125         JScrollPane JavaDoc outputScrollPane = new JScrollPane JavaDoc(this.outputPane);
126         subpanel.add(outputScrollPane);
127
128         add(subpanel, BorderLayout.CENTER);
129     }
130
131     /**
132      * Indicates wether the panel has been validated or not.
133      *
134      * @return The validation state.
135      */

136     public boolean isValidated()
137     {
138         return validated;
139     }
140
141     /** The compiler starts. */
142     public void startProcessing(int no_of_jobs)
143     {
144         this.noOfJobs = no_of_jobs;
145         overallProgressBar.setMaximum(noOfJobs);
146         parent.lockPrevButton();
147     }
148
149     /** The compiler stops. */
150     public void finishProcessing()
151     {
152         overallProgressBar.setValue(this.noOfJobs);
153         String JavaDoc no_of_jobs = Integer.toString(this.noOfJobs);
154         overallProgressBar.setString(no_of_jobs + " / " + no_of_jobs);
155         overallProgressBar.setEnabled(false);
156
157         processLabel.setText(" ");
158         processLabel.setEnabled(false);
159
160         validated = true;
161         idata.installSuccess = true;
162         if (idata.panels.indexOf(this) != (idata.panels.size() - 1)) parent.unlockNextButton();
163     }
164
165     /**
166      * Log a message.
167      *
168      * @param message The message.
169      * @param stderr Whether the message came from stderr or stdout.
170      */

171     public void logOutput(String JavaDoc message, boolean stderr)
172     {
173         // TODO: make it colored
174
this.outputPane.append(message + '\n');
175
176         SwingUtilities.invokeLater(new Runnable JavaDoc() {
177
178             public void run()
179             {
180                 outputPane.setCaretPosition(outputPane.getText().length());
181             }
182         });
183     }
184
185     /**
186      * Next job starts.
187      *
188      * @param jobName The job name.
189      */

190     public void startProcess(String JavaDoc jobName)
191     {
192         processLabel.setText(jobName);
193
194         this.currentJob++;
195         overallProgressBar.setValue(this.currentJob);
196         overallProgressBar.setString(Integer.toString(this.currentJob) + " / "
197                 + Integer.toString(this.noOfJobs));
198     }
199
200     public void finishProcess()
201     {
202     }
203
204     /** Called when the panel becomes active. */
205     public void panelActivate()
206     {
207         // We clip the panel
208
Dimension JavaDoc dim = parent.getPanelsContainerSize();
209         dim.width -= (dim.width / 4);
210         dim.height = 150;
211         setMinimumSize(dim);
212         setMaximumSize(dim);
213         setPreferredSize(dim);
214
215         parent.lockNextButton();
216
217         this.worker.startThread();
218     }
219
220     /** Create XML data for automated installation. */
221     public void makeXMLData(XMLElement panelRoot)
222     {
223         // does nothing (no state to save)
224
}
225
226 }
227
Popular Tags