1 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 Object [] DESCRIPTIONS = {"name", "type", "heartbeat", "min value", 37 "max value", "last value", "accum. value", "NaN seconds"}; 38 private static final String [] COLUMN_NAMES = {"description", "value"}; 39 40 private File file; 41 private Object [] 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 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 getColumnName(int column) { 68 return COLUMN_NAMES[column]; 69 } 70 71 void setFile(File 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 []{ 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 e) { 97 e.printStackTrace(); 98 } 99 catch (RrdException e) { 100 e.printStackTrace(); 101 } 102 } 103 fireTableDataChanged(); 104 } 105 } 106 } | Popular Tags |