Subversion Repositories public

Rev

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