⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dwsnmpmibtreegui.java

📁 用JAVA实现的MIB浏览器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        if(node.getSyntaxID()!=DwSnmpMibRecord.VALUE_TYPE_NONE) {
          message=message+"\nValue Type: " + node.syntax.trim() + " [" + node.getSyntaxIDString() + "]";
        } else {
          message=message+"\nValue type " + node.syntax.trim() + " unknown, will use STRING.";
        }
        setValue = JOptionPane.showInputDialog(message);
        if(setValue!=null && node.checkValidValue(setValue)) {
          outputText("Request : Set  " + oid + "  Value : " + setValue);
          if (snmp.processSetRequest(node, oid, setValue) == null) {
            outputError("Error in processing variable data/set request");
            return;
          }
          //DwSnmpRequestSet(oid,setValue);
          outputText("Set command executed...");
          outputText("Getting new value of " + oid + " ...");
          sendGetRequest(oid);
        }
        return;
      }
      else if (source == btnStop) {
        snmp.destroySession();
        outputText(" ******** Cancelled *********\n");
        return;
      }
      else if (source == btnClear) {
        resultText.setText("");
        return;
      }
      else if (source == btnOidDetails) {
        mouseClicked(new MouseEvent(selectedTreeOid, 0, 0, 0, 0, 0, 0, true));
        return;
      }
      else if (source == toolbarBtnAbout) {
        JOptionPane.showMessageDialog(paneMain,
                                      "JMibBrowser version 1.1 Copyright (C) 2005 Dwipal Desai\n\n" +
                                      "This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are\n" +
                                      "welcome to redistribute it under certain conditions. See License.txt for details.\n\n" +
                                      "This software uses snmp4j from http://www.snmp4j.org.\n\n" +
                                      "Please email your suggestions to mibbrowser@dwipal.com.\n",
                                      "About JMibBrowser",
                                      JOptionPane.INFORMATION_MESSAGE);

        return;
      }
      else if (source == toolbarBtnIP) {
        try {
          String newIP = new String(" ");
          if (snmp == null) {
            snmp = new DwSnmpMibBrowserFunctions();
            snmp.setOidSupport(treeSupport.oidSupport);
          }
          getNewIPInfo();
        }
        catch (Exception e) {
          System.err.println("Error in changing IP..\n" + e.toString());
        }
        return;
      }
      else if (source == btnLoadMib) {
        loadNewMib();
        return;
      }
    }
    catch (Exception e) {
      outputError("\nError in processing user request : \n" + e.toString());
    }
  }

  void getNewIPInfo() {
    DwSnmpSelectServerDialog ipSrv = new DwSnmpSelectServerDialog();

    String ipInfo[] = ipSrv.show(snmp.getIP(), snmp.getPort(),
                                 snmp.getReadCommunity(),
                                 snmp.getWriteCommunity());

    if (ipInfo == null) {
      return;
    }

    snmp.setIP(ipInfo[0]);
    snmp.setPort(Integer.parseInt(ipInfo[1]));
    snmp.setCommunity(ipInfo[2], ipInfo[3]);

  }

  public void loadNewMib() {

    try {
      JFileChooser fileChooser = new JFileChooser();
      fileChooser.setVisible(true);
      fileChooser.setDialogTitle("Select the MIB File to Load");
      fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
      fileChooser.setCurrentDirectory(new File("."));
      fileChooser.setMultiSelectionEnabled(true);

      String strFileName = "";
      int returnVal = fileChooser.showOpenDialog(null);
      if (returnVal == JFileChooser.APPROVE_OPTION) {
        File[] files = fileChooser.getSelectedFiles();
        if (files != null && files.length > 0) {
          for (int i = 0; i < files.length; i++) {
            try {
              loadSingleFile(files[i]);
            }
            catch (Exception e) {
              outputError("Error in loading file: " + files[i].getAbsolutePath());
            }
          }
        }
      }
      else {
        return;
      }

//			treeSupport.addFile(strFileName);
    }
    catch (Exception e) {
      System.out.println("Error in getting new MIB Filenames.\n" + e.toString());
    }

    /*		try {
       myTree.removeTreeSelectionListener(this);
       myTree.removeAll();
       treeScrollPane.remove(myTree);
       System.out.println("Building tree..");
       String fileNames[]=treeSupport.getFiles();
       treeSupport=new DwSnmpMibTreeBuilder();
       treeSupport.setOutput(output);
       try {
        for(int i=0;i<fileNames.length;i++) {
         treeSupport.addFile(fileNames[i]);
        }
       } catch(Exception e) {
     System.out.println("Error in loadin new MIB Filenames.\n" + e.toString());
       }

       try{
       myTree=treeSupport.buildTree();
       } catch(Exception e) {
       System.out.println("Error in building new MIB tree.\n" + e.toString());
       }

       myTree.addTreeSelectionListener(this);
       treeScrollPane.setEnabled(true);
       //treeScrollPane.add(myTree);
       treeScrollPane.setViewportView(myTree);
       treeScrollPane.repaint();
      }catch(Exception e){
       System.out.println("Error in loading new MIB.\n" + e.toString());
      }

     */
  }

  private void loadSingleFile(File file) {
    String strFileName = file.getAbsolutePath();
    treeSupport.loadNewFile(strFileName);
  }

  public static void main(String[] args) {
//    try {
//      UIManager.setLookAndFeel(
//          UIManager.getSystemLookAndFeelClassName());
//    }
//    catch (Exception e) {}
//
    //Create the top-level container and add contents to it.
    JFrame frame = new JFrame("MIB Browser");
    frame.setSize(700, 550);
    DwSnmpMibTreeGUI tree1 = new DwSnmpMibTreeGUI();
    Component comp = tree1.createComponents();
    JTree t = tree1.getTree();
    TreePath tp = null;
    for(int i=0; i<t.getRowCount(); i++){
      t.expandRow(i);
      Object []path = t.getPathForRow(i).getPath();
      tp = t.getPathForRow(i);
      if(path.length>1){
        System.out.println(path[path.length-1]+" ["+ path[path.length-2]+"]");
      }else{
        System.out.println("Root:"+path[0]);
      }
    }
    if (comp != null) {
      frame.getContentPane().add(comp);
    }
    else {
      JOptionPane.showMessageDialog(frame.getContentPane(),
                                    "Error in loading default MIBs.");
    }
//    Finish setting up the frame, and show it.
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    
//    t.setSelectionPath(tp);
//    t.expandPath(tp);
//    DefaultMutableTreeNode node = (DefaultMutableTreeNode) t.getLastSelectedPathComponent();
//    System.out.println(tree1.getOid(node));
//    if (node == null) {
//      selectedTreeOid.setText(" ");
//      return;
//    }
//    selectedTreeOid.setText(treeSupport.oidSupport.getNodeOid(node));
    frame.setVisible(true);
  }
  
  public String getOid(DefaultMutableTreeNode node){
    return treeSupport.oidSupport.getNodeOid(node);
  }

  public void sendGetRequest(String strReq) {
    if (strReq.endsWith("0")) {
      strReq = strReq.substring(0, strReq.lastIndexOf("."));
      outputText("Request : Get  " + strReq + "\n");
    } else if (strReq.endsWith("*")) {
      strReq = strReq.substring(0, strReq.lastIndexOf("*") - 1);
      outputText("Request : Walk " + strReq + "\n");
    } else if (strReq.endsWith(")")) {
      strReq = strReq.substring(0, strReq.indexOf("(") - 1);
      outputText("Request : Walk " + strReq + "\n");
    } else {
      outputError("Error in request. Please check the OID.");
    }

    final String strReqFin=strReq;
    Thread t=new Thread(new Runnable() {
      public void run() {
        snmp.snmpRequestGet(strReqFin);
      }
    });
    t.start();
  }


  void outputText(String s) {
    if (output != null) {
      output.println(s);
    }
    else {
      System.out.println(s);
    }
  }

  void outputError(String e) {
    if (output != null) {
      output.printError(e);
    }
    else {
      System.out.println(e);

    }
  }

  private void jbInit() throws Exception {
  }
} // END OF CLASS *****************************************

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -