View Javadoc

1   /***
2    * BaseTaskActivityMonitor.java
3    *
4    * This file is part of the creme library.
5    * The creme library intends to ease the development effort of large
6    * applications by providing easy to use support classes.
7    *
8    * Copyright (C) 2002 Denis Bregeon
9    *
10   * This library is free software; you can redistribute it and/or
11   * modify it under the terms of the GNU Lesser General Public
12   * License as published by the Free Software Foundation; either
13   * version 2.1 of the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   * Lesser General Public License for more details.
19   *
20   * You should have received a copy of the GNU Lesser General Public
21   * License along with this library; if not, write to the Free Software
22   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23   *
24   * contact information: dbregeon@sourceforge.net
25   */
26  package org.jcreme.processing;
27  
28  import java.beans.PropertyChangeListener;
29  import java.beans.PropertyChangeSupport;
30  import java.util.Date;
31  
32  /***
33   * This is a simple implementation of the TaskActivityMonitor interface. Objects
34   * of this class can be used to display information about how the task is doing.
35   * 
36   * @author $Author: dbregeon $
37   * @version $Revision: 1.2 $
38   */
39  public class BaseTaskActivityMonitor implements TaskActivityMonitor {
40  	/***
41  	 * A Name that identifies the monitored task.
42  	 */
43  	private String taskName;
44  
45  	/***
46  	 * The current completion percentage of the task.
47  	 */
48  	private Double completionPercentage;
49  
50  	/***
51  	 * The date when the task started.
52  	 */
53  	private Date startDate;
54  
55  	/***
56  	 * The date when the task ended.
57  	 */
58  	private Date endDate;
59  
60  	/***
61  	 * Change support change events.
62  	 */
63  	private final PropertyChangeSupport propertySupport;
64  
65  	/***
66  	 * Creates new DefaultTaskActivityMonitor
67  	 */
68  	public BaseTaskActivityMonitor() {
69  		this.propertySupport = new PropertyChangeSupport(this);
70  	}
71  
72  	/***
73  	 * Gives access to the task's name.
74  	 * 
75  	 * @return a name associated to the task.
76  	 */
77  	public String getTaskName() {
78  		return this.taskName;
79  	}
80  
81  	/***
82  	 * Changes the name of the task.
83  	 * 
84  	 * @param value
85  	 *            The name for the task.
86  	 */
87  	public void setTaskName(final String value) {
88  		final String oldValue = this.taskName;
89  		this.taskName = value;
90  		firePropertyChange(TASK_NAME_PROPERTY, oldValue, this.taskName);
91  	}
92  
93  	/***
94  	 * Gives access to the completion percentage of the task.
95  	 * 
96  	 * @return A percentage of completion for the task.
97  	 */
98  	public Double getCompletionPercentage() {
99  		return this.completionPercentage;
100 	}
101 
102 	/***
103 	 * Changes the completion percentage of the task.
104 	 * 
105 	 * @param value
106 	 *            The new completion percentage for this task.
107 	 */
108 	public void setCompletionPercentage(final Double value) {
109 		final Double oldValue = this.completionPercentage;
110 		this.completionPercentage = value;
111 		firePropertyChange(COMPLETION_PERCENTAGE_PROPERTY, oldValue,
112 				this.completionPercentage);
113 	}
114 
115 	/***
116 	 * Gives access to the date when the task started.
117 	 * 
118 	 * @return the date when the task was started or null if the task has not
119 	 *         started.
120 	 */
121 	public Date getStartDate() {
122 		return this.startDate;
123 	}
124 
125 	/***
126 	 * Sets the start date of the task.
127 	 * 
128 	 * @param value
129 	 *            The start date.
130 	 */
131 	public void setStartDate(final Date value) {
132 		final Date oldValue = this.startDate;
133 		this.startDate = value;
134 		firePropertyChange(START_DATE_PROPERTY, oldValue, this.startDate);
135 	}
136 
137 	/***
138 	 * Gives access to the task's completion date.
139 	 * 
140 	 * @return the date when the task was finished or null if the task is not
141 	 *         finished.
142 	 */
143 	public Date getEndDate() {
144 		return this.endDate;
145 	}
146 
147 	/***
148 	 * Sets the end date of the task.
149 	 * 
150 	 * @param value
151 	 *            The end date.
152 	 */
153 	public void setEndDate(final Date value) {
154 		final Date oldValue = this.endDate;
155 		this.endDate = value;
156 		firePropertyChange(END_DATE_PROPERTY, oldValue, this.endDate);
157 	}
158 
159 	/***
160 	 * Provides the elapsed time since the task started.
161 	 * 
162 	 * @return the difference between the start date and the end date. Null when
163 	 *         the getStartDate method returns null. The difference between a
164 	 *         newly created Date and the start date if the getEndDate method
165 	 *         returns null and the getStartDate does not.
166 	 */
167 	public Long getElapsedTime() {
168 		Long result = null;
169 		if (!(this.startDate == null)) {
170 			if (!(this.endDate == null)) {
171 				result = new Long(this.endDate.getTime()
172 						- this.startDate.getTime());
173 			} else {
174 				result = new Long(new Date().getTime()
175 						- this.startDate.getTime());
176 			}
177 		}
178 		return result;
179 	}
180 
181 	/***
182 	 * @param listener
183 	 *            a property change listener as defined by the beans
184 	 *            conventions. It should at least be reported any change to the
185 	 *            following properties: completionPercentage, startDate,
186 	 *            endDate.
187 	 */
188 	public void addPropertyChangeListener(final PropertyChangeListener listener) {
189 		this.propertySupport.addPropertyChangeListener(listener);
190 	}
191 
192 	/***
193 	 * @param propertyName
194 	 *            the name of a property that the listener has to monitor.
195 	 * @param listener
196 	 *            a property change listener as defined by the beans
197 	 *            conventions.
198 	 */
199 	public void addPropertyChangeListener(final String propertyName,
200 			final PropertyChangeListener listener) {
201 		this.propertySupport.addPropertyChangeListener(propertyName, listener);
202 	}
203 
204 	/***
205 	 * @param listener
206 	 *            the listener that must be removed from the event diffusion
207 	 *            list.
208 	 */
209 	public void removePropertyChangeListener(
210 			final PropertyChangeListener listener) {
211 		this.propertySupport.removePropertyChangeListener(listener);
212 	}
213 
214 	/***
215 	 * @param propertyName
216 	 *            the property for which changes won't be monitored anymore.
217 	 * @param listener
218 	 *            the listener that must be removed from the event diffusion
219 	 *            list for that property.
220 	 */
221 	public void removePropertyChangeListener(final String propertyName,
222 			final PropertyChangeListener listener) {
223 		this.propertySupport.removePropertyChangeListener(propertyName,
224 				listener);
225 	}
226 
227 	/***
228 	 * Fires a PropertyChangeEvent to the registered listener for an Object
229 	 * property.
230 	 * 
231 	 * @param propertyName
232 	 *            the property that was modified.
233 	 * @param oldValue
234 	 *            the previous value for the property.
235 	 * @param newValue
236 	 *            the new value for the property.
237 	 */
238 	protected void firePropertyChange(final String propertyName,
239 			final Object oldValue, final Object newValue) {
240 		this.propertySupport.firePropertyChange(propertyName, oldValue,
241 				newValue);
242 	}
243 
244 	/***
245 	 * Fires a PropertyChangeEvent to the registered listener for an int
246 	 * property.
247 	 * 
248 	 * @param propertyName
249 	 *            the property that was modified.
250 	 * @param oldValue
251 	 *            the previous value for the property.
252 	 * @param newValue
253 	 *            the new value for the property.
254 	 */
255 	protected void firePropertyChange(final String propertyName,
256 			final int oldValue, final int newValue) {
257 		this.propertySupport.firePropertyChange(propertyName, oldValue,
258 				newValue);
259 	}
260 }