Subversion Repositories public

Rev

Rev 89 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
87 luk 1
/*
2
 * Task.java - implementation of tracked task
3
 *
4
 * Copyright (c) 2006 Lukas Jelinek, http://www.aiken.cz
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;
38
 
39
    private Timer timer = null;
40
    /**
41
     * period for updating time values [ms]
42
     */
43
    public static final int PERIOD = 10000;
44
 
45
    private ActionListener listener = null;
46
 
47
    private static int nextId = 0;
48
 
49
    /**
50
     * Generates a new task identifier.
51
     * @return new identifier
52
     */
53
    public static int getNewId() {
54
        int id = nextId;
55
        nextId++;
56
        return id;
57
    }
58
 
59
 
60
 
61
    /**
62
     * Creates a new instance of Task
63
     */
64
    public Task() {
65
        id = getNewId();
66
    }
67
 
68
    /**
69
     * Creates an instance for a task which has been already tracked.
70
     * @param id task identifier
71
     * @param name task name
72
     * @param consumption up to now time consumption
73
     */
74
    public Task(int id, String name, long consumption) {
75
        this.id = id;
76
        this.name = name;
77
        this.consumption = consumption;
78
 
79
        if (id >= nextId)
80
            nextId = id + 1;
81
    }
82
 
83
    /**
84
     * Sets the listener where action events should be passed to.
85
     * @param al action listener
86
     */
87
    public void setActionListener(ActionListener al) {
88
        listener = al;
89
    }
90
 
91
    /**
92
     * Returns the task identifier.
93
     * @return task identifier
94
     */
95
    public int getId() {
96
        return id;
97
    }
98
 
99
    /**
100
     * Returns the task name.
101
     * @return task name
102
     */
103
    public String getName() {
104
        return name;
105
    }
106
 
107
    /**
108
     * Sets the task name.
109
     * @param name new task name
110
     */
111
    public void setName(String name) {
112
        this.name = name;
113
    }
114
 
115
    /**
116
     * Returns the current value of cumulative consumption [ms].
117
     * @return current cumulative consumption
118
     */
119
    public long getConsumption() {
120
        return consumption;
121
    }
122
 
123
    /**
124
     * Sets the task cumulative time consumption [ms].
125
     * @param consumption new time consumption value
126
     */
127
    public void setConsumption(long consumption) {
128
        this.consumption = consumption;
129
    }
130
 
131
    /**
132
     * Converts the instance to the string representation. It contains
133
     * the task name and consumption.
134
     * @return string representation
135
     */
136
    public String toString() {
137
        return name + "(" + Long.toString(consumption) + ")";
138
    }
139
 
140
    /**
141
     * Starts tracking of this task.
142
     */
143
    public void start() {
144
        if (timer != null)
145
            return;
146
 
147
        timer = new Timer(PERIOD, this);
148
        timer.start();
149
    }
150
 
151
    /**
152
     * Stops tracking of this task.
153
     */
154
    public void stop() {
155
        if (timer == null)
156
            return;
157
 
158
        timer.stop();
159
        timer = null;
160
    }
161
 
162
    /**
163
     * Checks whether the task is running.
164
     * @return <CODE>true</CODE> if running, <CODE>false</CODE> otherwise
165
     */
166
    public boolean isRunning() {
167
        return timer != null;
168
    }
169
 
170
    /**
171
     * Updates the time consumption value. Then it creates a new action event
172
     * and passes it to the assigned listener (if any).
173
     * @param e action event
174
     */
175
    public void actionPerformed(ActionEvent e) {
176
        consumption += PERIOD;
177
 
178
        ActionEvent e2 = new ActionEvent(this, id, "");
179
 
180
        if (listener != null)
181
            listener.actionPerformed(e2);
182
    }
183
 
184
    /**
185
     * Compares this task to another one. The comparison result is based
186
     * on task identifiers.
187
     * @param t another task
188
     * @return 1 if this class' id is greater then the given class' id,
189
     * -1 if both ids are equal and 0 otherwise
190
     */
191
    public int compareTo(Task t) {
192
        if (t == null)
193
            throw new NullPointerException("cannot compare to null pointer");
194
 
195
        if (id > t.getId())
196
           return 1;
197
        else if (id == t.getId())
198
            return 0;
199
        else
200
            return -1;
201
    }    
202
 
203
}