src/core/net/sf/basedb/util/ChainedProgressReporter.java

Code
Comments
Other
Rev Date Author Line
1355 20 Sep 05 nicklas 1 /*
1355 20 Sep 05 nicklas 2   $Id$
1355 20 Sep 05 nicklas 3
3675 16 Aug 07 jari 4   Copyright (C) 2005 Nicklas Nordborg
4889 06 Apr 09 nicklas 5   Copyright (C) 2006 Jari Häkkinen
1355 20 Sep 05 nicklas 6  
2304 22 May 06 jari 7   This file is part of BASE - BioArray Software Environment.
2304 22 May 06 jari 8   Available at http://base.thep.lu.se/
1355 20 Sep 05 nicklas 9
1355 20 Sep 05 nicklas 10   BASE is free software; you can redistribute it and/or
1355 20 Sep 05 nicklas 11   modify it under the terms of the GNU General Public License
4479 05 Sep 08 jari 12   as published by the Free Software Foundation; either version 3
1355 20 Sep 05 nicklas 13   of the License, or (at your option) any later version.
1355 20 Sep 05 nicklas 14
1355 20 Sep 05 nicklas 15   BASE is distributed in the hope that it will be useful,
1355 20 Sep 05 nicklas 16   but WITHOUT ANY WARRANTY; without even the implied warranty of
1355 20 Sep 05 nicklas 17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1355 20 Sep 05 nicklas 18   GNU General Public License for more details.
1355 20 Sep 05 nicklas 19
1355 20 Sep 05 nicklas 20    You should have received a copy of the GNU General Public License
4515 11 Sep 08 jari 21    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 28   An implementation of the ProgressReporter interface that chains
1355 20 Sep 05 nicklas 29   to another progress reporter while recalculating the percentage
1355 20 Sep 05 nicklas 30   values. This class is useful when you call several methods in a row
1355 20 Sep 05 nicklas 31   that each of them outputs progress messages between 0 and 100 percent.
1355 20 Sep 05 nicklas 32   Use this progress reporter to change the scale so that the first method
1355 20 Sep 05 nicklas 33   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 35   @author Nicklas
1355 20 Sep 05 nicklas 36   @version 2.0
1355 20 Sep 05 nicklas 37   @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 50     Create a new chained progress reporter. All calls to this 
1355 20 Sep 05 nicklas 51     progress reporter will be propagated to the master progress reporter
1355 20 Sep 05 nicklas 52     after the percentage value has been recalculated to be within the specified 
1355 20 Sep 05 nicklas 53     range.
1355 20 Sep 05 nicklas 54     @param master The master progress reporter
1355 20 Sep 05 nicklas 55     @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 64     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 82     Set the range to use for the percentage values. When the
1355 20 Sep 05 nicklas 83     {@link #display(int, String)} method is invoked the percentage
1355 20 Sep 05 nicklas 84     value is recalculated to be within the specified range before the
1355 20 Sep 05 nicklas 85     master progress reporter is invoked.
1355 20 Sep 05 nicklas 86     Ie. 0 --&gt; <code>start</code> and 100 --&gt; <code>end</code>.
4026 30 Nov 07 martin 87      @param start Start value 
4026 30 Nov 07 martin 88      @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 98     Set a message prefix that is pre-pended to message before it is sent to the
6928 04 Jun 15 nicklas 99     master progress reporter.
6928 04 Jun 15 nicklas 100     @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 }