Subversion Repositories public

Rev

Rev 85 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
85 luk 1
/*
2
 * ICTableModel.java - jIncron table model class
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.jincron;
25
 
26
import javax.swing.table.*;
27
import java.util.*;
28
import java.io.*;
29
 
30
/**
31
 *
32
 * @author luk
33
 */
34
public class ICTableModel extends AbstractTableModel {
35
 
36
    private ArrayList<ICRow> rows = new ArrayList<ICRow>();
37
 
38
    /** Creates a new instance of ICTableModel */
39
    public ICTableModel() {
40
    }
41
 
42
    public Object getValueAt(int rowIndex, int columnIndex) {
43
        ICRow r = rows.get(rowIndex);
44
        Object o = null;
45
 
46
        switch (columnIndex) {
47
            case 0: o = r.getPath();
48
                break;
49
            case 1: o = r.getEvent().toString();
50
                break;
51
            case 2: o = r.getCommand();
52
                break;
53
            default:;
54
        }
55
 
56
        return o;
57
    }
58
 
59
    public int getRowCount() {
60
        return rows.size();
61
    }
62
 
63
    public int getColumnCount() {
64
        return ICRow.getFieldCount();
65
    }
66
 
67
    public String getColumnName(int column) {
68
        switch (column) {
69
            case 0: return "Path";
70
            case 1: return "Events";
71
            case 2: return "Command";
72
            default: return "";
73
        }
74
    }
75
 
76
    public ICRow getRow(int index) {
77
        return rows.get(index);
78
    }
79
 
80
    public void updateRow(int index, ICRow row) {
81
        rows.set(index, row);
82
        fireTableRowsUpdated(index, index);
83
    }
84
 
85
    public void addRow(ICRow r) {
86
        rows.add(r);
87
        int index = rows.size() - 1;
88
        fireTableRowsInserted(index, index);
89
    }
90
 
91
    public void removeRows(int start, int end) {
92
        for (int i=end; i>=start; i--)
93
            rows.remove(i);
94
        fireTableRowsDeleted(start, end);
95
    }
96
 
97
    public void clear() {
98
        int size = rows.size();
99
        rows.clear();
100
        fireTableRowsDeleted(0, size - 1);
101
    }
102
 
103
    public String toString() {
104
        StringBuilder sb = new StringBuilder();
105
        Iterator<ICRow> it = rows.iterator();
106
        String sep = System.getProperty("line.separator");
107
 
108
        while (it.hasNext()) {
109
            ICRow r = it.next();
110
            sb.append(r.toString());
111
            sb.append(sep);
112
        }
113
 
114
        return sb.toString();
115
    }
116
 
117
    public void exportTable(Writer w) throws IOException {
118
        w.write(toString());
119
    }
120
 
121
    public void importTable(Reader r) throws IOException {
122
        BufferedReader br = new BufferedReader(r);
123
        int end = rows.size() - 1;
124
        if (end > -1) {
125
            rows.clear();
126
            fireTableRowsDeleted(0, end);
127
        }
128
        String s = "";
129
        while ((s = br.readLine()) != null) {
130
            ICRow row = ICRow.parse(s);
131
            if (row != null)
132
                rows.add(row);
133
        }
134
 
135
        end = rows.size() - 1;
136
        if (end > -1) {
137
            fireTableRowsInserted(0, end);
138
        }
139
    }
140
 
141
    public void exportToFile(File f) throws IOException {
142
        FileWriter fw = new FileWriter(f);
143
        exportTable(fw);
144
        fw.close();
145
    }
146
 
147
    public void importFromFile(File f) throws IOException {
148
        FileReader fr = new FileReader(f);
149
        importTable(fr);
150
        fr.close();
151
    }
152
}