/*$Id: lineGraphDemo.java,v 1.3 2002/09/09 05:35:19 tonyjpaul Exp $*/ /* * @(#)lineGraphDemo.java * Copyright (c) 2012 ZOHO Corp. All Rights Reserved. * Please read the COPYRIGHTS file for more details. */ import java.awt.*; import java.util.*; import java.awt.event.*; import java.applet.Applet; import com.adventnet.snmp.beans.*; import com.adventnet.snmp.ui.*; import javax.swing.*; import java.io.*; public class lineGraphDemo extends JFrame implements ActionListener,ComponentListener { //Declare the beans LineGraphBean lineGraph = null; SnmpPoller poller = null; JButton button = null; JButton loadButton = null; JButton browseButton = null; JTextField communityText = null; JTextField oidText = null; JTextField mibText = null; JTextField hostText = null; JTextField intervalText = null; JPanel editPanel = new JPanel(); JPanel labelPanel = new JPanel(); long oldValue = 0; public static void main(String [] args) { try { UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) {} lineGraphDemo frame = new lineGraphDemo(); frame.show(); } public lineGraphDemo() { this.resize(650,400); this.setTitle( "LineGraph Demo" ); } public void init() { // Set the layout for the container getContentPane().setLayout(null); //Instantiate lineGraph //lineGraph= new LineGraphBean(300,250); lineGraph= new LineGraphBean(); lineGraph.setBounds(20,50,300,250); lineGraph.setLfontsize(15); lineGraph.setLfontstyle("ITALIC"); communityText = new JTextField("public", 10); oidText = new JTextField("1.3.0", 10); intervalText = new JTextField("1", 10); hostText = new JTextField("localhost", 10); mibText = new JTextField(10); loadButton = new JButton("Load MIB"); loadButton.addActionListener(this); browseButton = new JButton("Browse..."); browseButton.addActionListener(this); button= new JButton("Start Polling"); button.addActionListener(this); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); //JPanel editPanel = new JPanel(); editPanel.setLayout(gridbag); c.fill = GridBagConstraints.BOTH; c.insets = new Insets(0,0,5,0); addSingle(editPanel, gridbag, c, "Host :", hostText); addSingle(editPanel, gridbag, c, "Community :", communityText); addSingle(editPanel, gridbag, c, "OID :", oidText); addSingle(editPanel, gridbag, c, "Interval :", intervalText); addSingle(editPanel, gridbag, c, "MIB Url :", mibText); addSingle(editPanel, gridbag, c, "", browseButton); addSingle(editPanel, gridbag, c, "", loadButton); c.insets = new Insets(5,0,0,0); addSingle(editPanel, gridbag, c, "", button); editPanel.setBounds( 325, 50 ,250 ,250 ); JLabel label = new JLabel("You can give space separated list of OIDs"); labelPanel.add("Center",label); labelPanel.setBounds(10,10,300,25); getContentPane().add("North", labelPanel); getContentPane().add("West", lineGraph); getContentPane().add("East", editPanel); addComponentListener(this); //Instantiate snmpPoller poller= new SnmpPoller(); //Set the modified properties for lineGraph try { lineGraph.setForeground(new Color(-16764109)); lineGraph.setBackground(new Color(-16764109)); }catch(Exception ex){}; poller.addResultListener(lineGraph); // listen for response events poller.setSendTimeoutEvents(true); this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); } }); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Browse...")) { FileDialog localFd = new FileDialog(this, "Mib Load Dialog", FileDialog.LOAD); localFd.setSize(700,300); localFd.show(); String dir = localFd.getDirectory(); String file = localFd.getFile(); if((dir != null)&&(file != null)) mibText.setText(dir + file); } if (e.getActionCommand().equals("Load MIB")) { if(mibText.getText().trim().equals("")) System.err.println("Specify MIBs path"); try { poller.loadMibs( mibText.getText() ); //mibText.setText(""); } catch(Exception me) { System.out.println("Loading MIB Failed. "+ me); //System.exit(1); } } if (e.getActionCommand().equals("Stop Polling")) { poller.stopPolling(); button.setLabel("Start Polling"); } else if (e.getActionCommand().equals("Start Polling")) { // else restart it setValues(); poller.restartPolling(); button.setLabel("Stop Polling"); } } public void show() { init(); super.show(); } public void setValues() { // load MIB to allow us to use names try{ poller.setPollInterval( Integer.parseInt(intervalText.getText()) ); }catch (NumberFormatException nfm){ System.err.println("PollInterval should be an integer"); intervalText.setText(String.valueOf(poller.getPollInterval())); } poller.setCommunity( communityText.getText() ); poller.setTargetHost( hostText.getText() ); String lineLabels = oidText.getText(); StringTokenizer stoken = new StringTokenizer(lineLabels, " "); int num = stoken.countTokens(); lineGraph.setTitle("Plotting "+num +" OIDs"); String oidList[] = new String[num]; for(int i=0; i