1 /*
2  * $Id$
3  *
4  * Copyright 2007 Bruno Lowagie.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20 
21 package com.lowagie.rups.model;
22 
23 import java.awt.GridBagConstraints;
24 import java.awt.GridBagLayout;
25 import java.awt.Insets;
26 
27 import javax.swing.JDialog;
28 import javax.swing.JFrame;
29 import javax.swing.JLabel;
30 import javax.swing.JProgressBar;
31 import javax.swing.UIManager;
32 
33 /**
34  * An informational dialog window showing the progress of a certain action.
35  */
36 public class ProgressDialog extends JDialog {
37 
38 	/** a serial version uid. */
39 	private static final long serialVersionUID = -8286949678008659120L;
40 	/** label showing the message describing what's in progress. */
41 	protected JLabel message;
42 	/** the progress bar */
43 	protected JProgressBar progress;
44 	/** the icon used for this dialog box. */
45 	public static final JLabel INFO = new JLabel(UIManager.getIcon("OptionPane.informationIcon"));
46 
47 	/**
48 	 * Creates a Progress frame displaying a certain message
49 	 * and a progress bar in indeterminate mode.
50 	 * @param	parent the parent frame of this dialog (used to position the dialog)
51 	 * @param	msg	the message that will be displayed.
52 	 */
ProgressDialog(JFrame parent, String msg)53 	public ProgressDialog(JFrame parent, String msg) {
54 		super();
55 		this.setTitle("Progress...");
56 		setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
57 	    setSize(300, 100);
58 	    this.setLocationRelativeTo(parent);
59 
60 		setLayout(new GridBagLayout());
61 		GridBagConstraints constraints = new GridBagConstraints();
62 		constraints.gridx = 0;
63 		constraints.gridy = 0;
64 		constraints.gridheight = 2;
65 		getContentPane().add(INFO, constraints);
66 		constraints.gridheight = 1;
67 		constraints.gridx = 1;
68 		constraints.insets = new Insets(5, 5, 5, 5);
69 	    message = new JLabel(msg);
70 	    getContentPane().add(message, constraints);
71 		constraints.gridy = 1;
72 	    progress = new JProgressBar();
73 	    progress.setIndeterminate(true);
74 	    getContentPane().add(progress, constraints);
75 
76 	    setVisible(true);
77 	}
78 
79 	/**
80 	 * Changes the message describing what's in progress
81 	 * @param msg	the message describing what's in progress
82 	 */
setMessage(String msg)83 	public void setMessage(String msg) {
84 		message.setText(msg);
85 	}
86 
87 	/**
88 	 * Changes the value of the progress bar.
89 	 * @param value	the current value
90 	 */
setValue(int value)91 	public void setValue(int value) {
92 		progress.setValue(value);
93 	}
94 
95 	/**
96 	 * Sets the maximum value for the progress bar.
97 	 * If 0 or less, sets the progress bar to indeterminate mode.
98 	 * @param n	the maximum value for the progress bar
99 	 */
setTotal(int n)100 	public void setTotal(int n) {
101 		if (n > 0) {
102 			progress.setMaximum(n);
103 			progress.setIndeterminate(false);
104 			progress.setStringPainted(true);
105 		}
106 		else {
107 			progress.setIndeterminate(true);
108 			progress.setStringPainted(false);
109 		}
110 	}
111 }
112