001: /*
002: * WizardProgressBarPanel.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing.wizard;
023:
024: import java.awt.GridBagConstraints;
025: import java.awt.GridBagLayout;
026: import java.awt.event.ActionEvent;
027: import java.awt.event.ActionListener;
028: import java.sql.SQLException;
029: import javax.swing.JButton;
030: import javax.swing.JPanel;
031: import javax.swing.JProgressBar;
032: import javax.swing.JScrollPane;
033: import javax.swing.SwingUtilities;
034:
035: import org.underworldlabs.jdbc.DataSourceException;
036: import org.underworldlabs.swing.GUIUtils;
037: import org.underworldlabs.swing.StyledLogPane;
038:
039: /* ----------------------------------------------------------
040: * CVS NOTE: Changes to the CVS repository prior to the
041: * release of version 3.0.0beta1 has meant a
042: * resetting of CVS revision numbers.
043: * ----------------------------------------------------------
044: */
045:
046: /**
047: * Wizard progress bar panel rendering a progress bar and stop button.
048: *
049: * @author Takis Diakoumis
050: * @version $Revision: 1.5 $
051: * @date $Date: 2006/06/04 07:16:33 $
052: */
053: public class WizardProgressBarPanel extends JPanel implements
054: ActionListener {
055:
056: /** the stop button */
057: private JButton stopButton;
058:
059: /** The progress bar tracking the process */
060: private JProgressBar progressBar;
061:
062: /** the parent process */
063: private InterruptibleWizardProcess parent;
064:
065: /** The text area displaying process info */
066: private StyledLogPane output;
067:
068: /** Creates a new instance of WizardProgressBarPanel */
069: public WizardProgressBarPanel(InterruptibleWizardProcess parent) {
070: super (new GridBagLayout());
071: this .parent = parent;
072:
073: output = new StyledLogPane();
074: output.setBackground(getBackground());
075:
076: progressBar = new JProgressBar(0, 100);
077:
078: stopButton = new JButton("Stop");
079: stopButton.addActionListener(this );
080:
081: GridBagConstraints gbc = new GridBagConstraints();
082: gbc.gridy = 0;
083: gbc.gridx = 0;
084: gbc.weightx = 1.0;
085: gbc.fill = GridBagConstraints.HORIZONTAL;
086: add(progressBar, gbc);
087: gbc.gridx = 1;
088: gbc.weightx = 0;
089: gbc.insets.left = 5;
090: gbc.fill = GridBagConstraints.NONE;
091: add(stopButton, gbc);
092: gbc.gridy++;
093: gbc.gridx = 0;
094: gbc.weighty = 1.0;
095: gbc.insets.top = 10;
096: gbc.insets.left = 0;
097: gbc.fill = GridBagConstraints.BOTH;
098: gbc.gridwidth = GridBagConstraints.REMAINDER;
099: add(new JScrollPane(output), gbc);
100: }
101:
102: /**
103: * Sets the process as finished and disables the stop button.
104: */
105: public void finished() {
106: setProgressStatus(-1);
107: GUIUtils.invokeLater(new Runnable() {
108: public void run() {
109: stopButton.setEnabled(false);
110: }
111: });
112: }
113:
114: /**
115: * Resets the progress bar to zero and clears the output text area.
116: */
117: public void reset() {
118: GUIUtils.invokeLater(new Runnable() {
119: public void run() {
120: progressBar.setValue(0);
121: output.setText("");
122: stopButton.setEnabled(true);
123: }
124: });
125: }
126:
127: /**
128: * Returns the text currently displayed in the output pane.
129: */
130: public String getText() {
131: return output.getText();
132: }
133:
134: /**
135: * Sets the text to be appended within the
136: * progress info text area.
137: *
138: * @param the text to append
139: */
140: public void appendProgressText(final String t) {
141: Runnable setProgressText = new Runnable() {
142: public void run() {
143: output.append(StyledLogPane.PLAIN_MESSAGE_PREFORMAT, t);
144: }
145: };
146: SwingUtilities.invokeLater(setProgressText);
147: }
148:
149: /**
150: * Sets the text to be appended within the
151: * progress info text area as an error message.
152: *
153: * @param the text to append
154: */
155: public void appendProgressErrorText(final String t) {
156: Runnable setProgressText = new Runnable() {
157: public void run() {
158: output.append(StyledLogPane.ERROR_MESSAGE_PREFORMAT, t);
159: }
160: };
161: SwingUtilities.invokeLater(setProgressText);
162: }
163:
164: public void appendExceptionError(String message, Throwable e) {
165: StringBuffer outputBuffer = new StringBuffer();
166: if (message != null) {
167: outputBuffer.append(message);
168: }
169: outputBuffer.append("\n[ ");
170:
171: String exceptionName = null;
172: if (e.getCause() != null) {
173: Throwable _e = e.getCause();
174: exceptionName = _e.getClass().getName();
175: } else {
176: exceptionName = e.getClass().getName();
177: }
178:
179: int index = exceptionName.lastIndexOf('.');
180: if (index != -1) {
181: exceptionName = exceptionName.substring(index + 1);
182: }
183: outputBuffer.append(exceptionName);
184: outputBuffer.append(" ] ");
185:
186: if (e instanceof DataSourceException) {
187: outputBuffer.append(e.getMessage());
188: outputBuffer.append(((DataSourceException) e)
189: .getExtendedMessage());
190: } else if (e instanceof SQLException) {
191: outputBuffer.append(e.getMessage());
192: SQLException _e = (SQLException) e;
193: outputBuffer.append("\nError Code: " + _e.getErrorCode());
194:
195: String state = _e.getSQLState();
196: if (state != null) {
197: outputBuffer.append("\nSQL State Code: " + state);
198: }
199:
200: } else {
201: outputBuffer.append(e.getMessage());
202: }
203:
204: appendProgressErrorText(outputBuffer.toString());
205: }
206:
207: /**
208: * Sets the progress bar's position during the process.
209: * A value of -1 will set the progress bar to its set maximum.
210: *
211: * @param the new process status
212: */
213: public void setProgressStatus(int status) {
214: final int value = (status > 0 ? status : progressBar
215: .getMaximum());
216: Runnable setProgressBar = new Runnable() {
217: public void run() {
218: progressBar.setValue(value);
219: }
220: };
221: SwingUtilities.invokeLater(setProgressBar);
222: }
223:
224: /**
225: * Sets the progress bar's minimum value to the specified value.
226: *
227: * @param min - the minimum value
228: */
229: public void setMinimum(int min) {
230: progressBar.setMinimum(min);
231: }
232:
233: /**
234: * Sets the progress bar's maximum value to the specified value.
235: *
236: * @param max - the maximum value
237: */
238: public void setMaximum(int max) {
239: progressBar.setMaximum(max);
240: }
241:
242: /**
243: * Retrieves the progress bar's maximum value.
244: *
245: * @param the progress bar's maximum value
246: */
247: public int getMaximum() {
248: return progressBar.getMaximum();
249: }
250:
251: /** <p>Sets the progress bar to track
252: * indeterminate values - action of
253: * unknown length is taking place.
254: */
255: public void setIndeterminate(boolean indeterminate) {
256: progressBar.setIndeterminate(indeterminate);
257: }
258:
259: /**
260: * Invoked when the stop button is pushed.
261: */
262: public void actionPerformed(ActionEvent e) {
263: parent.stop();
264: stopButton.setEnabled(false);
265: }
266:
267: }
|