View Javadoc

1   /*
2    *  XNap Commons
3    *
4    *  Copyright (C) 2005  Steffen Pingel
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.xnap.commons.gui;
21  
22  import java.awt.BorderLayout;
23  import java.awt.Component;
24  import javax.swing.BorderFactory;
25  import javax.swing.JDialog;
26  import javax.swing.JFrame;
27  import javax.swing.JLabel;
28  import javax.swing.JPanel;
29  import javax.swing.JProgressBar;
30  import javax.swing.SwingUtilities;
31  import org.xnap.commons.gui.util.GUIHelper;
32  import org.xnap.commons.io.ProgressMonitor;
33  
34  /***
35   * A simple progress dialog that displays text in a {@link javax.swing.JLabel}
36   * and a {@link javax.swing.JProgressBar}. The dialog can be used to monitor 
37   * long running operations.
38   * 
39   * @author Steffen Pingel
40   * 
41   * @see org.xnap.commons.io.ProgressMonitor
42   */
43  public class ProgressDialog extends DefaultDialog implements ProgressMonitor {
44  
45  	public static final int MAX_VALUE = 1000;
46  	
47  	private boolean isCancelled = false;
48  	private JPanel panel;
49      private JProgressBar progressBar;
50      private JLabel statusLabel;
51  	private boolean disposed = false;
52  	private long value;
53  	private long totalSteps;
54  
55      public ProgressDialog(JDialog owner, int buttons, String title) 
56      {
57  		super(owner, buttons);
58  
59  		initialize();
60  		
61          setTitle(title);
62      }
63  
64      public ProgressDialog(JFrame owner, int buttons, String title) 
65      {
66  		super(owner, buttons);
67  
68  		initialize();
69  		
70          setTitle(title);
71      }
72  
73      public ProgressDialog(int buttons, String title) 
74      {
75  		super(buttons);
76  
77  		initialize();
78  		
79          setTitle(title);
80      }
81  
82  	public ProgressDialog(JDialog owner, String title)
83  	{
84  		this(owner, BUTTON_CANCEL, title);
85  	}
86  
87  	public ProgressDialog(JFrame owner, String title)
88  	{
89  		this(owner, BUTTON_CANCEL, title);
90  	}
91  
92  	public ProgressDialog(String title)
93  	{
94  		this(BUTTON_CANCEL, title);
95  	}
96  
97      public ProgressDialog(int buttons) 
98      {
99  		super(buttons);
100 
101 		initialize();
102     }
103 
104 	public ProgressDialog(JDialog owner)
105 	{
106 		this(owner, BUTTON_CANCEL, null);
107 	}
108 
109 	public ProgressDialog(JFrame owner)
110 	{
111 		this(owner, BUTTON_CANCEL, null);
112 	}
113 
114 	public ProgressDialog()
115 	{
116 		this(BUTTON_CANCEL);
117 	}
118 	
119 	private void initialize()
120 	{
121 		setModal(true);
122 
123 		panel = new JPanel(new BorderLayout());
124 
125 		// status
126 		statusLabel = new JLabel(GUIHelper.tt(" "));
127 		statusLabel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
128 		panel.add(statusLabel, BorderLayout.CENTER);
129 
130 		// progress
131 		progressBar = new JProgressBar();
132 		progressBar.setMinimum(0);
133 		progressBar.setMaximum(MAX_VALUE);
134 		progressBar.setValue(0);
135 		panel.add(progressBar, BorderLayout.SOUTH);
136 
137 		setMainComponent(panel);
138 		pack();
139 	}
140 
141 	/***
142 	 * Sets the cancelled status to true.
143 	 */
144 	public void close()
145 	{
146 		getCancelAction().setEnabled(false);
147 		isCancelled = true;
148 	}
149     
150 	/***
151 	 * Disposes the dialog.
152 	 */
153 	public void done()
154 	{
155 		Runnable r = new Runnable()
156 			{
157 				public void run()
158 				{
159 					if (isVisible()) {
160 						dispose();
161 					}
162 					else {
163 						disposed = true;
164 					}
165 				}
166 			};
167 		SwingUtilities.invokeLater(r);		
168 	}
169 	
170 	public JPanel getPanel()
171 	{
172 		return panel;
173 	}
174 
175 	public boolean isCancelled()
176 	{
177 		return isCancelled;
178 	}
179 
180 	public long getValue()
181 	{
182 		return value;
183 	}
184 	
185     public void setCancelEnabled(final boolean enabled)
186     {
187 		Runnable r = new Runnable()
188 			{
189 				public void run()
190 				{
191 					getCancelAction().setEnabled(enabled);
192 				}
193 			};
194 		SwingUtilities.invokeLater(r);
195     }
196 
197     public void setTotalSteps(final long max)
198     {
199 		this.totalSteps = max;
200     }
201 
202     public void setValue(final long value)
203     {
204 		this.value = value;
205 		final double percent = (double)this.value / totalSteps;
206 		Runnable r = new Runnable()
207 			{
208 				public void run()
209 				{
210 					progressBar.setValue((int)(percent * MAX_VALUE));
211 				}
212 			};
213 		SwingUtilities.invokeLater(r);
214     }
215 
216     public void work(final long amount)
217     {
218 		this.value += amount;
219 		final double percent = (double)this.value / totalSteps;
220 		Runnable r = new Runnable()
221 			{
222 				public void run()
223 				{
224 					progressBar.setValue((int)(percent * MAX_VALUE));
225 				}
226 			};
227 		SwingUtilities.invokeLater(r);
228     }
229 
230     public void setText(final String text)
231     {
232 		Runnable r = new Runnable()
233 			{
234 				public void run()
235 				{
236 					statusLabel.setText(GUIHelper.tt(text));
237 					pack();
238 				}
239 			};
240 		SwingUtilities.invokeLater(r);
241     }
242 
243 	public void showDialog()
244 	{
245 		if (disposed) {
246 			disposed = false;
247 			return;
248 		}
249 		super.setVisible(true);
250 	}
251 
252 	public Component getComponent()
253 	{
254 		return this;
255 	}
256 
257 }