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  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 }