001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
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:
022: package com.izforge.izpack.panels;
023:
024: import java.awt.BorderLayout;
025: import java.awt.Dimension;
026: import java.awt.Font;
027: import java.io.IOException;
028:
029: import javax.swing.BoxLayout;
030: import javax.swing.JLabel;
031: import javax.swing.JPanel;
032: import javax.swing.JProgressBar;
033: import javax.swing.JScrollPane;
034: import javax.swing.JTextArea;
035: import javax.swing.SwingConstants;
036: import javax.swing.SwingUtilities;
037:
038: import net.n3.nanoxml.XMLElement;
039:
040: import com.izforge.izpack.installer.InstallData;
041: import com.izforge.izpack.installer.InstallerFrame;
042: import com.izforge.izpack.installer.IzPanel;
043: import com.izforge.izpack.installer.ProcessPanelWorker;
044: import 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: */
056: public class ProcessPanel extends IzPanel implements
057: AbstractUIProcessHandler {
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 = 0;
078:
079: private int currentJob = 0;
080:
081: /** Where the output is displayed */
082: private JTextArea outputPane;
083:
084: /**
085: * The constructor.
086: *
087: * @param parent The parent window.
088: * @param idata The installation data.
089: */
090: public ProcessPanel(InstallerFrame parent, InstallData idata)
091: throws IOException {
092: super (parent, idata);
093:
094: this .worker = new ProcessPanelWorker(idata, this );
095:
096: JLabel heading = new JLabel();
097: Font font = heading.getFont();
098: font = font.deriveFont(Font.BOLD, font.getSize() * 2.0f);
099: heading.setFont(font);
100: heading.setHorizontalAlignment(SwingConstants.CENTER);
101: heading.setText(parent.langpack
102: .getString("ProcessPanel.heading"));
103: heading.setVerticalAlignment(SwingConstants.TOP);
104: BorderLayout layout = new BorderLayout();
105: layout.setHgap(2);
106: layout.setVgap(2);
107: setLayout(layout);
108: add(heading, BorderLayout.NORTH);
109:
110: // put everything but the heading into it's own panel
111: // (to center it vertically)
112: JPanel subpanel = new JPanel();
113:
114: subpanel.setAlignmentX(0.5f);
115: subpanel.setLayout(new BoxLayout(subpanel, BoxLayout.Y_AXIS));
116:
117: this .processLabel = new JLabel();
118: this .processLabel.setAlignmentX(0.5f);
119: this .processLabel.setText(" ");
120: subpanel.add(this .processLabel);
121:
122: this .overallProgressBar = new JProgressBar();
123: this .overallProgressBar.setAlignmentX(0.5f);
124: this .overallProgressBar.setStringPainted(true);
125: subpanel.add(this .overallProgressBar);
126:
127: this .outputPane = new JTextArea();
128: this .outputPane.setEditable(false);
129: JScrollPane outputScrollPane = new JScrollPane(this .outputPane);
130: subpanel.add(outputScrollPane);
131:
132: add(subpanel, BorderLayout.CENTER);
133: }
134:
135: /**
136: * Indicates wether the panel has been validated or not.
137: *
138: * @return The validation state.
139: */
140: public boolean isValidated() {
141: return validated;
142: }
143:
144: /** The compiler starts. */
145: public void startProcessing(int no_of_jobs) {
146: this .noOfJobs = no_of_jobs;
147: overallProgressBar.setMaximum(no_of_jobs);
148: overallProgressBar.setIndeterminate(true);
149: parent.lockPrevButton();
150: }
151:
152: /** The compiler stops. */
153: public void finishProcessing() {
154: overallProgressBar.setIndeterminate(false);
155: String no_of_jobs = Integer.toString(this .noOfJobs);
156: overallProgressBar.setString(no_of_jobs + " / " + no_of_jobs);
157:
158: processLabel.setText(" ");
159: processLabel.setEnabled(false);
160:
161: validated = true;
162: idata.installSuccess = worker.getResult();
163: if (idata.panels.indexOf(this ) != (idata.panels.size() - 1))
164: 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: // TODO: make it colored
175: this .outputPane.append(message + '\n');
176:
177: SwingUtilities.invokeLater(new Runnable() {
178:
179: public void run() {
180: outputPane.setCaretPosition(outputPane.getText()
181: .length());
182: }
183: });
184: }
185:
186: /**
187: * Next job starts.
188: *
189: * @param jobName The job name.
190: */
191: public void startProcess(String jobName) {
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: // We clip the panel
207: Dimension dim = parent.getPanelsContainerSize();
208: dim.width -= (dim.width / 4);
209: dim.height = 150;
210: setMinimumSize(dim);
211: setMaximumSize(dim);
212: setPreferredSize(dim);
213:
214: parent.lockNextButton();
215:
216: this .worker.startThread();
217: }
218:
219: /** Create XML data for automated installation. */
220: public void makeXMLData(XMLElement panelRoot) {
221: // does nothing (no state to save)
222: }
223:
224: }
|