Rev 89 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
87 | luk | 1 | /* |
2 | * Task.java - implementation of tracked task |
||
3 | * |
||
89 | luk | 4 | * Copyright (c) 2006, 2007, 2008 Lukas Jelinek, http://www.aiken.cz |
87 | luk | 5 | * |
6 | * ========================================================================== |
||
7 | * |
||
8 | * This program is free software; you can redistribute it and/or modify |
||
9 | * it under the terms of the GNU General Public License Version 2 as |
||
10 | * published by the Free Software Foundation. |
||
11 | * |
||
12 | * This program is distributed in the hope that it will be useful, |
||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
15 | * GNU General Public License for more details. |
||
16 | * |
||
17 | * You should have received a copy of the GNU General Public License |
||
18 | * along with this program; if not, write to the Free Software |
||
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
20 | * |
||
21 | * ========================================================================== |
||
22 | */ |
||
23 | |||
24 | package cz.aiken.util.lwtt; |
||
25 | |||
26 | import javax.swing.Timer; |
||
27 | import java.awt.event.*; |
||
93 | luk | 28 | import java.math.*; |
87 | luk | 29 | |
30 | /** |
||
31 | * This class represents a tracked task. |
||
32 | * @author luk |
||
33 | */ |
||
34 | public class Task implements ActionListener, Comparable<Task> { |
||
35 | private int id; |
||
36 | |||
37 | private String name = "Unnamed task"; |
||
38 | private long consumption = 0L; |
||
93 | luk | 39 | private double price = 1; |
87 | luk | 40 | |
41 | private Timer timer = null; |
||
42 | /** |
||
43 | * period for updating time values [ms] |
||
44 | */ |
||
45 | public static final int PERIOD = 10000; |
||
46 | |||
93 | luk | 47 | /** |
48 | * time units per hour |
||
49 | */ |
||
50 | public static final double UNITS_PER_HOUR = 3600000; |
||
51 | |||
87 | luk | 52 | private ActionListener listener = null; |
53 | |||
54 | private static int nextId = 0; |
||
55 | |||
56 | /** |
||
57 | * Generates a new task identifier. |
||
58 | * @return new identifier |
||
59 | */ |
||
60 | public static int getNewId() { |
||
61 | int id = nextId; |
||
62 | nextId++; |
||
63 | return id; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Creates a new instance of Task |
||
68 | */ |
||
69 | public Task() { |
||
70 | id = getNewId(); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Creates an instance for a task which has been already tracked. |
||
75 | * @param id task identifier |
||
76 | * @param name task name |
||
77 | * @param consumption up to now time consumption |
||
93 | luk | 78 | * @param price price per hour |
87 | luk | 79 | */ |
93 | luk | 80 | public Task(int id, String name, long consumption, double price) { |
87 | luk | 81 | this.id = id; |
82 | this.name = name; |
||
83 | this.consumption = consumption; |
||
93 | luk | 84 | this.price = price; |
87 | luk | 85 | |
86 | if (id >= nextId) |
||
87 | nextId = id + 1; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Sets the listener where action events should be passed to. |
||
92 | * @param al action listener |
||
93 | */ |
||
94 | public void setActionListener(ActionListener al) { |
||
95 | listener = al; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Returns the task identifier. |
||
100 | * @return task identifier |
||
101 | */ |
||
102 | public int getId() { |
||
103 | return id; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Returns the task name. |
||
108 | * @return task name |
||
109 | */ |
||
110 | public String getName() { |
||
111 | return name; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Sets the task name. |
||
116 | * @param name new task name |
||
117 | */ |
||
118 | public void setName(String name) { |
||
119 | this.name = name; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Returns the current value of cumulative consumption [ms]. |
||
124 | * @return current cumulative consumption |
||
125 | */ |
||
126 | public long getConsumption() { |
||
127 | return consumption; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Sets the task cumulative time consumption [ms]. |
||
132 | * @param consumption new time consumption value |
||
133 | */ |
||
134 | public void setConsumption(long consumption) { |
||
135 | this.consumption = consumption; |
||
136 | } |
||
137 | |||
138 | /** |
||
93 | luk | 139 | * Sets the price per hour [currency unit]. |
140 | * Changing this parameter affects the current total price of the task. |
||
141 | * @param price new price value |
||
142 | */ |
||
143 | public void setPrice(double price) { |
||
144 | this.price = price; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Returns the current price per hour [currency unit]. |
||
149 | * @return current price value |
||
150 | */ |
||
151 | public double getPrice() { |
||
152 | return price; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Returns the total price of this task [currency unit]. |
||
157 | * @return total price of this task |
||
158 | */ |
||
159 | public double getTotalPrice() { |
||
160 | return ((double) consumption) / UNITS_PER_HOUR * price; |
||
161 | } |
||
162 | |||
163 | /** |
||
87 | luk | 164 | * Converts the instance to the string representation. It contains |
165 | * the task name and consumption. |
||
166 | * @return string representation |
||
167 | */ |
||
89 | luk | 168 | @Override |
87 | luk | 169 | public String toString() { |
170 | return name + "(" + Long.toString(consumption) + ")"; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Starts tracking of this task. |
||
175 | */ |
||
176 | public void start() { |
||
177 | if (timer != null) |
||
178 | return; |
||
179 | |||
180 | timer = new Timer(PERIOD, this); |
||
181 | timer.start(); |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Stops tracking of this task. |
||
186 | */ |
||
187 | public void stop() { |
||
188 | if (timer == null) |
||
189 | return; |
||
190 | |||
191 | timer.stop(); |
||
192 | timer = null; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Checks whether the task is running. |
||
197 | * @return <CODE>true</CODE> if running, <CODE>false</CODE> otherwise |
||
198 | */ |
||
199 | public boolean isRunning() { |
||
200 | return timer != null; |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Updates the time consumption value. Then it creates a new action event |
||
205 | * and passes it to the assigned listener (if any). |
||
206 | * @param e action event |
||
207 | */ |
||
208 | public void actionPerformed(ActionEvent e) { |
||
209 | consumption += PERIOD; |
||
210 | |||
211 | ActionEvent e2 = new ActionEvent(this, id, ""); |
||
212 | |||
213 | if (listener != null) |
||
214 | listener.actionPerformed(e2); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Compares this task to another one. The comparison result is based |
||
219 | * on task identifiers. |
||
220 | * @param t another task |
||
221 | * @return 1 if this class' id is greater then the given class' id, |
||
222 | * -1 if both ids are equal and 0 otherwise |
||
223 | */ |
||
224 | public int compareTo(Task t) { |
||
225 | if (t == null) |
||
226 | throw new NullPointerException("cannot compare to null pointer"); |
||
227 | |||
228 | if (id > t.getId()) |
||
229 | return 1; |
||
230 | else if (id == t.getId()) |
||
231 | return 0; |
||
232 | else |
||
233 | return -1; |
||
234 | } |
||
235 | |||
236 | } |