1 25 26 package org.jrobin.inspector; 27 28 import org.jrobin.core.*; 29 30 import javax.swing.table.AbstractTableModel ; 31 import java.io.File ; 32 import java.io.IOException ; 33 import java.util.Date ; 34 35 class DataTableModel extends AbstractTableModel { 36 private static final String [] COLUMN_NAMES = {"timestamp", "date", "value"}; 37 38 private File file; 39 private Object [][] 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 getValueAt(int rowIndex, int columnIndex) { 56 if(values == null) { 57 return "--"; 58 } 59 return values[rowIndex][columnIndex]; 60 } 61 62 public String getColumnName(int column) { 63 return COLUMN_NAMES[column]; 64 } 65 66 void setFile(File 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 [robinValues.length][]; 85 for(int i = 0; i < robinValues.length; i++) { 86 long timestamp = start + i * step; 87 String date = new Date (timestamp * 1000L).toString(); 88 String value = InspectorModel.formatDouble(robinValues[i]); 89 values[i] = new Object [] { 90 "" + timestamp, date, value 91 }; 92 } 93 rrd.close(); 94 } 95 catch (IOException e) { 96 e.printStackTrace(); 97 } 98 catch (RrdException e) { 99 e.printStackTrace(); 100 } 101 } 102 fireTableDataChanged(); 103 } 104 } 105 } 106 | Popular Tags |