Source code: org/jempeg/empeg/emplode/dialog/ProgressDialog.java
1 /**
2 * Copyright (c) 2001, Mike Schrag & Daniel Zimmerman
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * Neither the name of Mike Schrag, Daniel Zimmerman, nor the names of any
16 * other contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 package org.jempeg.empeg.emplode.dialog;
32
33 import org.jempeg.empeg.emptool.DatabaseProgressListenerIfc;
34 import org.jempeg.util.Debug;
35
36 import javax.swing.BorderFactory;
37 import javax.swing.Box;
38 import javax.swing.JButton;
39 import javax.swing.JComponent;
40 import javax.swing.JDialog;
41 import javax.swing.JFrame;
42 import javax.swing.JLabel;
43 import javax.swing.JPanel;
44 import javax.swing.JProgressBar;
45 import javax.swing.SwingConstants;
46 import javax.swing.SwingUtilities;
47
48 import java.awt.BorderLayout;
49 import java.awt.Cursor;
50 import java.awt.Dimension;
51 import java.awt.GridBagLayout;
52 import java.awt.GridLayout;
53 import java.awt.Toolkit;
54
55 /**
56 * ProgressDialog is an implementation of
57 * DatabaseProgressListenerIfc that renders
58 * a set of labels and JProgressBars to
59 * represents the status of a download or
60 * upload.
61 *
62 * @author Mike Schrag
63 * @version $Revision: 1.12 $
64 */
65 public class ProgressDialog implements DatabaseProgressListenerIfc {
66 private JFrame myFrame;
67 private JDialog myDialog;
68
69 private JLabel myOperationLabel;
70 private JLabel myTaskLabel;
71
72 private JProgressBar myOperationProgressBar;
73 private JProgressBar myTaskProgressBar;
74
75 public ProgressDialog(JFrame _frame) {
76 myFrame = _frame;
77 myOperationLabel = new JLabel();
78 myTaskLabel = new JLabel();
79 myOperationProgressBar = new JProgressBar();
80 myTaskProgressBar = new JProgressBar();
81 }
82
83 protected void createDialog() {
84 myDialog = new JDialog(myFrame, "Working...", false);
85
86 JComponent comp = (JComponent)myDialog.getContentPane();
87
88 comp.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
89
90 GridLayout gridLayout = new GridLayout(0, 1, 0, 5);
91 comp.setLayout(gridLayout);
92 myOperationLabel.setHorizontalTextPosition(SwingConstants.CENTER);
93 myTaskLabel.setHorizontalTextPosition(SwingConstants.CENTER);
94
95 JButton cancelButton = new JButton("Cancel");
96 JButton detailsButton = new JButton("Details >>");
97
98 comp.add(myOperationLabel);
99 comp.add(myOperationProgressBar);
100 comp.add(Box.createVerticalStrut(5));
101 comp.add(myTaskLabel);
102 comp.add(myTaskProgressBar);
103
104 myDialog.pack();
105 Dimension size = myDialog.getSize();
106 myDialog.setSize(Math.max(400, size.width), Math.max(100, size.height));
107
108 DialogUtils.centerWindow(myDialog);
109
110 myDialog.getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
111 }
112
113 public synchronized void setVisible(boolean _visible) {
114 if (_visible) {
115 if (myDialog == null) {
116 createDialog();
117 myDialog.setVisible(true);
118 }
119 } else {
120 if (myDialog != null) {
121 myDialog.setVisible(false);
122 myDialog.dispose();
123 myDialog = null;
124 }
125 }
126 }
127
128 public synchronized void operationStarted(int _code, String _fmt) {
129 myOperationLabel.setText(_fmt);
130 myOperationLabel.repaint();
131 setVisibleInBackground(true);
132 }
133
134 public synchronized void operationUpdated(long _progress, long _total) {
135 updateProgress(myOperationProgressBar, _progress, _total);
136 }
137
138 public synchronized void taskStarted(int _code, String _fmt) {
139 myTaskLabel.setText(_fmt);
140 myTaskLabel.repaint();
141 setVisibleInBackground(true);
142 }
143
144 public synchronized void taskUpdated(long _progress, long _total) {
145 updateProgress(myTaskProgressBar, _progress, _total);
146 }
147
148 public void debug(int _level, String _fmt) {
149 Debug.println(_fmt);
150 }
151
152 public void log(int _level, String _fmt) {
153 Debug.println(_fmt);
154 }
155
156 public void error(int _code, String _fmt) {
157 Debug.handleError(myFrame, _fmt);
158 }
159
160 public void error(int _code, String _errorMessage, Throwable _error) {
161 Debug.handleError(myFrame, _errorMessage, _error);
162 }
163
164 public void error(int _code, Throwable _error) {
165 Debug.handleError(myFrame, _error);
166 }
167
168 protected synchronized void updateProgress(JProgressBar _progressBar, long _value, long _maximum) {
169 _progressBar.setMaximum((int)_maximum);
170 _progressBar.setValue((int)_value);
171 }
172
173 /**
174 * Calls setVisible() in a thread other than the one which called
175 * this method.
176 *
177 * @param _visible The new visible state.
178 * @ensures a request to set the dialog to the new visible state is
179 * enqueued in the Swing event queue.
180 **/
181
182 public synchronized void setVisibleInBackground(boolean _visible) {
183 setVisible(_visible);
184
185 // final boolean finalVisible = _visible;
186 //
187 // Runnable runnable = new Runnable() {
188 // public void run() {
189 // setVisible(finalVisible);
190 // }
191 // };
192 //
193 // SwingUtilities.invokeLater(runnable);
194 }
195 }