1355 |
20 Sep 05 |
nicklas |
1 |
/* |
1355 |
20 Sep 05 |
nicklas |
$Id$ |
1355 |
20 Sep 05 |
nicklas |
3 |
|
3675 |
16 Aug 07 |
jari |
Copyright (C) 2005 Nicklas Nordborg |
4889 |
06 Apr 09 |
nicklas |
Copyright (C) 2006 Jari Häkkinen |
1355 |
20 Sep 05 |
nicklas |
6 |
|
2304 |
22 May 06 |
jari |
This file is part of BASE - BioArray Software Environment. |
2304 |
22 May 06 |
jari |
Available at http://base.thep.lu.se/ |
1355 |
20 Sep 05 |
nicklas |
9 |
|
1355 |
20 Sep 05 |
nicklas |
BASE is free software; you can redistribute it and/or |
1355 |
20 Sep 05 |
nicklas |
modify it under the terms of the GNU General Public License |
4479 |
05 Sep 08 |
jari |
as published by the Free Software Foundation; either version 3 |
1355 |
20 Sep 05 |
nicklas |
of the License, or (at your option) any later version. |
1355 |
20 Sep 05 |
nicklas |
14 |
|
1355 |
20 Sep 05 |
nicklas |
BASE is distributed in the hope that it will be useful, |
1355 |
20 Sep 05 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
1355 |
20 Sep 05 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1355 |
20 Sep 05 |
nicklas |
GNU General Public License for more details. |
1355 |
20 Sep 05 |
nicklas |
19 |
|
1355 |
20 Sep 05 |
nicklas |
You should have received a copy of the GNU General Public License |
4515 |
11 Sep 08 |
jari |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
1355 |
20 Sep 05 |
nicklas |
22 |
*/ |
1355 |
20 Sep 05 |
nicklas |
23 |
package net.sf.basedb.util; |
1355 |
20 Sep 05 |
nicklas |
24 |
|
1355 |
20 Sep 05 |
nicklas |
25 |
import net.sf.basedb.core.ProgressReporter; |
1355 |
20 Sep 05 |
nicklas |
26 |
|
1355 |
20 Sep 05 |
nicklas |
27 |
/** |
1355 |
20 Sep 05 |
nicklas |
An implementation of the ProgressReporter interface that chains |
1355 |
20 Sep 05 |
nicklas |
to another progress reporter while recalculating the percentage |
1355 |
20 Sep 05 |
nicklas |
values. This class is useful when you call several methods in a row |
1355 |
20 Sep 05 |
nicklas |
that each of them outputs progress messages between 0 and 100 percent. |
1355 |
20 Sep 05 |
nicklas |
Use this progress reporter to change the scale so that the first method |
1355 |
20 Sep 05 |
nicklas |
output values between 0 and 20, the next between 20 and 50, and so on. |
1355 |
20 Sep 05 |
nicklas |
34 |
|
1355 |
20 Sep 05 |
nicklas |
@author Nicklas |
1355 |
20 Sep 05 |
nicklas |
@version 2.0 |
1355 |
20 Sep 05 |
nicklas |
@base.modified $Date$ |
1355 |
20 Sep 05 |
nicklas |
38 |
*/ |
1355 |
20 Sep 05 |
nicklas |
39 |
public class ChainedProgressReporter |
1355 |
20 Sep 05 |
nicklas |
40 |
implements ProgressReporter |
1355 |
20 Sep 05 |
nicklas |
41 |
{ |
1355 |
20 Sep 05 |
nicklas |
42 |
|
1355 |
20 Sep 05 |
nicklas |
43 |
private final ProgressReporter master; |
1355 |
20 Sep 05 |
nicklas |
44 |
private int start; |
1355 |
20 Sep 05 |
nicklas |
45 |
private int end; |
1355 |
20 Sep 05 |
nicklas |
46 |
private float scale; |
6928 |
04 Jun 15 |
nicklas |
47 |
private String messagePrefix; |
1355 |
20 Sep 05 |
nicklas |
48 |
|
1355 |
20 Sep 05 |
nicklas |
49 |
/** |
1355 |
20 Sep 05 |
nicklas |
Create a new chained progress reporter. All calls to this |
1355 |
20 Sep 05 |
nicklas |
progress reporter will be propagated to the master progress reporter |
1355 |
20 Sep 05 |
nicklas |
after the percentage value has been recalculated to be within the specified |
1355 |
20 Sep 05 |
nicklas |
range. |
1355 |
20 Sep 05 |
nicklas |
@param master The master progress reporter |
1355 |
20 Sep 05 |
nicklas |
@see #setRange(int, int) |
1355 |
20 Sep 05 |
nicklas |
56 |
*/ |
1355 |
20 Sep 05 |
nicklas |
57 |
public ChainedProgressReporter(ProgressReporter master) |
1355 |
20 Sep 05 |
nicklas |
58 |
{ |
1355 |
20 Sep 05 |
nicklas |
59 |
this.master = master; |
1355 |
20 Sep 05 |
nicklas |
60 |
setRange(0, 100); |
1355 |
20 Sep 05 |
nicklas |
61 |
} |
1355 |
20 Sep 05 |
nicklas |
62 |
|
1355 |
20 Sep 05 |
nicklas |
63 |
/* |
1355 |
20 Sep 05 |
nicklas |
From the ProgressReporter interface |
1355 |
20 Sep 05 |
nicklas |
65 |
------------------------------------------- |
1355 |
20 Sep 05 |
nicklas |
66 |
*/ |
6127 |
14 Sep 12 |
nicklas |
67 |
@Override |
1355 |
20 Sep 05 |
nicklas |
68 |
public void display(int percent, String message) |
1355 |
20 Sep 05 |
nicklas |
69 |
{ |
5329 |
29 Apr 10 |
nicklas |
70 |
if (percent != -1) percent = (int)(percent*scale)+start; |
6928 |
04 Jun 15 |
nicklas |
71 |
if (messagePrefix != null) message = messagePrefix + message; |
5329 |
29 Apr 10 |
nicklas |
72 |
master.display(percent, message); |
1355 |
20 Sep 05 |
nicklas |
73 |
} |
6127 |
14 Sep 12 |
nicklas |
74 |
@Override |
1355 |
20 Sep 05 |
nicklas |
75 |
public void append(String message) |
1355 |
20 Sep 05 |
nicklas |
76 |
{ |
1355 |
20 Sep 05 |
nicklas |
77 |
master.append(message); |
1355 |
20 Sep 05 |
nicklas |
78 |
} |
1355 |
20 Sep 05 |
nicklas |
79 |
// ------------------------------------------- |
1355 |
20 Sep 05 |
nicklas |
80 |
|
1355 |
20 Sep 05 |
nicklas |
81 |
/** |
1355 |
20 Sep 05 |
nicklas |
Set the range to use for the percentage values. When the |
1355 |
20 Sep 05 |
nicklas |
{@link #display(int, String)} method is invoked the percentage |
1355 |
20 Sep 05 |
nicklas |
value is recalculated to be within the specified range before the |
1355 |
20 Sep 05 |
nicklas |
master progress reporter is invoked. |
1355 |
20 Sep 05 |
nicklas |
Ie. 0 --> <code>start</code> and 100 --> <code>end</code>. |
4026 |
30 Nov 07 |
martin |
@param start Start value |
4026 |
30 Nov 07 |
martin |
@param end End value. |
1355 |
20 Sep 05 |
nicklas |
89 |
*/ |
1355 |
20 Sep 05 |
nicklas |
90 |
public void setRange(int start, int end) |
1355 |
20 Sep 05 |
nicklas |
91 |
{ |
1355 |
20 Sep 05 |
nicklas |
92 |
this.start = start; |
1355 |
20 Sep 05 |
nicklas |
93 |
this.end = end; |
1355 |
20 Sep 05 |
nicklas |
94 |
this.scale = ((float)(end - start)) / 100; |
1355 |
20 Sep 05 |
nicklas |
95 |
} |
1355 |
20 Sep 05 |
nicklas |
96 |
|
6928 |
04 Jun 15 |
nicklas |
97 |
/** |
6928 |
04 Jun 15 |
nicklas |
Set a message prefix that is pre-pended to message before it is sent to the |
6928 |
04 Jun 15 |
nicklas |
master progress reporter. |
6928 |
04 Jun 15 |
nicklas |
@since 3.6 |
6928 |
04 Jun 15 |
nicklas |
101 |
*/ |
6928 |
04 Jun 15 |
nicklas |
102 |
public void setMessagePrefix(String prefix) |
6928 |
04 Jun 15 |
nicklas |
103 |
{ |
6928 |
04 Jun 15 |
nicklas |
104 |
this.messagePrefix = prefix; |
6928 |
04 Jun 15 |
nicklas |
105 |
} |
6928 |
04 Jun 15 |
nicklas |
106 |
|
1355 |
20 Sep 05 |
nicklas |
107 |
} |