KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.jrobin.inspector;
26
27 import org.jrobin.core.RrdDb;
28 import org.jrobin.core.Datasource;
29 import org.jrobin.core.RrdException;
30
31 import javax.swing.table.AbstractTableModel JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.File JavaDoc;
34
35 class DatasourceTableModel extends AbstractTableModel JavaDoc {
36     private static final Object JavaDoc[] DESCRIPTIONS = {"name", "type", "heartbeat", "min value",
37                                            "max value", "last value", "accum. value", "NaN seconds"};
38     private static final String JavaDoc[] COLUMN_NAMES = {"description", "value"};
39
40     private File JavaDoc file;
41     private Object JavaDoc[] values;
42     private int dsIndex = -1;
43
44     public int getRowCount() {
45         return DESCRIPTIONS.length;
46     }
47
48     public int getColumnCount() {
49         return COLUMN_NAMES.length;
50     }
51
52     public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
53         if (columnIndex == 0) {
54             return DESCRIPTIONS[rowIndex];
55         }
56         else if (columnIndex == 1) {
57             if (values != null) {
58                 return values[rowIndex];
59             }
60             else {
61                 return "--";
62             }
63         }
64         return null;
65     }
66
67     public String JavaDoc getColumnName(int column) {
68         return COLUMN_NAMES[column];
69     }
70
71     void setFile(File JavaDoc newFile) {
72         file = newFile;
73         setIndex(-1);
74     }
75
76     void setIndex(int newDsIndex) {
77         if (dsIndex != newDsIndex) {
78             dsIndex = newDsIndex;
79             values = null;
80             if(dsIndex >= 0) {
81                 try {
82                     RrdDb rrd = new RrdDb(file.getAbsolutePath());
83                     Datasource ds = rrd.getDatasource(dsIndex);
84                     values = new Object JavaDoc[]{
85                         ds.getDsName(),
86                         ds.getDsType(),
87                         "" + ds.getHeartbeat(),
88                         InspectorModel.formatDouble(ds.getMinValue()),
89                         InspectorModel.formatDouble(ds.getMaxValue()),
90                         InspectorModel.formatDouble(ds.getLastValue()),
91                         InspectorModel.formatDouble(ds.getAccumValue()),
92                         "" + ds.getNanSeconds()
93                     };
94                     rrd.close();
95                 }
96                 catch (IOException JavaDoc e) {
97                     e.printStackTrace();
98                 }
99                 catch (RrdException e) {
100                     e.printStackTrace();
101                 }
102             }
103             fireTableDataChanged();
104         }
105     }
106 }
Popular Tags