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.RrdDb;
29  import org.jrobin.core.Header;
30  import org.jrobin.core.RrdException;
31  
32  import javax.swing.table.AbstractTableModel;
33  import java.util.Date;
34  import java.io.IOException;
35  import java.io.File;
36  
37  class HeaderTableModel extends AbstractTableModel {
38  	private static final long serialVersionUID = 1L;
39  	private static final Object[] DESCRIPTIONS = {
40  			"path", "signature", "step", "last timestamp",
41  			"datasources", "archives", "size"
42  	};
43  	private static final String[] COLUMN_NAMES = {"description", "value"};
44  
45  	private Object[] values;
46  
47  	public int getRowCount() {
48  		return DESCRIPTIONS.length;
49  	}
50  
51  	public int getColumnCount() {
52  		return COLUMN_NAMES.length;
53  	}
54  
55  	public Object getValueAt(int rowIndex, int columnIndex) {
56  		if (columnIndex == 0) {
57  			return DESCRIPTIONS[rowIndex];
58  		}
59  		else if (columnIndex == 1) {
60  			if (values != null) {
61  				return values[rowIndex];
62  			}
63  			else {
64  				return "--";
65  			}
66  		}
67  		return null;
68  	}
69  
70  	public String getColumnName(int column) {
71  		return COLUMN_NAMES[column];
72  	}
73  
74  
75  	void setFile(File file) {
76  		try {
77  			values = null;
78  			String path = file.getAbsolutePath();
79  			RrdDb rrd = new RrdDb(path, true);
80  			try {
81  				Header header = rrd.getHeader();
82  				String signature = header.getSignature();
83  				String step = "" + header.getStep();
84  				String lastTimestamp = header.getLastUpdateTime() + " [" +
85  						new Date(header.getLastUpdateTime() * 1000L) + "]";
86  				String datasources = "" + header.getDsCount();
87  				String archives = "" + header.getArcCount();
88  				String size = rrd.getRrdBackend().getLength() + " bytes";
89  				values = new Object[] {
90  						path, signature, step, lastTimestamp, datasources, archives, size
91  				};
92  			}
93  			finally {
94  				rrd.close();
95  			}
96  			fireTableDataChanged();
97  		}
98  		catch (IOException e) {
99  			Util.error(null, e);
100 		}
101 		catch (RrdException e) {
102 			Util.error(null, e);
103 		}
104 	}
105 }