- /* $Id: mibTreeDemo.java,v 1.3.2.3 2009/01/28 12:51:12 prathika Exp $ */
- /*
- * @(#)mibTreeDemo.java
- * Copyright (c) 1996-2009 AdventNet, Inc. All Rights Reserved.
- * Please read the COPYRIGHTS file for more details.
- */
- /**
- * An example of using the MibTree class with the JFC tree component.
- * This enables loading and using SNMP MIBs with the JFC.
- * mibs .Mandatory. - The mibs to be loaded.
- **/
- import javax.swing.*;
- import javax.swing.tree.*;
- import javax.swing.event.*;
- import java.awt.event.*;
- import java.awt.*;
- import com.adventnet.snmp.ui.*;
- import com.adventnet.snmp.mibs.*;
- public class mibTreeDemo extends JFrame {
- JTextArea text; // we'll use to display node information
- public mibTreeDemo(String mibs) {
- super("MainWindow");
- MibOperations mibOps = new MibOperations();
- //To load MIBs from compiled file
- mibOps.setLoadFromCompiledMibs(true);
- try {
- System.out.println("Loading MIBs: "+mibs);
- mibOps.loadMibModules(mibs);
- System.out.println("Done.");
- } catch (Exception ex) {
- System.err.println("Error: "+ex);
- }
- JPanel treepanel = new JPanel();
- JPanel displaypanel = new JPanel();
- JSplitPane splitpane = new JSplitPane();
- splitpane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
- splitpane.setLeftComponent(treepanel);
- splitpane.setDividerSize(2);
- splitpane.setRightComponent(displaypanel);
- MibTree tree = new MibTree(mibOps);
- //tree.setRootVisible(false);
- //Create the scroll pane and add the tree to it.
- JScrollPane scrollPane = new JScrollPane(tree);
- //Add the scroll pane to this tree panel.
- treepanel.setLayout(new GridLayout(1, 0));
- treepanel.setMinimumSize(new Dimension(200,300));
- treepanel.add(scrollPane);
- // lets add a text area at right
- text = new JTextArea("We'll display MIB node information here");
- text.setLineWrap(true);
- displaypanel.setLayout(new GridLayout(1, 0));
- displaypanel.add(text);
- displaypanel.setMinimumSize(new Dimension(300,300));
- // add the splitpane
- getContentPane().add(splitpane);
- setTitle("JFC Mib Tree Demo");
- // Need to setup a listener for selection to display data
- TreeSelectionListener listener = new TreeSelectionListener() {
- public void valueChanged(TreeSelectionEvent e) {
- Object selected = e.getPath().getLastPathComponent();
- if (selected instanceof NodeData) {
- Object obj = ((NodeData)selected).getUserObject();
- if (obj instanceof MibNode)
- text.setText("nn"+ ((MibNode)obj).toTagString() );
- } //else text.setText( "Selected: "+selected.getClass() );
- }
- };
- tree.getTree().addTreeSelectionListener(listener);
- tree.getTree().expandRow(0);
- } /* mibTreeDemo */
- public static void main(String[] args) {
- if(args.length<1){
- System.out.println("usage: mibTreeDemo MIB_File ");
- System.exit(0);
- }
- String mibs = args[0];
- try {
- UIManager.setLookAndFeel(
- UIManager.getSystemLookAndFeelClassName());
- } catch (Exception e) {
- System.err.println("Couldn't use the system "
- + "look and feel: " + e);
- }
- JFrame frame = new mibTreeDemo(mibs);
- WindowListener l = new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- };
- frame.addWindowListener(l);
- frame.setSize(700,500);
- frame.setVisible(true);
- }
- }