MibBrowserApplication.java.txt
上传用户:aonuowh
上传日期:2021-05-23
资源大小:35390k
文件大小:10k
源码类别:
SNMP编程
开发平台:
C/C++
- /* $Id: MibBrowserApplication.src,v 1.16 2002/09/09 06:20:20 tonyjpaul Exp $ */
- /**
- * MibBrowserApplication.java
- * Copyright (c) 1996-2003 AdventNet, Inc. All Rights Reserved.
- * Please read the COPYRIGHTS file for more details.
- * ADVENTNET, INC. MAKES NO REPRESENTATIONS OR WARRANTIES
- * ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED,
- * INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. ADVENTNET,
- * INC. SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
- * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE
- * OR ITS DERIVATIVES.
- */
- import com.adventnet.snmp.snmp2.*;
- import com.adventnet.snmp.ui.*;
- import com.adventnet.snmp.mibs.*;
- import com.adventnet.utils.*;
- import javax.swing.*;
- import java.io.*;
- import javax.swing.event.*;
- import java.util.StringTokenizer;
- import java.util.Locale;
- import java.awt.*;
- import java.awt.event.*;
- import com.sun.jimi.core.component.JimiCanvas;
- import com.adventnet.builder.framework.utils.startup.StartupWin;
- /** A MIB Browser Application using the MIB Browser Bean. **/
- public class MibBrowserApplication extends JFrame implements ActionListener {
- static String values[] = // stores the options
- { null, null, null, null, null, null, null, null, "None", null, null, null };
- JMenuItem exitItem;
- boolean conExitOpen = false;
- MibBrowser mbrowser;
- /** Setup the main frame and initialize MIB Browser bean **/
- public MibBrowserApplication() {
- // Since internationalization cannot be done here, super is commented out.
- // setTitle is called from main method to set the title.
- //super(SnmpUtils.getString("AdventNet MibBrowser"));
- getContentPane().setLayout(new BorderLayout());
- String path=System.getProperty("user.dir");
- String confDir = null;
- if(path.endsWith("MibBrowser")){
- path=System.getProperty("user.dir")+File.separator+".."+File.separator+"images"+File.separator+"about.jpg";
- }else{
- confDir = path + File.separator + "conf" + File.separator;
- MibBrowser.setConfDir(confDir);
- path=System.getProperty("user.dir")+File.separator+"images"+File.separator+"about.jpg";
- }
- StartupWin startwin=new StartupWin(this,path);
- startwin.setVisible(true);
- startwin.setProgressValues(100,0,100);
- startwin.showProgress(1);
- if(values[8].equals("Set"))
- {
- if(values[9] == null || values[10] == null)
- {
- System.out.println(SnmpUtils.getString("Please enter the Locale and Font for Internationalization"));
- System.exit(1);
- }else
- {
- StringTokenizer strt=new StringTokenizer(values[9],"| ");
- if(strt.countTokens() != 2)
- {
- System.out.println(SnmpUtils.getString("Enter the Language and Country name for Locale"));
- System.exit(1);
- }
- String temp=(String)strt.nextToken();
- String temp1=(String)strt.nextToken();
- if(temp.indexOf("(")!=-1)
- {
- temp=temp.substring(0,temp.indexOf("("));
- }
- if(temp1.indexOf("(")!=-1)
- {
- temp1=temp1.substring(0,temp1.indexOf("("));
- }
- Locale locale=new Locale(temp,temp1);
- strt=new StringTokenizer(values[10],"| ");
- if(strt.countTokens() != 3)
- {
- System.out.println(SnmpUtils.getString("Enter the Font name, Style and Size for the font"));
- System.exit(1);
- }
- if(values[11] != null)
- SnmpUtils.setSearchPath(values[11]);
- try{
- Font f=new Font((String)strt.nextToken(), Integer.parseInt((String)strt.nextToken()),Integer.parseInt((String)strt.nextToken()));
- mbrowser.internationalize(locale,f);
- }catch(Exception execpt){
- execpt.printStackTrace();
- System.exit(1);
- }
- }
- }
- startwin.showProgress(10);
- mbrowser = new MibBrowser();
- startwin.showProgress(50);
- /**
- * You can customize the MenuBar. You can
- * Add and Remove the MenuItems from the MenuBar.
- * Exit MenuItem is added to the File Menu.
- */
- JMenuBar bar = mbrowser.getMenuBar();
- JMenu fileMenu = bar.getMenu(0);
- fileMenu.addSeparator();
- exitItem =new JMenuItem(SnmpUtils.getString("Exit"),'x');
- Font lfont=SnmpUIUtils.getFont();//new Font("Regular",Font.PLAIN,12);
- exitItem.setFont(lfont);
- fileMenu.add(exitItem);
- exitItem.addActionListener(this);
- startwin.showProgress(60);
- /**
- * MenuBar is an Option
- */
- mbrowser.setMenuBarVisible(true);
- /**
- * You can customize the MibBrowser ToolBar. You can
- * Add and Remove the Buttons from the ToolBar.
- * Exit Button is added to the ToolBar.
- */
- startwin.showProgress(70);
- /**
- * To remove a button use the following method.
- *
- * Note:Use the index starting from 0. If a button
- * is removed then the indexes will be changed.
- * e.g. If you want to remove 1st and 2nd buttons
- * indexes are 0 and 0. Because if you remove first
- * button then the next button becomes the first button
- * its index become 0.
- */
- //toolbar.removeComponentAt(0);
- if (values[0] != null) mbrowser.setCommunity(values[0]);
- try {
- if (values[1] != null) mbrowser.setTargetPort(Integer.parseInt(values[1]));
- if (values[2] != null) mbrowser.setRetries(values[2]);
- if (values[3] != null) mbrowser.setTimeout(values[3]);
- } catch (NumberFormatException ex) {
- System.err.println(SnmpUtils.getString("Invalid option: ")+" "+ex);
- }
- if (values[5] != null) mbrowser.setTargetHost(values[5]);
- if (values[6] != null) {
- if(values[6].equals("v2"))
- mbrowser.setSnmpVersion( SnmpAPI.SNMP_VERSION_2C ) ;
- else if(values[6].equals("v1"))
- mbrowser.setSnmpVersion( SnmpAPI.SNMP_VERSION_1 );
- else if(values[6].equals("v3"))
- mbrowser.setSnmpVersion( SnmpAPI.SNMP_VERSION_3 );
- else {
- System.out.println(SnmpUtils.getString("Invalid Version Number"));
- System.exit(0);
- }
- }
- /**
- * If database params are set, call the initJdbcParams or
- * setting to load from compiled mibs by default.
- */
- startwin.showProgress(80);
- if(values[7] != null)
- {
- StringTokenizer st = new StringTokenizer(values[7],"| ");
- if(st.countTokens()==4)
- {
- try{
- String s1=(String)st.nextToken();
- String s2=(String)st.nextToken();
- String s3,s4;
- if((s3=(String)st.nextToken()).equals("null"))
- {
- s3="";
- }
- if((s4=(String)st.nextToken()).equals("null"))
- {
- s4="";
- }
- mbrowser.initJdbcParams(s1,s2,s3,s4);
- }catch(Exception e)
- {
- System.out.println(SnmpUtils.getString("Error in Database Params :")+" "+e);
- }
- }
- mbrowser.setLoadMibsFromDatabase(true);
- }
- startwin.showProgress(90);
- WindowListener l = new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- if(!conExitOpen && confirmExit()) {
- mbrowser.releaseResources();
- System.exit(0);
- }
- }
- };
- this.addWindowListener(l);
- this.setIconImage((createImageIcon("logo.png")).getImage());
- this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
- getContentPane().add("Center", mbrowser);
- startwin.showProgress(100);
- startwin.setVisible(false);
- }
- ImageIcon createImageIcon(String names)
- {
- JimiCanvas canvas=new JimiCanvas();
- canvas.setImageLocation(getClass().getResource(names));
- return new ImageIcon(canvas.getImage());
- }
- /** Get command line options and setup the MIB Browser accordingly **/
- public static void main(String[] args) {
- // Take care of getting options
- String usage = "java MibBrowserApplication [-v version(v1/v2/v3)][-m MIB_files] [-c community] [-p port] [-t timeout] [-r retries] [-h host] [-db "driverName url username password"] [-i "Internationalize"] [-L "Locale (language,country) "] [-F "Font (fontname,style,size)"] [-S "MibBrower properties file path" ] ";
- String options[] = { "-c", "-p", "-r", "-t", "-m", "-h", "-v","-db", "-i", "-L", "-F", "-S" };
- ParseOptions opt = new ParseOptions(args,options,values, usage);
- if (opt.remArgs.length != 0) opt.usage_error();
- try {
- UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName());
- } catch (Exception e) {
- System.err.println(SnmpUtils.getString("Couldn't use the system look and feel: ")+" "+ e);
- }
- MibBrowserApplication jframe = new MibBrowserApplication();
- jframe.setTitle(SnmpUtils.getString("AdventNet MibBrowser"));
- jframe.setSize(730,500);
- centerWindow(jframe);
- jframe.setVisible(true);
- if (values[4] != null)
- {
- if(values[4].indexOf('(')!=-1)
- {
- String paths="";
- StringTokenizer str=new StringTokenizer(values[4],"|");
- while(str.hasMoreTokens())
- {
- //replace ( with " " to avoid the Program Files problems
- paths=paths + """ + ((String)str.nextToken()).replace('(',' ') + """+ "|";
- }
- paths=paths.substring(0,paths.length()-1);
- values[4]=paths;
- }
- jframe.mbrowser.setMibModules(values[4]);
- }
- else {
- if(!jframe.mbrowser.isLoadRecentMibs()) {
- String path = System.getProperty("user.dir");
- if(path.endsWith("MibBrowser")) {
- int index = path.indexOf("MibBrowser");
- path = path.substring(0, index-1);
- }
- if(path.indexOf(" ") != -1) {
- jframe.mbrowser.setMibModules(""" + path + File.separator + "mibs" + File.separator + "RFC1213-MIB" + """);
- }
- else {
- jframe.mbrowser.setMibModules(path + File.separator + "mibs" + File.separator + "RFC1213-MIB");
- }
- }
- }
- jframe.mbrowser.getMibTree().getTree().expandRow(0);
- }
- public void actionPerformed(ActionEvent e) {
- if(e.getActionCommand().equals(SnmpUtils.getString("Exit"))) {
- if(!conExitOpen && confirmExit()) {
- mbrowser.releaseResources();
- this.dispose();
- System.exit(0);
- }
- }
- }
- private static void centerWindow(Component win) {
- Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
- Point screenCenter = new Point((screenSize.width/2), (screenSize.height/2));
- int x = screenCenter.x-win.getSize().width/2;
- int y = screenCenter.y-win.getSize().height/2;
- if (x < 0) x=0;
- if (y<0) y=0;
- win.setLocation( x, y);
- }
- private boolean confirmExit() {
- conExitOpen = true;
- int retval = JOptionPane.showConfirmDialog(this,
- SnmpUtils.getString("Do you want to really quit"),
- SnmpUtils.getString("Confirm Exit"),
- JOptionPane.YES_NO_OPTION);
- if (retval != JOptionPane.YES_OPTION)
- conExitOpen = false;
- return (retval == JOptionPane.YES_OPTION);
- }
- }