KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > inspector > MainTreeModel


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  * Arne Vandamme (cobralord@jrobin.org)
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.RrdException;
30
31 import javax.swing.tree.DefaultTreeModel JavaDoc;
32 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.File JavaDoc;
35
36 class MainTreeModel extends DefaultTreeModel JavaDoc {
37     private static final DefaultMutableTreeNode JavaDoc INVALID_NODE =
38         new DefaultMutableTreeNode JavaDoc("No valid RRD file specified");
39
40     private File JavaDoc file;
41
42     MainTreeModel() {
43         super(INVALID_NODE);
44     }
45
46     boolean setFile(File JavaDoc newFile) {
47         try {
48             file = newFile;
49             RrdDb rrd = new RrdDb(file.getAbsolutePath());
50             DefaultMutableTreeNode JavaDoc root = new DefaultMutableTreeNode JavaDoc(new RrdNode(rrd));
51             int dsCount = rrd.getRrdDef().getDsCount();
52             int arcCount = rrd.getRrdDef().getArcCount();
53             for (int dsIndex = 0; dsIndex < dsCount; dsIndex++) {
54                 DefaultMutableTreeNode JavaDoc dsNode =
55                     new DefaultMutableTreeNode JavaDoc(new RrdNode(rrd, dsIndex));
56                 for (int arcIndex = 0; arcIndex < arcCount; arcIndex++) {
57                     DefaultMutableTreeNode JavaDoc arcNode =
58                         new DefaultMutableTreeNode JavaDoc(new RrdNode(rrd, dsIndex, arcIndex));
59                     dsNode.add(arcNode);
60                 }
61                 root.add(dsNode);
62             }
63             rrd.close();
64             setRoot(root);
65             return true;
66         } catch (IOException JavaDoc e) {
67             setRoot(INVALID_NODE);
68         } catch (RrdException e) {
69             setRoot(INVALID_NODE);
70         }
71         return false;
72     }
73 }
74
Popular Tags