1 /*
2 * Scope: a generic MVC framework.
3 * Copyright (c) 2000-2002, The Scope team
4 * All rights reserved.
5 *
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * Neither the name "Scope" nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 *
36 * $Id: TimesheetModel.java,v 1.4 2002/09/05 15:41:50 ludovicc Exp $
37 */
38 package samples.swing.timesheet.model;
39
40 import java.util.Iterator;
41 import java.util.Timer;
42 import java.util.TimerTask;
43
44 import org.scopemvc.core.ModelChangeEvent;
45 import org.scopemvc.core.Selector;
46 import org.scopemvc.model.basic.BasicModel;
47 import org.scopemvc.model.collection.ListModel;
48 import org.scopemvc.util.DateTime;
49 import org.scopemvc.util.Time;
50
51 /***
52 * <p>
53 *
54 * </p>
55 *
56 * @author <a href="mailto:steve.jones@netdecisions.co.uk>Steve Jones</a>
57 * @created 05 September 2002
58 * @since Scope v0.8
59 * @version $Revision: 1.4 $ $Date: 2002/09/05 15:41:50 $
60 */
61 public class TimesheetModel extends BasicModel {
62
63 /***
64 * TODO: describe of the Field
65 */
66 public final static Selector WORK_ITEMS = Selector.fromString("workItems");
67 /***
68 * TODO: describe of the Field
69 */
70 public final static Selector TOTAL_WORK = Selector.fromString("totalWork");
71 /***
72 * TODO: describe of the Field
73 */
74 public final static Selector CURRENT_PROJECT = Selector.fromString("currentProject");
75 /***
76 * TODO: describe of the Field
77 */
78 public final static Selector CURRENT_ITEM = Selector.fromString("currentItem");
79 /***
80 * TODO: describe of the Field
81 */
82 public final static Selector PROJECT_TOTAL = Selector.fromString("projectTotal");
83
84 private ListModel workItems;
85 private Time totalWork;
86 private String currentProject;
87 private Time projectTotal;
88 private WorkItemModel currentItem;
89
90 /***
91 * The timer stuff
92 */
93 private Timer timer;
94
95
96 /***
97 * Constructor for the TimesheetModel object
98 *
99 * @param workItems TODO: Describe the Parameter
100 * @param totalWork TODO: Describe the Parameter
101 * @param currentProject TODO: Describe the Parameter
102 * @param projectTotal TODO: Describe the Parameter
103 */
104 public TimesheetModel(ListModel workItems, Time totalWork, String currentProject, Time projectTotal) {
105 this.workItems = workItems;
106 this.totalWork = totalWork;
107 this.currentProject = currentProject;
108 this.projectTotal = projectTotal;
109 this.listenNewSubmodel(WORK_ITEMS);
110 }
111
112 /***
113 * Constructor for the TimesheetModel object
114 */
115 public TimesheetModel() {
116 this(new ListModel(), new Time(0), null, new Time(0));
117 }
118
119 /***
120 * Gets the work items
121 *
122 * @return The workItems value
123 */
124 public ListModel getWorkItems() {
125 return workItems;
126 }
127
128
129 /***
130 * Gets the total work
131 *
132 * @return The totalWork value
133 */
134 public Time getTotalWork() {
135 return totalWork;
136 }
137
138 /***
139 * Gets the current project
140 *
141 * @return The currentProject value
142 */
143 public String getCurrentProject() {
144 return currentProject;
145 }
146
147 /***
148 * Gets the project total
149 *
150 * @return The projectTotal value
151 */
152 public Time getProjectTotal() {
153 return projectTotal;
154 }
155
156 /***
157 * Sets the work items
158 *
159 * @param workItems The new workItems value
160 */
161 public void setWorkItems(ListModel workItems) {
162 this.unlistenOldSubmodel(WORK_ITEMS);
163 this.workItems = workItems;
164 this.listenNewSubmodel(WORK_ITEMS);
165 this.fireModelChange(VALUE_CHANGED, WORK_ITEMS);
166 }
167
168 /***
169 * Sets the total work
170 *
171 * @param totalWork The new totalWork value
172 */
173 public void setTotalWork(Time totalWork) {
174 this.totalWork = totalWork;
175 this.fireModelChange(VALUE_CHANGED, TOTAL_WORK);
176 }
177
178 /***
179 * Sets the current project
180 *
181 * @param currentProject The new currentProject value
182 */
183 public void setCurrentProject(String currentProject) {
184 this.currentProject = currentProject;
185 if (currentProject != null) {
186 // also need to create a new work item
187 WorkItemModel newItem = new WorkItemModel(currentProject, new DateTime(), new Time(0));
188 workItems.add(newItem);
189 this.currentItem = newItem;
190 } else {
191 currentItem = null;
192 }
193 this.runTimer();
194 this.fireModelChange(VALUE_CHANGED, CURRENT_PROJECT);
195 recalculate();
196 }
197
198 /***
199 * Sets the project total
200 *
201 * @param projectTotal The new projectTotal value
202 */
203 public void setProjectTotal(Time projectTotal) {
204 this.projectTotal = projectTotal;
205 this.fireModelChange(VALUE_CHANGED, PROJECT_TOTAL);
206 }
207
208 /***
209 * <p>
210 *
211 * Overide of the model change event, this is done so additional processing
212 * can be done on the model change when the work items changes. </p>
213 *
214 * @param inEvent the event received from a child ModelChangeEventSource.
215 */
216 public void modelChanged(ModelChangeEvent inEvent) {
217 recalculate();
218
219 super.modelChanged(inEvent);
220 }
221
222 /***
223 * Recalculate the information in the timesheet reporting fields.
224 */
225 private void recalculate() {
226 Time newProjectTotal = new Time(0);
227 Time newTotalWork = new Time(0);
228
229 for (Iterator iterator = workItems.iterator(); iterator.hasNext(); ) {
230 WorkItemModel workItemModel = (WorkItemModel) iterator.next();
231 newTotalWork.setTime(newTotalWork.getTime() + workItemModel.getDuration().getTime());
232 if (workItemModel.getProject().equals(this.getCurrentProject())) {
233 newProjectTotal.setTime(newProjectTotal.getTime() + workItemModel.getDuration().getTime());
234 }
235 }
236 this.setProjectTotal(newProjectTotal);
237 this.setTotalWork(newTotalWork);
238 }
239
240 /***
241 * Start the timer if not already started
242 */
243 private void runTimer() {
244 if (timer == null) {
245 timer = new Timer(true);
246 timer.scheduleAtFixedRate(new TimesheetTimerTask(), 1000, 1000);
247 }
248 }
249
250 /***
251 * Inner class to handle the timer task stuff
252 *
253 * @author lclaude
254 * @created 05 September 2002
255 */
256 class TimesheetTimerTask extends TimerTask {
257 /***
258 * Main processing method for the TimesheetTimerTask object
259 */
260 public void run() {
261 if (currentItem != null) {
262 currentItem.incrementDuration();
263 }
264 }
265 }
266 }
This page was automatically generated by Maven