📄 dwsnmpmibtreegui.java
字号:
package com.dwipal;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JTree;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.io.*;
import java.util.*;
public class DwSnmpMibTreeGUI
implements ActionListener, MouseListener, TreeSelectionListener {
DwSnmpMibTreeBuilder treeSupport;
//DwSnmpOidSupport oidSupport;
DwSnmpMibBrowserFunctions snmp;
DwSnmpMibOutputHandler output = new DwSnmpMibOutputHandler();
JTree myTree;
JScrollPane treeScrollPane;
JPanel treePane;
JButton btnLoadMib;
JPanel paneMain = new JPanel(new BorderLayout());
// Other GUI stuff
JTextField selectedTreeOid = new JTextField("Selected oid..");
JTextArea resultText;
JButton btnGet = new JButton("Get");
JButton btnSet = new JButton("Set");
JButton btnStop = new JButton("Stop");
JCheckBox chkScroll = new JCheckBox("Scroll Display");
JButton btnOidDetails = new JButton("Details");
JButton btnClear = new JButton("Clear");
// Tooltips and Toolbars
JToolBar mainToolbar;
JButton toolbarBtnIP;
JButton toolbarBtnAbout;
JToolBar statusToolbar;
// Initial Vars
PipedInputStream pin;
PipedOutputStream pout;
PrintStream out;
BufferedReader in;
public DwSnmpMibBrowserFunctions getSnmpObject(){
return snmp;
}
public DwSnmpMibTreeGUI() {
output.setLogging(true);
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public DwSnmpOidSupport getOidSupport() {
return treeSupport.oidSupport;
}
public JPanel getMainPane() {
return paneMain;
}
public JTree getTree() {
return myTree;
}
public Component createComponents() {
// First initialise the output text area and Status areas
btnOidDetails.setToolTipText("Get details of selected element");
btnClear.setToolTipText("Clear the contents of result window");
btnOidDetails.addActionListener(this);
btnClear.addActionListener(this);
resultText = new JTextArea();
JScrollPane resultPane = new JScrollPane(resultText);
// Set everyone's output to resulttext
output = new DwSnmpMibOutputHandler();
output.setOutput(resultText);
output.setOutputError(resultText);
snmp = new DwSnmpMibBrowserFunctions();
snmp.setOutput(output);
selectedTreeOid = new JTextField("Your Selection");
// Create a tooltip for jlabel, and also add a message handler to it.
selectedTreeOid.setToolTipText(
"Click here for more information on this variable");
selectedTreeOid.setText("Selected Element's OID");
selectedTreeOid.addMouseListener(this);
// Create the TREE and Tree pane.
outputText("Building tree..");
treeSupport = new DwSnmpMibTreeBuilder();
treeSupport.setOutput(output);
String projectdir = System.getProperty("ProjectDir");
if (projectdir == null) {
projectdir = "D:/";
}
if (treeSupport.addDirectory(projectdir + "/mibs/") == false) {
outputError("Directory " + projectdir +
"/mibs/ not found, or it is an empty directory!");
}
//treeSupport.addFile("mib_core.txt");
// treeSupport.addFile("mib_II.txt");
myTree = treeSupport.buildTree();
if (myTree == null || treeSupport.oidSupport == null) {
outputError("Error in loading MIB tree, quitting");
return null;
}
snmp.setOidSupport(treeSupport.oidSupport);
myTree.addTreeSelectionListener(this);
treeScrollPane = new JScrollPane(myTree);
btnLoadMib = new JButton("Load MIB");
treePane = new JPanel(new BorderLayout());
treePane.add("Center", treeScrollPane);
treePane.add("South", btnLoadMib);
//buildOidToNameResolutionTable(rootNode,oidResolveHash);
statusToolbar = new JToolBar();
statusToolbar.add(btnClear);
statusToolbar.add(btnOidDetails);
statusToolbar.addSeparator();
statusToolbar.add(selectedTreeOid);
// Create the Right pane containing buttons,status and textbox
btnGet.setToolTipText("Get the values for selected element");
btnSet.setToolTipText("Set the value of selected element");
btnStop.setToolTipText("Stop the current action");
JPanel paneBtn = new JPanel(new FlowLayout());
btnGet.addActionListener(this);
btnSet.addActionListener(this);
btnStop.addActionListener(this);
chkScroll.setSelected(true);
chkScroll.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
output.setAutoScroll(chkScroll.isSelected());
}
});
paneBtn.add(btnGet);
paneBtn.add(btnSet);
paneBtn.add(btnStop);
paneBtn.add(chkScroll);
JPanel paneStatus = new JPanel(new BorderLayout());
paneStatus.add("South", paneBtn);
paneStatus.add("North", statusToolbar);
paneStatus.add("Center", resultPane);
// Create the Main Toolbar
mainToolbar = new JToolBar();
toolbarBtnIP = new JButton("Select Server");
toolbarBtnAbout = new JButton("About");
toolbarBtnIP.addActionListener(this);
toolbarBtnAbout.addActionListener(this);
btnLoadMib.addActionListener(this);
mainToolbar.add(toolbarBtnIP);
mainToolbar.add(toolbarBtnAbout);
// Create the Content pane and add other panes to it :)
JSplitPane paneContent = new JSplitPane();
paneContent.setLeftComponent(treePane);
paneContent.setRightComponent(paneStatus);
paneContent.setDividerLocation(250);
// Finally create the Main Pane with the toolbar and content pane in it
paneMain.add("Center", paneContent);
paneMain.add("North", mainToolbar);
return paneMain;
}
/** Returns the tree pane
*/
public JPanel getTreePane() {
return treePane;
}
public void setTreePane(JPanel treePanel) {
}
/** TREE SELECTION LISTENER.
* LISTENS TO THE EVENTS IN THE TREE "myTree"
*/
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) myTree.
getLastSelectedPathComponent();
if (node == null) {
selectedTreeOid.setText(" ");
return;
}
selectedTreeOid.setText(treeSupport.oidSupport.getNodeOid(node));
}
/** END OF TREE SELECTION EVENT LISTENER
*/
public void mouseClicked(MouseEvent evt) {
Object source = evt.getSource();
if (source == selectedTreeOid) {
DwSnmpMibRecord node = getSelectedTreeNode();
if(node != null)
outputText(node.getCompleteString());
}
}
public DwSnmpMibRecord getSelectedTreeNode() {
DwSnmpMibRecord ret=null;
DefaultMutableTreeNode node = (DefaultMutableTreeNode) myTree.getLastSelectedPathComponent();
if(node!=null) {
ret=(DwSnmpMibRecord) node.getUserObject();
}
return ret;
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
try {
if (source == btnGet) {
sendGetRequest(selectedTreeOid.getText());
return;
}
else if (source == btnSet) {
DwSnmpMibRecord node=getSelectedTreeNode();
if(!node.isWritable()) {
JOptionPane.showMessageDialog(getMainPane(), "The selected node is not writable.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
String oid = selectedTreeOid.getText();
String oidText=getOidSupport().resolveOidName(oid);
String setValue = "";
String message="Enter new value for " + oidText;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -