KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > inspector > DataTableModel


1 /* ============================================================
2  * JRobin : Pure java implementation of RRDTool's functionality
3  * ============================================================
4  *
5  * Project Info: http://www.jrobin.org
6  * Project Lead: Sasa Markovic (saxon@jrobin.org);
7  *
8  * (C) Copyright 2003, by Sasa Markovic.
9  *
10  * Developers: Sasa Markovic (saxon@jrobin.org)
11  * Arne Vandamme (cobralord@jrobin.org)
12  *
13  * This library is free software; you can redistribute it and/or modify it under the terms
14  * of the GNU Lesser General Public License as published by the Free Software Foundation;
15  * either version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License along with this
22  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */

25
26 package org.jrobin.inspector;
27
28 import org.jrobin.core.*;
29
30 import javax.swing.table.AbstractTableModel JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.util.Date JavaDoc;
34
35 class DataTableModel extends AbstractTableModel JavaDoc {
36     private static final String JavaDoc[] COLUMN_NAMES = {"timestamp", "date", "value"};
37
38     private File JavaDoc file;
39     private Object JavaDoc[][] values;
40     private int dsIndex = -1, arcIndex = -1;
41
42     public int getRowCount() {
43         if(values == null) {
44             return 0;
45         }
46         else {
47             return values.length;
48         }
49     }
50
51     public int getColumnCount() {
52         return COLUMN_NAMES.length;
53     }
54
55     public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
56         if(values == null) {
57             return "--";
58         }
59         return values[rowIndex][columnIndex];
60     }
61
62     public String JavaDoc getColumnName(int column) {
63         return COLUMN_NAMES[column];
64     }
65
66     void setFile(File JavaDoc newFile) {
67         file = newFile;
68         setIndex(-1, -1);
69     }
70
71     void setIndex(int newDsIndex, int newArcIndex) {
72         if (dsIndex != newDsIndex || arcIndex != newArcIndex) {
73             dsIndex = newDsIndex;
74             arcIndex = newArcIndex;
75             values = null;
76             if(dsIndex >= 0 && arcIndex >= 0) {
77                 try {
78                     RrdDb rrd = new RrdDb(file.getAbsolutePath());
79                     Archive arc = rrd.getArchive(arcIndex);
80                     Robin robin = arc.getRobin(dsIndex);
81                     long start = arc.getStartTime();
82                     long step = arc.getArcStep();
83                     double robinValues[] = robin.getValues();
84                     values = new Object JavaDoc[robinValues.length][];
85                     for(int i = 0; i < robinValues.length; i++) {
86                         long timestamp = start + i * step;
87                         String JavaDoc date = new Date JavaDoc(timestamp * 1000L).toString();
88                         String JavaDoc value = InspectorModel.formatDouble(robinValues[i]);
89                         values[i] = new Object JavaDoc[] {
90                             "" + timestamp, date, value
91                         };
92                     }
93                     rrd.close();
94                 }
95                 catch (IOException JavaDoc e) {
96                     e.printStackTrace();
97                 }
98                 catch (RrdException e) {
99                     e.printStackTrace();
100                 }
101             }
102             fireTableDataChanged();
103         }
104     }
105 }
106
Popular Tags