KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
29
30 import javax.swing.*;
31 import javax.swing.filechooser.FileFilter JavaDoc;
32 import javax.swing.event.TreeSelectionListener JavaDoc;
33 import javax.swing.event.TreeSelectionEvent JavaDoc;
34 import javax.swing.tree.TreeSelectionModel JavaDoc;
35 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
36 import javax.swing.tree.TreePath JavaDoc;
37 import java.awt.*;
38 import java.awt.event.*;
39 import java.io.File JavaDoc;
40 import java.io.IOException JavaDoc;
41
42 class RrdInspector extends JFrame {
43     static final String JavaDoc TITLE = "RRD File Inspector";
44     static final boolean SHOULD_FIX_ARCHIVED_VALUES = false;
45     static final boolean SHOULD_CREATE_BACKUPS = true;
46
47     static Dimension MAIN_TREE_SIZE = new Dimension(250, 400);
48     static Dimension INFO_PANE_SIZE = new Dimension(450, 400);
49
50     JTabbedPane tabbedPane = new JTabbedPane();
51     private JTree mainTree = new JTree();
52     private JTable generalTable = new JTable();
53     private JTable datasourceTable = new JTable();
54     private JTable archiveTable = new JTable();
55     private JTable dataTable = new JTable();
56
57     private InspectorModel inspectorModel = new InspectorModel();
58
59     private String JavaDoc lastDirectory = null;
60
61     private RrdInspector(String JavaDoc path) {
62         super(TITLE);
63         constructUI();
64         showCentered();
65         if(path == null) {
66             selectFile();
67         }
68         else {
69             loadFile(new File JavaDoc(path));
70         }
71     }
72
73     private void showCentered() {
74         pack();
75         Toolkit t = Toolkit.getDefaultToolkit();
76         Dimension screenSize = t.getScreenSize(), frameSize = getPreferredSize();
77         double x = (screenSize.getWidth() - frameSize.getWidth()) / 2;
78         double y = (screenSize.getHeight() - frameSize.getHeight()) / 2;
79         setLocation((int) x, (int) y);
80         setVisible(true);
81     }
82
83     private void constructUI() {
84         JPanel content = (JPanel) getContentPane();
85         content.setLayout(new BorderLayout());
86
87         // WEST, tree pane
88
JPanel leftPanel = new JPanel();
89         leftPanel.setLayout(new BorderLayout());
90         JScrollPane treePane = new JScrollPane(mainTree);
91         leftPanel.add(treePane);
92         leftPanel.setPreferredSize(MAIN_TREE_SIZE);
93         content.add(leftPanel, BorderLayout.WEST);
94         mainTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
95         mainTree.addTreeSelectionListener(new TreeSelectionListener JavaDoc() {
96             public void valueChanged(TreeSelectionEvent JavaDoc e) {
97                 nodeChangedAction();
98             }
99         });
100         mainTree.setModel(inspectorModel.getMainTreeModel());
101
102         ////////////////////////////////////////////
103
// EAST, tabbed pane
104
////////////////////////////////////////////
105

106         // GENERAL TAB
107
JScrollPane spGeneral = new JScrollPane(generalTable);
108         spGeneral.setPreferredSize(INFO_PANE_SIZE);
109         tabbedPane.add("General info", spGeneral);
110         generalTable.setModel(inspectorModel.getGeneralTableModel());
111         generalTable.getColumnModel().getColumn(0).setPreferredWidth(150);
112         generalTable.getColumnModel().getColumn(0).setMaxWidth(150);
113
114         // DATASOURCE TAB
115
JScrollPane spDatasource = new JScrollPane(datasourceTable);
116         spDatasource.setPreferredSize(INFO_PANE_SIZE);
117         tabbedPane.add("Datasource info", spDatasource);
118         datasourceTable.setModel(inspectorModel.getDatasourceTableModel());
119         datasourceTable.getColumnModel().getColumn(0).setPreferredWidth(150);
120         datasourceTable.getColumnModel().getColumn(0).setMaxWidth(150);
121
122         // ARCHIVE TAB
123
JScrollPane spArchive = new JScrollPane(archiveTable);
124         archiveTable.setModel(inspectorModel.getArchiveTableModel());
125         archiveTable.getColumnModel().getColumn(0).setPreferredWidth(150);
126         archiveTable.getColumnModel().getColumn(0).setMaxWidth(150);
127         spArchive.setPreferredSize(INFO_PANE_SIZE);
128         tabbedPane.add("Archive info", spArchive);
129
130         // DATA TAB
131
JScrollPane spData = new JScrollPane(dataTable);
132         dataTable.setModel(inspectorModel.getDataTableModel());
133         dataTable.getColumnModel().getColumn(0).setPreferredWidth(100);
134         dataTable.getColumnModel().getColumn(0).setMaxWidth(100);
135         dataTable.getColumnModel().getColumn(1).setPreferredWidth(150);
136         spData.setPreferredSize(INFO_PANE_SIZE);
137         tabbedPane.add("Archive data", spData);
138
139         content.add(tabbedPane, BorderLayout.CENTER);
140
141         ////////////////////////////////////////
142
// MENU
143
////////////////////////////////////////
144
JMenuBar menuBar = new JMenuBar();
145         // FILE
146
JMenu fileMenu = new JMenu("File");
147         fileMenu.setMnemonic(KeyEvent.VK_F);
148         JMenuItem fileMenuItem = new JMenuItem("Open RRD file...", KeyEvent.VK_O);
149         fileMenuItem.addActionListener(new ActionListener() {
150             public void actionPerformed(ActionEvent e) {
151                 selectFile();
152             }
153         });
154         fileMenu.add(fileMenuItem);
155         fileMenu.addSeparator();
156         JMenuItem addDatasourceMenuItem = new JMenuItem("Add datasource...");
157         addDatasourceMenuItem.addActionListener(new ActionListener() {
158             public void actionPerformed(ActionEvent e) {
159                 addDatasource();
160             }
161         });
162         fileMenu.add(addDatasourceMenuItem);
163         JMenuItem editDatasourceMenuItem = new JMenuItem("Edit datasource...");
164         editDatasourceMenuItem.addActionListener(new ActionListener() {
165             public void actionPerformed(ActionEvent e) {
166                 editDatasource();
167             }
168         });
169         fileMenu.add(editDatasourceMenuItem);
170         JMenuItem removeDatasourceMenuItem = new JMenuItem("Remove datasource");
171         removeDatasourceMenuItem.addActionListener(new ActionListener() {
172             public void actionPerformed(ActionEvent e) {
173                 removeDatasource();
174             }
175         });
176         fileMenu.add(removeDatasourceMenuItem);
177         fileMenu.addSeparator();
178         JMenuItem addArchiveMenuItem = new JMenuItem("Add archive...");
179         addArchiveMenuItem.addActionListener(new ActionListener() {
180             public void actionPerformed(ActionEvent e) {
181                 addArchive();
182             }
183         });
184         fileMenu.add(addArchiveMenuItem);
185         JMenuItem editArchiveMenuItem = new JMenuItem("Edit archive...");
186         editArchiveMenuItem.addActionListener(new ActionListener() {
187             public void actionPerformed(ActionEvent e) {
188                 editArchive();
189             }
190         });
191         fileMenu.add(editArchiveMenuItem);
192         JMenuItem removeArchiveMenuItem = new JMenuItem("Remove archive...");
193         removeArchiveMenuItem.addActionListener(new ActionListener() {
194             public void actionPerformed(ActionEvent e) {
195                 removeArchive();
196             }
197         });
198         fileMenu.add(removeArchiveMenuItem);
199         fileMenu.addSeparator();
200         JMenuItem exitMenuItem = new JMenuItem("Exit", KeyEvent.VK_X);
201         exitMenuItem.addActionListener(new ActionListener() {
202             public void actionPerformed(ActionEvent e) {
203                 System.exit(0);
204             }
205         });
206         fileMenu.add(exitMenuItem);
207         menuBar.add(fileMenu);
208         setJMenuBar(menuBar);
209
210         // finalize UI
211
addWindowListener(new WindowAdapter() {
212             public void windowClosing(WindowEvent e) {
213                 System.exit(0);
214             }
215         });
216
217     }
218
219     private void nodeChangedAction() {
220         RrdNode rrdNode = getSelectedRrdNode();
221         if (rrdNode != null) {
222             inspectorModel.selectModel(rrdNode.getDsIndex(), rrdNode.getArcIndex());
223             if (rrdNode.getDsIndex() >= 0 && rrdNode.getArcIndex() >= 0) {
224                 // archive node
225
if (tabbedPane.getSelectedIndex() < 2) {
226                     tabbedPane.setSelectedIndex(2);
227                 }
228             } else if (rrdNode.getDsIndex() >= 0) {
229                 tabbedPane.setSelectedIndex(1);
230             } else {
231                 tabbedPane.setSelectedIndex(0);
232             }
233         }
234     }
235
236     private RrdNode getSelectedRrdNode() {
237         TreePath JavaDoc path = mainTree.getSelectionPath();
238         if (path != null) {
239             DefaultMutableTreeNode JavaDoc node = (DefaultMutableTreeNode JavaDoc) path.getLastPathComponent();
240             Object JavaDoc obj = node.getUserObject();
241             if (obj instanceof RrdNode) {
242                 RrdNode rrdNode = (RrdNode) obj;
243                 return rrdNode;
244             }
245         }
246         return null;
247     }
248
249     private void selectFile() {
250         JFileChooser chooser = new JFileChooser(lastDirectory);
251         FileFilter JavaDoc filter = new FileFilter JavaDoc() {
252             public boolean accept(File JavaDoc f) {
253                 return f.isDirectory() ? true :
254                     f.getAbsolutePath().toLowerCase().endsWith(".rrd");
255             }
256
257             public String JavaDoc getDescription() {
258                 return "JRobin RRD files";
259             }
260         };
261         chooser.setFileFilter(filter);
262         int returnVal = chooser.showOpenDialog(this);
263         if (returnVal == JFileChooser.APPROVE_OPTION) {
264             File JavaDoc file = chooser.getSelectedFile();
265             lastDirectory = file.getParent();
266             //inspectorModel.setFile(file);
267
//tabbedPane.setSelectedIndex(0);
268
loadFile(file);
269         }
270     }
271
272     private void loadFile(File JavaDoc file) {
273         inspectorModel.setFile(file);
274         tabbedPane.setSelectedIndex(0);
275     }
276
277
278     private void addDatasource() {
279         if (!inspectorModel.isOk()) {
280             Util.error(this, "Open a valid RRD file first.");
281             return;
282         }
283         DsDef newDsDef = new EditDatasourceDialog(this, null).getDsDef();
284         if (newDsDef != null) {
285             // action
286
RrdToolkit toolkit = RrdToolkit.getInstance();
287             try {
288                 String JavaDoc sourcePath = inspectorModel.getFile().getCanonicalPath();
289                 toolkit.addDatasource(sourcePath, newDsDef, SHOULD_CREATE_BACKUPS);
290                 inspectorModel.refresh();
291                 tabbedPane.setSelectedIndex(0);
292             } catch (IOException JavaDoc e) {
293                 Util.error(this, e.toString());
294             } catch (RrdException e) {
295                 Util.error(this, e.toString());
296             }
297         }
298     }
299
300     private void addArchive() {
301         if (!inspectorModel.isOk()) {
302             Util.error(this, "Open a valid RRD file first.");
303             return;
304         }
305         ArcDef newArcDef = new EditArchiveDialog(this, null).getArcDef();
306         if (newArcDef != null) {
307             // action
308
RrdToolkit toolkit = RrdToolkit.getInstance();
309             try {
310                 String JavaDoc sourcePath = inspectorModel.getFile().getCanonicalPath();
311                 toolkit.addArchive(sourcePath, newArcDef, SHOULD_CREATE_BACKUPS);
312                 inspectorModel.refresh();
313                 tabbedPane.setSelectedIndex(0);
314             } catch (IOException JavaDoc e) {
315                 Util.error(this, e.toString());
316             } catch (RrdException e) {
317                 Util.error(this, e.toString());
318             }
319         }
320     }
321
322     private void editDatasource() {
323         if (!inspectorModel.isOk()) {
324             Util.error(this, "Open a valid RRD file first.");
325             return;
326         }
327         RrdNode rrdNode = getSelectedRrdNode();
328         int dsIndex = -1;
329         if(rrdNode == null || (dsIndex = rrdNode.getDsIndex()) < 0) {
330             Util.error(this, "Select datasource first");
331             return;
332         }
333         try {
334             String JavaDoc sourcePath = inspectorModel.getFile().getCanonicalPath();
335             RrdDb rrd = new RrdDb(sourcePath);
336             DsDef dsDef = rrd.getRrdDef().getDsDefs()[dsIndex];
337             rrd.close();
338             DsDef newDsDef = new EditDatasourceDialog(this, dsDef).getDsDef();
339             if(newDsDef != null) {
340                 // action!
341
RrdToolkit toolkit = RrdToolkit.getInstance();
342                 toolkit.setDsHeartbeat(sourcePath, newDsDef.getDsName(),
343                     newDsDef.getHeartbeat());
344                 toolkit.setDsMinMaxValue(sourcePath, newDsDef.getDsName(),
345                     newDsDef.getMinValue(), newDsDef.getMaxValue(), SHOULD_FIX_ARCHIVED_VALUES);
346                 inspectorModel.refresh();
347                 tabbedPane.setSelectedIndex(0);
348             }
349             rrd.close();
350         } catch (IOException JavaDoc e) {
351             Util.error(this, e.toString());
352         } catch (RrdException e) {
353             Util.error(this, e.toString());
354         }
355     }
356
357     private void editArchive() {
358         if (!inspectorModel.isOk()) {
359             Util.error(this, "Open a valid RRD file first.");
360             return;
361         }
362         RrdNode rrdNode = getSelectedRrdNode();
363         int arcIndex = -1;
364         if(rrdNode == null || (arcIndex = rrdNode.getArcIndex()) < 0) {
365             Util.error(this, "Select archive first");
366             return;
367         }
368         try {
369             String JavaDoc sourcePath = inspectorModel.getFile().getCanonicalPath();
370             RrdDb rrd = new RrdDb(sourcePath);
371             ArcDef arcDef = rrd.getRrdDef().getArcDefs()[arcIndex];
372             rrd.close();
373             ArcDef newArcDef = new EditArchiveDialog(this, arcDef).getArcDef();
374             if(newArcDef != null) {
375                 // action!
376
RrdToolkit toolkit = RrdToolkit.getInstance();
377                 // fix X-files factor
378
toolkit.setArcXff(sourcePath, newArcDef.getConsolFun(),
379                     newArcDef.getSteps(), newArcDef.getXff());
380                 // fix archive size
381
toolkit.resizeArchive(sourcePath, newArcDef.getConsolFun(),
382                     newArcDef.getSteps(), newArcDef.getRows(), SHOULD_CREATE_BACKUPS);
383                 inspectorModel.refresh();
384                 tabbedPane.setSelectedIndex(0);
385             }
386             rrd.close();
387         } catch (IOException JavaDoc e) {
388             Util.error(this, e.toString());
389         } catch (RrdException e) {
390             Util.error(this, e.toString());
391         }
392     }
393
394     private void removeDatasource() {
395         if (!inspectorModel.isOk()) {
396             Util.error(this, "Open a valid RRD file first.");
397             return;
398         }
399         RrdNode rrdNode = getSelectedRrdNode();
400         int dsIndex = -1;
401         if(rrdNode == null || (dsIndex = rrdNode.getDsIndex()) < 0) {
402             Util.error(this, "Select datasource first");
403             return;
404         }
405         try {
406             String JavaDoc sourcePath = inspectorModel.getFile().getCanonicalPath();
407             RrdDb rrd = new RrdDb(sourcePath);
408             String JavaDoc dsName = rrd.getRrdDef().getDsDefs()[dsIndex].getDsName();
409             rrd.close();
410             RrdToolkit toolkit = RrdToolkit.getInstance();
411             toolkit.removeDatasource(sourcePath, dsName, SHOULD_CREATE_BACKUPS);
412             inspectorModel.refresh();
413             tabbedPane.setSelectedIndex(0);
414         } catch (IOException JavaDoc e) {
415             Util.error(this, e.toString());
416         } catch (RrdException e) {
417             Util.error(this, e.toString());
418         }
419     }
420
421     private void removeArchive() {
422         if (!inspectorModel.isOk()) {
423             Util.error(this, "Open a valid RRD file first.");
424             return;
425         }
426         RrdNode rrdNode = getSelectedRrdNode();
427         int arcIndex = -1;
428         if(rrdNode == null || (arcIndex = rrdNode.getArcIndex()) < 0) {
429             Util.error(this, "Select archive first");
430             return;
431         }
432         try {
433             String JavaDoc sourcePath = inspectorModel.getFile().getCanonicalPath();
434             RrdDb rrd = new RrdDb(sourcePath);
435             ArcDef arcDef = rrd.getRrdDef().getArcDefs()[arcIndex];
436             String JavaDoc consolFun = arcDef.getConsolFun();
437             int steps = arcDef.getSteps();
438             rrd.close();
439             RrdToolkit toolkit = RrdToolkit.getInstance();
440             toolkit.removeArchive(sourcePath, consolFun, steps, SHOULD_CREATE_BACKUPS);
441             inspectorModel.refresh();
442             tabbedPane.setSelectedIndex(0);
443         } catch (IOException JavaDoc e) {
444             Util.error(this, e.toString());
445         } catch (RrdException e) {
446             Util.error(this, e.toString());
447         }
448     }
449
450     private static void printUsageAndExit() {
451         System.err.println("usage: " + RrdInspector.class.getName() + " [<filename>]");
452         System.exit(1);
453     }
454
455     public static void main(String JavaDoc[] args) {
456         if (args.length > 1) {
457             printUsageAndExit();
458         }
459         String JavaDoc path = (args.length == 1)? args[0]: null;
460         new RrdInspector(path);
461     }
462 }
463
Popular Tags