View Javadoc

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   *
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;
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 long serialVersionUID = 1L;
37  	private static final String[] COLUMN_NAMES = {"timestamp", "date", "value"};
38  
39  	private File file;
40  	private Object[][] values;
41  	private int dsIndex = -1, arcIndex = -1;
42  
43  	public int getRowCount() {
44  		if (values == null) {
45  			return 0;
46  		}
47  		else {
48  			return values.length;
49  		}
50  	}
51  
52  	public int getColumnCount() {
53  		return COLUMN_NAMES.length;
54  	}
55  
56  	public Object getValueAt(int rowIndex, int columnIndex) {
57  		if (values == null) {
58  			return "--";
59  		}
60  		return values[rowIndex][columnIndex];
61  	}
62  
63  	public String getColumnName(int column) {
64  		return COLUMN_NAMES[column];
65  	}
66  
67  	public boolean isCellEditable(int rowIndex, int columnIndex) {
68  		return columnIndex == 2;
69  	}
70  
71  	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
72  		assert columnIndex == 2: "Column " + columnIndex + " is not editable!";
73  		double value;
74  		try {
75  			value = Double.parseDouble(aValue.toString());
76  		}
77  		catch (NumberFormatException nfe) {
78  			value = Double.NaN;
79  		}
80  		if (dsIndex >= 0 && arcIndex >= 0 && file != null) {
81  			try {
82  				RrdDb rrd = new RrdDb(file.getAbsolutePath());
83  				try {
84  					Robin robin = rrd.getArchive(arcIndex).getRobin(dsIndex);
85  					robin.setValue(rowIndex, value);
86  					values[rowIndex][2] = InspectorModel.formatDouble(robin.getValue(rowIndex));
87  				}
88  				finally {
89  					rrd.close();
90  				}
91  			}
92  			catch (Exception e) {
93  				Util.error(null, e);
94  			}
95  		}
96  	}
97  
98  	void setFile(File newFile) {
99  		file = newFile;
100 		setIndex(-1, -1);
101 	}
102 
103 	void setIndex(int newDsIndex, int newArcIndex) {
104 		if (dsIndex != newDsIndex || arcIndex != newArcIndex) {
105 			dsIndex = newDsIndex;
106 			arcIndex = newArcIndex;
107 			values = null;
108 			if (dsIndex >= 0 && arcIndex >= 0) {
109 				try {
110 					RrdDb rrd = new RrdDb(file.getAbsolutePath(), true);
111 					try {
112 						Archive arc = rrd.getArchive(arcIndex);
113 						Robin robin = arc.getRobin(dsIndex);
114 						long start = arc.getStartTime();
115 						long step = arc.getArcStep();
116 						double robinValues[] = robin.getValues();
117 						values = new Object[robinValues.length][];
118 						for (int i = 0; i < robinValues.length; i++) {
119 							long timestamp = start + i * step;
120 							String date = new Date(timestamp * 1000L).toString();
121 							String value = InspectorModel.formatDouble(robinValues[i]);
122 							values[i] = new Object[] {
123 									"" + timestamp, date, value
124 							};
125 						}
126 					}
127 					finally {
128 						rrd.close();
129 					}
130 				}
131 				catch (IOException e) {
132 					Util.error(null, e);
133 				}
134 				catch (RrdException e) {
135 					Util.error(null, e);
136 				}
137 			}
138 			fireTableDataChanged();
139 		}
140 	}
141 }