Subversion Repositories public

Rev

Rev 93 | 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.*;
28
 
29
/**
30
 * This class represents a tracked task.
31
 * @author luk
32
 */
33
public class Task implements ActionListener, Comparable<Task> {
34
    private int id;
35
 
36
    private String name = "Unnamed task";
37
    private long consumption = 0L;
93 luk 38
    private double price = 1;
87 luk 39
 
40
    private Timer timer = null;
41
    /**
42
     * period for updating time values [ms]
43
     */
44
    public static final int PERIOD = 10000;
45
 
93 luk 46
    /**
47
     * time units per hour
48
     */
49
    public static final double UNITS_PER_HOUR = 3600000;
50
 
87 luk 51
    private ActionListener listener = null;
52
 
53
    private static int nextId = 0;
54
 
55
    /**
56
     * Generates a new task identifier.
57
     * @return new identifier
58
     */
59
    public static int getNewId() {
60
        int id = nextId;
61
        nextId++;
62
        return id;
63
    }
64
 
65
    /**
66
     * Creates a new instance of Task
67
     */
68
    public Task() {
69
        id = getNewId();
70
    }
71
 
72
    /**
73
     * Creates an instance for a task which has been already tracked.
74
     * @param id task identifier
75
     * @param name task name
76
     * @param consumption up to now time consumption
93 luk 77
     * @param price price per hour
87 luk 78
     */
93 luk 79
    public Task(int id, String name, long consumption, double price) {
87 luk 80
        this.id = id;
81
        this.name = name;
82
        this.consumption = consumption;
93 luk 83
        this.price = price;
87 luk 84
 
85
        if (id >= nextId)
86
            nextId = id + 1;
87
    }
88
 
89
    /**
90
     * Sets the listener where action events should be passed to.
91
     * @param al action listener
92
     */
93
    public void setActionListener(ActionListener al) {
94
        listener = al;
95
    }
96
 
97
    /**
98
     * Returns the task identifier.
99
     * @return task identifier
100
     */
101
    public int getId() {
102
        return id;
103
    }
104
 
105
    /**
106
     * Returns the task name.
107
     * @return task name
108
     */
109
    public String getName() {
110
        return name;
111
    }
112
 
113
    /**
114
     * Sets the task name.
115
     * @param name new task name
116
     */
117
    public void setName(String name) {
118
        this.name = name;
119
    }
120
 
121
    /**
122
     * Returns the current value of cumulative consumption [ms].
123
     * @return current cumulative consumption
124
     */
125
    public long getConsumption() {
126
        return consumption;
127
    }
128
 
129
    /**
130
     * Sets the task cumulative time consumption [ms].
131
     * @param consumption new time consumption value
132
     */
133
    public void setConsumption(long consumption) {
134
        this.consumption = consumption;
135
    }
136
 
137
    /**
93 luk 138
     *  Sets the price per hour [currency unit].
139
     *  Changing this parameter affects the current total price of the task.
140
     * @param price new price value
141
     */
142
    public void setPrice(double price) {
143
        this.price = price;
144
    }
145
 
146
    /**
147
     * Returns the current price per hour [currency unit].
148
     * @return current price value
149
     */
150
    public double getPrice() {
151
        return price;
152
    }
153
 
154
    /**
155
     *  Returns the total price of this task [currency unit].
156
     * @return total price of this task
157
     */
158
    public double getTotalPrice() {
159
        return ((double) consumption) / UNITS_PER_HOUR * price;
160
    }
161
 
162
    /**
87 luk 163
     * Converts the instance to the string representation. It contains
164
     * the task name and consumption.
165
     * @return string representation
166
     */
89 luk 167
    @Override
87 luk 168
    public String toString() {
169
        return name + "(" + Long.toString(consumption) + ")";
170
    }
171
 
172
    /**
173
     * Starts tracking of this task.
174
     */
175
    public void start() {
176
        if (timer != null)
177
            return;
178
 
179
        timer = new Timer(PERIOD, this);
180
        timer.start();
181
    }
182
 
183
    /**
184
     * Stops tracking of this task.
185
     */
186
    public void stop() {
187
        if (timer == null)
188
            return;
189
 
190
        timer.stop();
191
        timer = null;
192
    }
193
 
194
    /**
195
     * Checks whether the task is running.
196
     * @return <CODE>true</CODE> if running, <CODE>false</CODE> otherwise
197
     */
198
    public boolean isRunning() {
199
        return timer != null;
200
    }
201
 
202
    /**
203
     * Updates the time consumption value. Then it creates a new action event
204
     * and passes it to the assigned listener (if any).
205
     * @param e action event
206
     */
207
    public void actionPerformed(ActionEvent e) {
208
        consumption += PERIOD;
209
 
210
        ActionEvent e2 = new ActionEvent(this, id, "");
211
 
212
        if (listener != null)
213
            listener.actionPerformed(e2);
214
    }
215
 
216
    /**
217
     * Compares this task to another one. The comparison result is based
218
     * on task identifiers.
219
     * @param t another task
220
     * @return 1 if this class' id is greater then the given class' id,
221
     * -1 if both ids are equal and 0 otherwise
222
     */
223
    public int compareTo(Task t) {
224
        if (t == null)
225
            throw new NullPointerException("cannot compare to null pointer");
226
 
227
        if (id > t.getId())
228
           return 1;
229
        else if (id == t.getId())
230
            return 0;
231
        else
232
            return -1;
233
    }    
234
 
235
}