KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > mrtg > client > Client


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.mrtg.client;
27
28 import org.apache.xmlrpc.XmlRpcException;
29 import org.jrobin.mrtg.MrtgException;
30
31 import javax.swing.*;
32 import javax.swing.event.TreeSelectionEvent JavaDoc;
33 import javax.swing.event.TreeSelectionListener JavaDoc;
34 import javax.swing.tree.*;
35 import java.awt.*;
36 import java.awt.event.*;
37 import java.io.IOException JavaDoc;
38
39 class Client extends JFrame {
40     static final String JavaDoc RESOURCE_PATH = "res/mrtg/";
41     static final String JavaDoc TITLE = "JRobin-MRTG client";
42     static final String JavaDoc SUBTITLE = "http://www.jrobin.org";
43     static final String JavaDoc COPYRIGHT = "Copyright \u00A9 2004 Sasa Markovic";
44     static final String JavaDoc ICON = RESOURCE_PATH + "icon.png";
45
46     static Dimension MAIN_TREE_SIZE = new Dimension(320, 400);
47     static Dimension INFO_PANE_SIZE = new Dimension(320, 400);
48     static String JavaDoc DEFAULT_HOST = "localhost";
49     static final int HGAP = 1, VGAP = 1, DIVIDER_SIZE = 3;
50
51     // controls
52
JTree mainTree = new JTree();
53     JTextPane infoTextPane = new JTextPane();
54     // menubar
55
JMenuBar menuBar = new JMenuBar();
56     // MRTG menu
57
JMenu mrtgMenu = new JMenu("MRTG");
58     JMenuItem newHostMenuItem = new JMenuItem("Connect to JRobin-MRTG server...", KeyEvent.VK_C);
59     JMenuItem reloadMenuItem = new JMenuItem("Reload data from JRobin-MRTG server", KeyEvent.VK_L);
60     JMenuItem exitMenuItem = new JMenuItem("Exit", KeyEvent.VK_X);
61     // Routers menu
62
JMenu routersMenu = new JMenu("Router actions");
63     JMenuItem newRouterItem = new JMenuItem("Add router...", KeyEvent.VK_A);
64     JMenuItem editRouterItem = new JMenuItem("Edit router data...", KeyEvent.VK_E);
65     JMenuItem deleteRouterItem = new JMenuItem("Remove router", KeyEvent.VK_V);
66     // Interfaces menu
67
JMenu linksMenu = new JMenu("Interface actions");
68     JMenuItem newLinkItem = new JMenuItem("Add interface(s)...", KeyEvent.VK_N);
69     JMenuItem editLinkItem = new JMenuItem("Edit interface data...", KeyEvent.VK_E);
70     JMenuItem deleteLinkItem = new JMenuItem("Remove interface", KeyEvent.VK_V);
71     JMenuItem quickGraphItem = new JMenuItem("Quick graph (last 24hr)...", KeyEvent.VK_Q);
72     JMenuItem dailyGraphItem = new JMenuItem("Daily graph...", KeyEvent.VK_D);
73     JMenuItem weeklyGraphItem = new JMenuItem("Weekly graph...", KeyEvent.VK_W);
74     JMenuItem monthlyGraphItem = new JMenuItem("Monthly graph...", KeyEvent.VK_O);
75     JMenuItem yearlyGraphItem = new JMenuItem("Yearly graph...", KeyEvent.VK_Y);
76     JMenuItem customGraphItem = new JMenuItem("Custom graph...", KeyEvent.VK_C);
77     // Help menu
78
JMenu helpMenu = new JMenu("Help");
79     JMenuItem helpItem = new JMenuItem("Help...", KeyEvent.VK_H);
80     JMenuItem aboutItem = new JMenuItem("About...", KeyEvent.VK_A);
81     // MRTG popup menu
82
JPopupMenu mrtgPopupMenu = new JPopupMenu();
83     JMenuItem mrtgPopupReloadMenuItem = new JMenuItem("Reload data from JRobin-MRTG server");
84     JMenuItem mrtgPopupAddRouterMenuItem = new JMenuItem("Add router...");
85     JMenuItem mrtgPopupExitMenuItem = new JMenuItem("Exit");
86     // ROUTER popup menu
87
JPopupMenu routerPopupMenu = new JPopupMenu();
88     JMenuItem routerPopupEditRouterMenuItem = new JMenuItem("Edit router data...");
89     JMenuItem routerPopupAddLinksMenuItem = new JMenuItem("Add interface(s)...");
90     JMenuItem routerPopupRemoveRouterMenuItem = new JMenuItem("Remove router");
91     // INTERFACES popup menu
92
JPopupMenu linksPopupMenu = new JPopupMenu();
93     JMenuItem linksPopupEditLinkMenuItem = new JMenuItem("Edit interface data...");
94     JMenuItem linksPopupRemoveLinkMenuItem = new JMenuItem("Remove interface");
95     JMenuItem linksPopupQuickGraphMenuItem = new JMenuItem("Quick graph (last 24hr)...");
96     JMenuItem linksPopupDailyGraphMenuItem = new JMenuItem("Daily graph...");
97     JMenuItem linksPopupWeeklyGraphMenuItem = new JMenuItem("Weekly graph...");
98     JMenuItem linksPopupMonthlyGraphMenuItem = new JMenuItem("Monthly graph...");
99     JMenuItem linksPopupYearlyGraphMenuItem = new JMenuItem("Yearly graph...");
100     JMenuItem linksPopupCustomGraphMenuItem = new JMenuItem("Custom graph...");
101
102     private MrtgData mrtgData = MrtgData.getInstance();
103
104     Client() throws IOException JavaDoc, XmlRpcException {
105         super(TITLE);
106         constructUI();
107         pack();
108     }
109
110     private void constructUI() {
111         JPanel content = (JPanel) getContentPane();
112         content.setLayout(new BorderLayout(HGAP, VGAP));
113
114         // tree pane
115
JPanel leftPanel = new JPanel();
116         leftPanel.setLayout(new BorderLayout());
117         JScrollPane treePane = new JScrollPane(mainTree);
118         leftPanel.add(treePane);
119         leftPanel.setPreferredSize(MAIN_TREE_SIZE);
120         mainTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
121         mainTree.addTreeSelectionListener(new TreeSelectionListener JavaDoc() {
122             public void valueChanged(TreeSelectionEvent JavaDoc e) { nodeChangedAction(); }
123         });
124         DefaultTreeCellRenderer renderer = new TreeRenderer();
125         mainTree.setCellRenderer(renderer);
126
127         // info pane
128
JPanel rightPanel = new JPanel();
129         rightPanel.setLayout(new BorderLayout());
130         JScrollPane infoPane = new JScrollPane(infoTextPane);
131         rightPanel.add(infoPane);
132         infoPane.setPreferredSize(INFO_PANE_SIZE);
133         infoTextPane.setEditable(false);
134         JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true,
135             leftPanel, rightPanel);
136         splitPane.setDividerSize(DIVIDER_SIZE);
137
138         // add split pane to content
139
content.add(splitPane, BorderLayout.CENTER);
140
141         // mrtg menu
142
newHostMenuItem.addActionListener(new ActionListener() {
143             public void actionPerformed(ActionEvent e) { selectNewHost(); }
144         });
145         mrtgMenu.add(newHostMenuItem);
146         reloadMenuItem.addActionListener(new ActionListener() {
147             public void actionPerformed(ActionEvent e) { reloadDataFromHost(); }
148         });
149         mrtgMenu.add(reloadMenuItem);
150         mrtgMenu.addSeparator();
151         exitMenuItem.addActionListener(new ActionListener() {
152             public void actionPerformed(ActionEvent e) { System.exit(0); }
153         });
154         mrtgMenu.add(exitMenuItem);
155         mrtgMenu.setMnemonic(KeyEvent.VK_M);
156         menuBar.add(mrtgMenu);
157
158         // routers menu
159
newRouterItem.addActionListener(new ActionListener() {
160             public void actionPerformed(ActionEvent e) { addRouter(); }
161         });
162         routersMenu.add(newRouterItem);
163         editRouterItem.addActionListener(new ActionListener() {
164             public void actionPerformed(ActionEvent e) { editRouter(); }
165         });
166         routersMenu.add(editRouterItem);
167         routersMenu.addSeparator();
168         deleteRouterItem.addActionListener(new ActionListener() {
169             public void actionPerformed(ActionEvent e) { removeRouter(); }
170         });
171         routersMenu.add(deleteRouterItem);
172         routersMenu.setMnemonic(KeyEvent.VK_R);
173         menuBar.add(routersMenu);
174
175         // interfaces menu
176
newLinkItem.addActionListener(new ActionListener() {
177             public void actionPerformed(ActionEvent e) { addLink(); }
178         });
179         linksMenu.add(newLinkItem);
180         editLinkItem.addActionListener(new ActionListener() {
181             public void actionPerformed(ActionEvent e) { editLink(); }
182         });
183         linksMenu.add(editLinkItem);
184         linksMenu.addSeparator();
185         deleteLinkItem.addActionListener(new ActionListener() {
186             public void actionPerformed(ActionEvent e) { removeLink(); }
187         });
188         linksMenu.add(deleteLinkItem);
189         linksMenu.addSeparator();
190         quickGraphItem.addActionListener(new ActionListener() {
191             public void actionPerformed(ActionEvent e) { graph(GraphFrame.TYPE_QUICK); }
192         });
193         linksMenu.add(quickGraphItem);
194         dailyGraphItem.addActionListener(new ActionListener() {
195             public void actionPerformed(ActionEvent e) { graph(GraphFrame.TYPE_DAILY); }
196         });
197         linksMenu.add(dailyGraphItem);
198         weeklyGraphItem.addActionListener(new ActionListener() {
199             public void actionPerformed(ActionEvent e) { graph(GraphFrame.TYPE_WEEKLY); }
200         });
201         linksMenu.add(weeklyGraphItem);
202         monthlyGraphItem.addActionListener(new ActionListener() {
203             public void actionPerformed(ActionEvent e) { graph(GraphFrame.TYPE_MONTHLY); }
204         });
205         linksMenu.add(monthlyGraphItem);
206         yearlyGraphItem.addActionListener(new ActionListener() {
207             public void actionPerformed(ActionEvent e) { graph(GraphFrame.TYPE_YEARLY); }
208         });
209         linksMenu.add(yearlyGraphItem);
210         linksMenu.addSeparator();
211         customGraphItem.addActionListener(new ActionListener() {
212             public void actionPerformed(ActionEvent e) { graph(GraphFrame.TYPE_CUSTOM); }
213         });
214         linksMenu.add(customGraphItem);
215         linksMenu.setMnemonic(KeyEvent.VK_I);
216         menuBar.add(linksMenu);
217
218         // help menu
219
helpMenu.setMnemonic(KeyEvent.VK_H);
220         helpItem.addActionListener(new ActionListener() {
221             public void actionPerformed(ActionEvent e) { new HelpDialog(Client.this); }
222         });
223         helpMenu.add(helpItem);
224         helpMenu.addSeparator();
225         aboutItem.addActionListener(new ActionListener() {
226             public void actionPerformed(ActionEvent e) { new AboutDialog(Client.this); }
227         });
228         helpMenu.add(aboutItem);
229         menuBar.add(helpMenu);
230
231         // MRTG popup menu
232
mrtgPopupReloadMenuItem.addActionListener(new ActionListener() {
233             public void actionPerformed(ActionEvent e) { reloadDataFromHost(); }
234         });
235         mrtgPopupMenu.add(mrtgPopupReloadMenuItem);
236         mrtgPopupAddRouterMenuItem.addActionListener(new ActionListener() {
237             public void actionPerformed(ActionEvent e) { addRouter(); }
238         });
239         mrtgPopupMenu.add(mrtgPopupAddRouterMenuItem);
240         mrtgPopupMenu.addSeparator();
241         mrtgPopupExitMenuItem.addActionListener(new ActionListener() {
242             public void actionPerformed(ActionEvent e) { System.exit(0); }
243         });
244         mrtgPopupMenu.add(mrtgPopupExitMenuItem);
245
246         // ROUTER popup menu
247
routerPopupEditRouterMenuItem.addActionListener(new ActionListener() {
248             public void actionPerformed(ActionEvent e) { editRouter(); }
249         });
250         routerPopupMenu.add(routerPopupEditRouterMenuItem);
251         routerPopupAddLinksMenuItem.addActionListener(new ActionListener() {
252             public void actionPerformed(ActionEvent e) { addLink(); }
253         });
254         routerPopupMenu.add(routerPopupAddLinksMenuItem);
255         routerPopupMenu.addSeparator();
256         routerPopupRemoveRouterMenuItem.addActionListener(new ActionListener() {
257             public void actionPerformed(ActionEvent e) { removeRouter(); }
258         });
259         routerPopupMenu.add(routerPopupRemoveRouterMenuItem);
260
261         // LINKS popup menu
262
linksPopupEditLinkMenuItem.addActionListener(new ActionListener() {
263             public void actionPerformed(ActionEvent e) { editLink(); }
264         });
265         linksPopupMenu.add(linksPopupEditLinkMenuItem);
266         linksPopupMenu.addSeparator();
267         linksPopupQuickGraphMenuItem.addActionListener(new ActionListener() {
268             public void actionPerformed(ActionEvent e) { graph(GraphFrame.TYPE_QUICK); }
269         });
270         linksPopupMenu.add(linksPopupQuickGraphMenuItem);
271         linksPopupDailyGraphMenuItem.addActionListener(new ActionListener() {
272             public void actionPerformed(ActionEvent e) { graph(GraphFrame.TYPE_DAILY); }
273         });
274         linksPopupMenu.add(linksPopupDailyGraphMenuItem);
275         linksPopupWeeklyGraphMenuItem.addActionListener(new ActionListener() {
276             public void actionPerformed(ActionEvent e) { graph(GraphFrame.TYPE_WEEKLY); }
277         });
278         linksPopupMenu.add(linksPopupWeeklyGraphMenuItem);
279         linksPopupMonthlyGraphMenuItem.addActionListener(new ActionListener() {
280             public void actionPerformed(ActionEvent e) { graph(GraphFrame.TYPE_MONTHLY); }
281         });
282         linksPopupMenu.add(linksPopupMonthlyGraphMenuItem);
283         linksPopupYearlyGraphMenuItem.addActionListener(new ActionListener() {
284             public void actionPerformed(ActionEvent e) { graph(GraphFrame.TYPE_YEARLY); }
285         });
286         linksPopupMenu.add(linksPopupYearlyGraphMenuItem);
287         linksPopupMenu.addSeparator();
288         linksPopupCustomGraphMenuItem.addActionListener(new ActionListener() {
289             public void actionPerformed(ActionEvent e) { graph(GraphFrame.TYPE_CUSTOM); }
290         });
291         linksPopupMenu.add(linksPopupCustomGraphMenuItem);
292         linksPopupMenu.addSeparator();
293         linksPopupRemoveLinkMenuItem.addActionListener(new ActionListener() {
294             public void actionPerformed(ActionEvent e) { removeLink(); }
295         });
296         linksPopupMenu.add(linksPopupRemoveLinkMenuItem);
297
298         MouseAdapter adapter = new MouseAdapter() {
299             public void mousePressed(MouseEvent e) { showPopup(e); }
300             public void mouseReleased(MouseEvent e) { showPopup(e); }
301             private void showPopup(MouseEvent e) {
302                 if (e.isPopupTrigger()) {
303                     int selRow = mainTree.getRowForLocation(e.getX(), e.getY());
304                     TreePath treePath = mainTree.getPathForLocation(e.getX(), e.getY());
305                     if(selRow != -1) {
306                         mainTree.setSelectionPath(treePath);
307                         DefaultMutableTreeNode inf =
308                             (DefaultMutableTreeNode) treePath.getLastPathComponent();
309                         Object JavaDoc obj = inf.getUserObject();
310                         if(obj instanceof ServerInfo) {
311                             mrtgPopupMenu.show(e.getComponent(), e.getX(), e.getY());
312                         }
313                         else if(obj instanceof RouterInfo) {
314                             routerPopupMenu.show(e.getComponent(), e.getX(), e.getY());
315                         }
316                         else if(obj instanceof LinkInfo) {
317                             linksPopupMenu.show(e.getComponent(), e.getX(), e.getY());
318                         }
319                     }
320                 }
321             }
322         };
323         mainTree.addMouseListener(adapter);
324
325         // finalize UI
326
try {
327             setIconImage(Resources.getImage(ICON));
328         } catch (MrtgException e) {
329             e.printStackTrace();
330         }
331         setJMenuBar(menuBar);
332         clearUI();
333         addWindowListener(new WindowAdapter() {
334             public void windowClosing(WindowEvent e) { System.exit(0); }
335             public void windowOpened(WindowEvent e) { selectNewHost(); }
336         });
337         Util.centerOnScreen(this);
338         mainTree.requestFocus();
339     }
340
341     private void clearUI() {
342         mainTree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode("Not connected")));
343         infoTextPane.setText("No info available");
344     }
345
346     private void selectNewHost() {
347         try {
348             HostDialog hostDialog = new HostDialog(this);
349             String JavaDoc newHost = hostDialog.getHost();
350             if(newHost != null) {
351                 mrtgData.setHost(newHost);
352                 reloadData();
353             }
354         } catch (Exception JavaDoc e) {
355             clearUI();
356             handleException("Host is not responding", e);
357         }
358     }
359
360     private void nodeChangedAction() {
361         DefaultMutableTreeNode node = (DefaultMutableTreeNode)
362             mainTree.getLastSelectedPathComponent();
363         if (node != null) {
364             Object JavaDoc nodeObj = node.getUserObject();
365             if(nodeObj instanceof TreeElementInfo) {
366                 TreeElementInfo treeElement = (TreeElementInfo) nodeObj;
367                 infoTextPane.setText(treeElement.getInfo());
368             }
369         }
370     }
371
372     private void reloadDataFromHost() {
373         try {
374             mrtgData.reload();
375             reloadData();
376         } catch (Exception JavaDoc e) {
377             clearUI();
378             handleException("Could not reload data from host", e);
379         }
380     }
381
382     private void reloadData() {
383         try {
384             TreeModel treeModel = mrtgData.getTreeModel();
385             mainTree.setModel(treeModel);
386             mainTree.setSelectionRow(0);
387             for(int i = mainTree.getRowCount() - 1; i > 0; i--) {
388                 mainTree.expandRow(i);
389             }
390         } catch (Exception JavaDoc e) {
391             clearUI();
392             handleException("Could not reload data from host", e);
393         }
394     }
395
396     private boolean isConnected() {
397         DefaultMutableTreeNode node = (DefaultMutableTreeNode) mainTree.getModel().getRoot();
398         return node.getUserObject() instanceof ServerInfo;
399     }
400
401     private void addRouter() {
402         if(!isConnected()) {
403             Util.error(this, "Connect to MRTG server first");
404             return;
405         }
406         EditRouterDialog newRouterDialog = new EditRouterDialog(this);
407         RouterInfo routerInfo = newRouterDialog.getRouterInfo();
408         if(routerInfo != null) {
409             try {
410                 if(mrtgData.addRouter(routerInfo) == 0) {
411                     Util.info(this, "Router " + routerInfo.getHost() + " added succesfully");
412                     reloadData();
413                 }
414                 else {
415                     Util.error(this, "Router " + routerInfo.getHost() + " not added");
416                 }
417             }
418             catch(Exception JavaDoc e) {
419                 handleException("Could not add router", e);
420             }
421         }
422     }
423
424     private void editRouter() {
425         RouterInfo routerInfo = findSelectedRouter();
426         if(routerInfo == null) {
427             Util.warn(this, "Please, select router first");
428             return;
429         }
430         EditRouterDialog newRouterDialog = new EditRouterDialog(this, routerInfo);
431         routerInfo = newRouterDialog.getRouterInfo();
432         if(routerInfo != null) {
433             try {
434                 if(mrtgData.updateRouter(routerInfo) == 0) {
435                     Util.info(this, "Router " + routerInfo.getHost() + " updated succesfully");
436                     reloadData();
437                 }
438                 else {
439                     Util.error(this, "Router " + routerInfo.getHost() + " not updated");
440                 }
441             }
442             catch(Exception JavaDoc e) {
443                 handleException("Could not edit router data", e);
444             }
445         }
446     }
447
448     private void removeRouter() {
449         RouterInfo routerInfo = findSelectedRouter();
450         if(routerInfo == null) {
451             Util.warn(this, "Please, select router first");
452             return;
453         }
454         int linkCount = routerInfo.getLinkInfo().length;
455         if(linkCount > 0) {
456             Util.error(this, "To remove router " + routerInfo.getHost() +
457                 " please delete associated interfaces first (" +
458                 linkCount + " found)");
459             return;
460         }
461         try {
462             if(mrtgData.deleteRouter(routerInfo) == 0) {
463                 Util.info(this, "Router " + routerInfo.getHost() + " deleted succesfully");
464                 reloadData();
465             }
466             else {
467                 Util.error(this, "Router " + routerInfo.getHost() + " not deleted");
468             }
469         } catch (Exception JavaDoc e) {
470             handleException("Could not delete router", e);
471         }
472     }
473
474     private void addLink() {
475         RouterInfo routerInfo = findSelectedRouter();
476         if(routerInfo == null) {
477             Util.warn(this, "Please, select router first");
478             return;
479         }
480         try {
481             String JavaDoc[] links = mrtgData.getAvailableLinks(routerInfo);
482             if(links == null || links.length == 0) {
483                 // no links available
484
Util.error(this, "No interfaces are available on this router");
485                 return;
486             }
487             EditLinkDialog newLinkDialog = new EditLinkDialog(this, routerInfo, links);
488             LinkInfo[] linkInfo = newLinkDialog.getLinkInfo();
489             if(linkInfo != null) {
490                 int ok = 0, bad = 0;
491                 String JavaDoc failedInterfaces = "";
492                 for(int i = 0; i < linkInfo.length; i++) {
493                     if(mrtgData.addLink(routerInfo, linkInfo[i]) == 0) {
494                         ok++;
495                     }
496                     else {
497                         bad++;
498                         failedInterfaces += linkInfo[i].getIfDescr() + "@" +
499                             routerInfo.getHost() + "\n";
500                     }
501                 }
502                 String JavaDoc message = ok + " interface(s) added successfully\n";
503                 if(bad != 0) {
504                     message += bad + " interface(s) not added:\n" + failedInterfaces;
505                     Util.error(this, message);
506                 }
507                 else {
508                     Util.info(this, message);
509                 }
510                 reloadData();
511             }
512         } catch (Exception JavaDoc e) {
513             handleException("Could not add new link", e);
514         }
515     }
516
517     private void editLink() {
518         RouterInfo routerInfo = findSelectedRouter();
519         LinkInfo linkInfo = findSelectedLink();
520         if(routerInfo == null || linkInfo == null) {
521             Util.warn(this, "Please, select interface first");
522             return;
523         }
524         try {
525             EditLinkDialog newLinkDialog = new EditLinkDialog(this, routerInfo, linkInfo);
526             LinkInfo[] linkInfoUpdated = newLinkDialog.getLinkInfo();
527             if(linkInfoUpdated != null) {
528                 if(mrtgData.updateLink(routerInfo, linkInfoUpdated[0]) == 0) {
529                     Util.info(this, "Interface " + linkInfoUpdated[0].getIfDescr() + "@" +
530                         routerInfo.getHost() + " updated successfully");
531                     reloadData();
532                 }
533                 else {
534                     Util.error(this, "Interface " + linkInfoUpdated[0].getIfDescr() + "@" +
535                         routerInfo.getHost() + " NOT updated");
536                 }
537             }
538         } catch (Exception JavaDoc e) {
539             handleException("Could not edit link data", e);
540         }
541     }
542
543     private void removeLink() {
544         RouterInfo routerInfo = findSelectedRouter();
545         LinkInfo linkInfo = findSelectedLink();
546         if(routerInfo == null || linkInfo == null) {
547             Util.warn(this, "Please, select interface first");
548             return;
549         }
550         try {
551             if(mrtgData.removeLink(routerInfo, linkInfo) == 0) {
552                 Util.info(this, "Interface " + linkInfo.getIfDescr() + "@" +
553                     routerInfo.getHost() + " removed successfully");
554                 reloadData();
555             }
556             else {
557                 Util.error(this, "Interface " + linkInfo.getIfDescr() + "@" +
558                     routerInfo.getHost() + " NOT removed");
559             }
560
561         } catch (Exception JavaDoc e) {
562             handleException("Could not remove link", e);
563         }
564     }
565
566     private void graph(int type) {
567         RouterInfo routerInfo = findSelectedRouter();
568         LinkInfo linkInfo = findSelectedLink();
569         if(routerInfo == null || linkInfo == null) {
570             Util.warn(this, "Please, select interface first");
571             return;
572         }
573         new GraphFrame(this, routerInfo, linkInfo, type);
574     }
575
576     private RouterInfo findSelectedRouter() {
577         TreePath path = mainTree.getSelectionPath();
578         if(path == null || path.getPathCount() < 2) {
579             return null;
580         }
581         else {
582             DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getPathComponent(1);
583             return (RouterInfo) node.getUserObject();
584         }
585     }
586
587     private LinkInfo findSelectedLink() {
588         TreePath path = mainTree.getSelectionPath();
589         if(path == null || path.getPathCount() < 3) {
590             return null;
591         }
592         else {
593             DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
594             return (LinkInfo) node.getUserObject();
595         }
596     }
597
598     private void handleException(String JavaDoc msg, Exception JavaDoc e) {
599         Util.error(this, msg + ":\n" + e);
600     }
601
602     public static void main(String JavaDoc[] args) throws IOException JavaDoc, XmlRpcException, InterruptedException JavaDoc {
603         SplashWindow splashWindow = new SplashWindow();
604         Thread.sleep(1000);
605         Client client = new Client();
606         client.setVisible(true);
607         splashWindow.close();
608     }
609
610 }
611
Popular Tags