1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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;
32 import java.io.IOException;
33 import java.io.File;
34
35 class DatasourceTableModel extends AbstractTableModel {
36 private static final long serialVersionUID = 1L;
37 private static final Object[] DESCRIPTIONS = {
38 "name", "type", "heartbeat", "min value",
39 "max value", "last value", "accum. value", "NaN seconds"
40 };
41 private static final String[] COLUMN_NAMES = {
42 "description", "value"
43 };
44
45 private File file;
46 private Object[] values;
47 private int dsIndex = -1;
48
49 public int getRowCount() {
50 return DESCRIPTIONS.length;
51 }
52
53 public int getColumnCount() {
54 return COLUMN_NAMES.length;
55 }
56
57 public Object getValueAt(int rowIndex, int columnIndex) {
58 if (columnIndex == 0) {
59 return DESCRIPTIONS[rowIndex];
60 }
61 else if (columnIndex == 1) {
62 if (values != null) {
63 return values[rowIndex];
64 }
65 else {
66 return "--";
67 }
68 }
69 return null;
70 }
71
72 public String getColumnName(int column) {
73 return COLUMN_NAMES[column];
74 }
75
76 public boolean isCellEditable(int rowIndex, int columnIndex) {
77 return false;
78 }
79
80 void setFile(File newFile) {
81 file = newFile;
82 setIndex(-1);
83 }
84
85 void setIndex(int newDsIndex) {
86 if (dsIndex != newDsIndex) {
87 dsIndex = newDsIndex;
88 values = null;
89 if (dsIndex >= 0) {
90 try {
91 RrdDb rrd = new RrdDb(file.getAbsolutePath(), true);
92 try {
93 Datasource ds = rrd.getDatasource(dsIndex);
94 values = new Object[] {
95 ds.getDsName(),
96 ds.getDsType(),
97 "" + ds.getHeartbeat(),
98 InspectorModel.formatDouble(ds.getMinValue()),
99 InspectorModel.formatDouble(ds.getMaxValue()),
100 InspectorModel.formatDouble(ds.getLastValue()),
101 InspectorModel.formatDouble(ds.getAccumValue()),
102 "" + ds.getNanSeconds()
103 };
104 }
105 finally {
106 rrd.close();
107 }
108 }
109 catch (IOException e) {
110 Util.error(null, e);
111 }
112 catch (RrdException e) {
113 Util.error(null, e);
114 }
115 }
116 fireTableDataChanged();
117 }
118 }
119 }