Subversion Repositories public

Rev

Rev 87 | Rev 91 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 87 Rev 89
Line 1... Line 1...
1
/*
1
/*
2
 * TaskTableModel.java - implementation of tracked tasks table model
2
 * TaskTableModel.java - implementation of tracked tasks table model
3
 *
3
 *
4
 * Copyright (c) 2006 Lukas Jelinek, http://www.aiken.cz
4
 * Copyright (c) 2006, 2007, 2008 Lukas Jelinek, http://www.aiken.cz
5
 *
5
 *
6
 * ==========================================================================
6
 * ==========================================================================
7
 *
7
 *
8
 * This program is free software; you can redistribute it and/or modify
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
9
 * it under the terms of the GNU General Public License Version 2 as
Line 26... Line 26...
26
import javax.swing.*;
26
import javax.swing.*;
27
import javax.swing.table.*;
27
import javax.swing.table.*;
28
import java.util.*;
28
import java.util.*;
29
import java.io.*;
29
import java.io.*;
30
import java.awt.event.*;
30
import java.awt.event.*;
-
 
31
import java.math.*;
-
 
32
import java.text.*;
31
33
32
/**
34
/**
33
 * This class represents the task table model.
35
 * This class represents the task table model.
34
 * @author luk
36
 * @author luk
35
 */
37
 */
36
public class TaskTableModel extends AbstractTableModel implements ActionListener {
38
public class TaskTableModel extends AbstractTableModel implements ActionListener {
37
   
39
   
38
    private ArrayList<Task> tasks = new ArrayList<Task>();
40
    private ArrayList<Task> tasks = new ArrayList<Task>();
39
    private javax.swing.Timer timer = new javax.swing.Timer(300000, this);
41
    private javax.swing.Timer timer = new javax.swing.Timer(300000, this);
40
   
42
   
-
 
43
    private MessageFormat timeFormat = new MessageFormat("{0,number}:{1,number,00}");
-
 
44
   
-
 
45
    private TaskFrame taskFrame = null;
-
 
46
   
41
    /**
47
    /**
42
     * Creates a new instance of TaskTableModel
48
     * Creates a new instance of TaskTableModel
-
 
49
     * @param tf task frame instance
43
     */
50
     */
44
    public TaskTableModel() {
51
    public TaskTableModel(TaskFrame tf) {
-
 
52
        taskFrame = tf;
45
        loadFromFile();
53
        loadFromFile();
46
        timer.start();
54
        timer.start();
47
    }
55
    }
48
56
49
    /**
57
    /**
Line 53... Line 61...
53
     * @return appropriate cell value; if the arguments are invalid
61
     * @return appropriate cell value; if the arguments are invalid
54
     * it returns <CODE>null</CODE>
62
     * it returns <CODE>null</CODE>
55
     */
63
     */
56
    public Object getValueAt(int rowIndex, int columnIndex) {
64
    public Object getValueAt(int rowIndex, int columnIndex) {
57
        switch (columnIndex) {
65
        switch (columnIndex) {
-
 
66
            case 0:
58
            case 0: return tasks.get(rowIndex).getName();
67
                return tasks.get(rowIndex).getName();
-
 
68
            case 1:
59
            case 1: return new Long(tasks.get(rowIndex).getConsumption() / 60000);
69
                long mins = tasks.get(rowIndex).getConsumption() / 60000;
-
 
70
                BigDecimal hm[] = new BigDecimal((int) mins).divideAndRemainder(new BigDecimal(60));
-
 
71
                return timeFormat.format(hm);
60
            default: return null;
72
            default: return null;
61
        }
73
        }
62
    }
74
    }
63
75
64
    /**
76
    /**
Line 82... Line 94...
82
     * coordinates is invalid it does nothing.
94
     * coordinates is invalid it does nothing.
83
     * @param aValue new value
95
     * @param aValue new value
84
     * @param rowIndex row index
96
     * @param rowIndex row index
85
     * @param columnIndex column index
97
     * @param columnIndex column index
86
     */
98
     */
-
 
99
    @Override
87
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
100
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
88
        switch (columnIndex) {
101
        switch (columnIndex) {
89
            case 0: tasks.get(rowIndex).setName((String) aValue);
102
            case 0: tasks.get(rowIndex).setName((String) aValue);
90
                break;
103
                break;
91
            case 1: tasks.get(rowIndex).setConsumption(((Long) aValue).longValue());
-
 
92
                break;
-
 
93
        }
104
        }
94
    }
105
    }
95
106
96
    /**
107
    /**
97
     * Returns the name of the given column.
108
     * Returns the name of the given column.
98
     * @param column column index
109
     * @param column column index
99
     * @return column name; if the index is invalid it returns an empty
110
     * @return column name; if the index is invalid it returns an empty
100
     * string
111
     * string
101
     */
112
     */
-
 
113
    @Override
