View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.gui.component;
21  
22  import javax.swing.JLabel;
23  import javax.swing.SwingUtilities;
24  
25  /***
26   * Provides a text based progress monitor.
27   */
28  public class TextProgressMonitor extends ProgressMonitorAdapter { 
29  
30  	//--- Data Field(s) ---
31  	
32  	private JLabel label;
33  	private int min;
34  	private int max;
35  	private int value;
36  	private double percent;
37  
38  	//--- Constructor(s) ---
39  
40  	public TextProgressMonitor(JLabel label)
41  	{
42  		this.label = label;
43  	}
44  
45      //--- Method(s) ---
46  
47  	/***
48  	 * Does nothing.
49  	 */
50  	public void done()
51  	{
52  	}
53  
54  	/***
55  	 * Returns false.
56  	 */
57  	public boolean isCancelled()
58  	{
59  		return false;
60  	}
61  	
62  	/***
63  	 * Does nothing.
64  	 */
65  	public void setCancelEnabled(boolean enabled)
66  	{
67  	}
68  	
69  	public void setMaximum(int max)
70  	{
71  		this.max = max;
72  	}
73  	
74  	public void setMinimum(int min)
75  	{
76  		this.min = min;
77  	}
78  
79  	public void setPercent(double percent)
80  	{
81  		this.percent = percent;
82  	}
83  
84  	public void setValue(int value)
85  	{
86  		this.value = value;
87  	}
88   
89  	public void setText(String text)
90  	{
91  		StringBuffer sb = new StringBuffer(text.length());
92  		for (int i = 0; i < text.length() - 1; i++) {
93  			if (text.charAt(i) == '%') {
94  				char c = text.charAt(i + 1);
95  				if (c == 'v') {
96  					sb.append(value);
97  				}
98  				if (c == '%') {
99  					sb.append('%');
100 				}
101 			}
102 			else {
103 				sb.append(text.charAt(i));
104 			}
105 		}
106 		
107 		final String s = text.toString();
108 		Runnable runner = new Runnable() 
109 			{
110 				public void run()
111 				{
112 					label.setText(s);
113 				}
114 			};
115 		SwingUtilities.invokeLater(runner);
116 	}
117 
118 }