View Javadoc
1   package fr.ifremer.adagio.synchro.type;
2   
3   /*
4    * #%L
5    * Ifremer shared :: Application
6    * $Id: ApplicationProgressionModel.java 1379 2013-11-24 22:44:33Z tchemit $
7    * $HeadURL: http://svn.forge.codelutin.com/svn/tutti/application/tags/ifremer-shared-1.0.1/application/src/main/java/fr/ifremer/shared/application/type/ApplicationProgressionModel.java $
8    * %%
9    * Copyright (C) 2013 Ifremer, CodeLutin, Tony CHEMIT
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import java.beans.PropertyChangeSupport;
27  
28  /**
29   * Simple model for a progression long task.
30   * 
31   * @author tchemit <chemit@codelutin.com>
32   * @since 1.0
33   */
34  public class ProgressionModel extends PropertyChangeSupport {
35  
36  	private static final long serialVersionUID = 1L;
37  
38  	public static final String PROPERTY_TOTAL = "total";
39  
40  	public static final String PROPERTY_CURRENT = "current";
41  
42  	public static final String PROPERTY_RATE = "rate";
43  
44  	public static final String PROPERTY_MESSAGE = "message";
45  
46  	protected int total;
47  
48  	protected int current;
49  
50  	protected float rate;
51  
52  	protected String message;
53  
54  	/**
55  	 * Constructs a <code>ApplicationProgressionModel</code> object.
56  	 * 
57  	 * @param sourceBean
58  	 *            The bean to be given as the source for any events.
59  	 */
60  	public ProgressionModel(Object sourceBean) {
61  		super(sourceBean);
62  	}
63  
64  	public int getTotal() {
65  		return total;
66  	}
67  
68  	public void setTotal(int total) {
69  		Object oldValue = getTotal();
70  		this.total = total;
71  		firePropertyChange(PROPERTY_TOTAL, oldValue, total);
72  		setCurrent(0);
73  	}
74  
75  	public void adaptTotal(int total) {
76  		int current = getCurrent();
77  		setTotal(total);
78  		increments(current);
79  		Object oldValue = getTotal();
80  		this.total = total;
81  		firePropertyChange(PROPERTY_TOTAL, oldValue, total);
82  		setCurrent(current);
83  	}
84  
85  	public int getCurrent() {
86  		return current;
87  	}
88  
89  	public void setCurrent(int current) {
90  		Object oldValue = getCurrent();
91  		this.current = current;
92  		firePropertyChange(PROPERTY_CURRENT, oldValue, current);
93  	}
94  
95  	public void increments(int nb) {
96  		setCurrent(current + nb);
97  		setRate(getCurrent() / getTotal());
98  	}
99  
100 	public float getRate() {
101 		return rate;
102 	}
103 
104 	public void setRate(float rate) {
105 		Object oldValue = getRate();
106 		this.rate = rate;
107 		firePropertyChange(PROPERTY_RATE, oldValue, rate);
108 	}
109 
110 	public String getMessage() {
111 		return message;
112 	}
113 
114 	public void increments(String message) {
115 		increments(1);
116 		setMessage(message);
117 	}
118 
119 	public void setMessage(String message) {
120 		Object oldValue = getMessage();
121 		this.message = message;
122 		firePropertyChange(PROPERTY_MESSAGE, oldValue, message);
123 	}
124 }