102
    public String getColumnName(int column) {
114
    public String getColumnName(int column) {
103
        switch (column) {
115
        switch (column) {
104
            case 0: return "Task name";
116
            case 0: return "Task name";
105
            case 1: return "Time consumption [min]";
117
            case 1: return "Time consumption [h:min]";
106
            default: return "";
118
            default: return "";
107
        }
119
        }
108
    }
120
    }
109
121
110
    /**
122
    /**
111
     * Returns the class of the given column.
123
     * Returns the class of the given column.
112
     * @param columnIndex column index
124
     * @param columnIndex column index
113
     * @return appropriate class object; if the column index is invalid
125
     * @return appropriate class object; if the column index is invalid
114
     * it returns <CODE>Void.class</CODE>.
126
     * it returns <CODE>Void.class</CODE>.
115
     */
127
     */
-
 
128
    @Override
116
    public Class<?> getColumnClass(int columnIndex) {
129
    public Class<?> getColumnClass(int columnIndex) {
117
        switch (columnIndex) {
130
        switch (columnIndex) {
118
            case 0: return String.class;
131
            case 0: return String.class;
119
            case 1: return Long.class;
132
            case 1: return StringBuffer.class;
120
            default: return Void.class;
133
            default: return Void.class;
121
        }
134
        }
122
    }
135
    }
123
136
124
    /**
137
    /**
Line 126... Line 139...
126
     * @param rowIndex row index
139
     * @param rowIndex row index
127
     * @param columnIndex column index
140
     * @param columnIndex column index
128
     * @return <CODE>true</CODE> for the first column (index 0),
141
     * @return <CODE>true</CODE> for the first column (index 0),
129
     * <CODE>false</CODE> otherwise
142
     * <CODE>false</CODE> otherwise
130
     */
143
     */
-
 
144
    @Override
131
    public boolean isCellEditable(int rowIndex, int columnIndex) {
145
    public boolean isCellEditable(int rowIndex, int columnIndex) {
132
        return columnIndex == 0;
146
        return columnIndex == 0;
133
    }
147
    }
134
   
148
   
135
    /**
149
    /**
Line 196... Line 210...
196
       
210
       
197
        fireTableDataChanged();
211
        fireTableDataChanged();
198
    }
212
    }
199
   
213
   
200
    /**
214
    /**
-
 
215
     * Resets the given tasks.
-
 
216
     * @param start index of the first resetted task
-
 
217
     * @param end index of the first NOT resetted task (the first task beyond the interval)
-
 
218
     */
-
 
219
    public void resetTasks(int start, int end) {
-
 
220
        for (int i=start; i<=end; i++) {
-
 
221
            Task t = tasks.get(i);
-
 
222
            t.setConsumption(0);
-
 
223
            fireTableCellUpdated(i, 1);
-
 
224
        }
-
 
225
    }
-
 
226
   
-
 
227
    /**
201
     * Destroys the timer controlling automatic data saving.
228
     * Destroys the timer controlling automatic data saving.
202
     */
229
     */
203
    public void cancelAutoSave() {
230
    public void cancelAutoSave() {
204
        timer.stop();
231
        timer.stop();
205
    }
232
    }
Line 247... Line 274...
247
            FileInputStream is = new FileInputStream(file);
274
            FileInputStream is = new FileInputStream(file);
248
            Properties props = new Properties();
275
            Properties props = new Properties();
249
            props.loadFromXML(is);
276
            props.loadFromXML(is);
250
            is.close();
277
            is.close();
251
           
278
           
-
 
279
            taskFrame.setStartSettings(props);
-
 
280
           
252
            Iterator<Object> it = props.keySet().iterator();
281
            Iterator<Object> it = props.keySet().iterator();
253
            while (it.hasNext()) {
282
            while (it.hasNext()) {
254
                String key = (String) it.next();
283
                String key = (String) it.next();
255
                if (key.endsWith(".name")) {
284
                if (key.endsWith(".name")) {
256
                    String ids = key.substring(0, key.length() - 5);
285
                    String ids = key.substring(0, key.length() - 5);
Line 288... Line 317...
288
                return;
317
                return;
289
            }
318
            }
290
        }
319
        }
291
       
320
       
292
        Properties props = new Properties();
321
        Properties props = new Properties();
-
 
322
        props.setProperty("window.location.x", Integer.toString(taskFrame.getX()));
-
 
323
        props.setProperty("window.location.y", Integer.toString(taskFrame.getY()));
-
 
324
        props.setProperty("window.size.w", Integer.toString(taskFrame.getWidth()));
-
 
325
        props.setProperty("window.size.h", Integer.toString(taskFrame.getHeight()));
293
        for (int i=0; i<tasks.size(); i++) {
326
        for (int i=0; i<tasks.size(); i++) {
294
            Task t = tasks.get(i);
327
            Task t = tasks.get(i);
295
            String id = Integer.toString(t.getId());
328
            String id = Integer.toString(t.getId());
296
            props.setProperty(id + ".name", t.getName());
329
            props.setProperty(id + ".name", t.getName());
297
            props.setProperty(id + ".consumption", Long.toString(t.getConsumption()));
330
            props.setProperty(id + ".consumption", Long.toString(t.getConsumption()));