1 package com.dwipal; 2 3 import javax.swing.*; 4 import java.awt.*; 5 import java.awt.event.*; 6 import javax.swing.JTree ; 7 import javax.swing.tree.*; 8 import javax.swing.event.*; 9 import java.io.*; 10 import java.util.*; 11 12 public class DwSnmpMibTreeGUI 13 implements ActionListener, MouseListener, TreeSelectionListener { 14 15 DwSnmpMibTreeBuilder treeSupport; 16 DwSnmpMibBrowserFunctions snmp; 18 DwSnmpMibOutputHandler output = new DwSnmpMibOutputHandler(); 19 20 JTree myTree; 21 JScrollPane treeScrollPane; 22 JPanel treePane; 23 24 JButton btnLoadMib; 25 26 JPanel paneMain = new JPanel(new BorderLayout()); 27 28 JTextField selectedTreeOid = new JTextField("Selected oid.."); 30 JTextArea resultText; 31 JButton btnGet = new JButton("Get"); 32 JButton btnSet = new JButton("Set"); 33 JButton btnStop = new JButton("Stop"); 34 JCheckBox chkScroll = new JCheckBox("Scroll Display"); 35 JButton btnOidDetails = new JButton("Details"); 36 JButton btnClear = new JButton("Clear"); 37 38 JToolBar mainToolbar; 40 JButton toolbarBtnIP; 41 JButton toolbarBtnAbout; 42 43 JToolBar statusToolbar; 44 46 PipedInputStream pin; 47 PipedOutputStream pout; 48 PrintStream out; 49 BufferedReader in; 50 51 public DwSnmpMibTreeGUI() { 52 output.setLogging(true); 53 try { 54 jbInit(); 55 } 56 catch (Exception ex) { 57 ex.printStackTrace(); 58 } 59 } 60 61 public DwSnmpOidSupport getOidSupport() { 62 return treeSupport.oidSupport; 63 } 64 65 public JPanel getMainPane() { 66 return paneMain; 67 } 68 69 public JTree getTree() { 70 return myTree; 71 } 72 73 public Component createComponents() { 74 btnOidDetails.setToolTipText("Get details of selected element"); 76 btnClear.setToolTipText("Clear the contents of result window"); 77 btnOidDetails.addActionListener(this); 78 btnClear.addActionListener(this); 79 80 resultText = new JTextArea(); 81 JScrollPane resultPane = new JScrollPane(resultText); 82 83 output = new DwSnmpMibOutputHandler(); 85 output.setOutput(resultText); 86 output.setOutputError(resultText); 87 snmp = new DwSnmpMibBrowserFunctions(); 88 snmp.setOutput(output); 89 90 selectedTreeOid = new JTextField("Your Selection"); 91 92 selectedTreeOid.setToolTipText( 94 "Click here for more information on this variable"); 95 selectedTreeOid.setText("Selected Element's OID"); 96 selectedTreeOid.addMouseListener(this); 97 98 100 outputText("Building tree.."); 101 treeSupport = new DwSnmpMibTreeBuilder(); 102 treeSupport.setOutput(output); 103 String projectdir = System.getProperty("ProjectDir"); 104 if (projectdir == null) { 105 projectdir = "."; 106 } 107 if (treeSupport.addDirectory(projectdir + "/mibs/") == false) { 108 outputError("Directory " + projectdir + 109 "/mibs/ not found, or it is an empty directory!"); 110 } 111 112 115 myTree = treeSupport.buildTree(); 116 if (myTree == null || treeSupport.oidSupport == null) { 117 outputError("Error in loading MIB tree, quitting"); 118 return null; 119 } 120 snmp.setOidSupport(treeSupport.oidSupport); 121 myTree.addTreeSelectionListener(this); 122 treeScrollPane = new JScrollPane(myTree); 123 124 btnLoadMib = new JButton("Load MIB"); 125 treePane = new JPanel(new BorderLayout()); 126 treePane.add("Center", treeScrollPane); 127 treePane.add("South", btnLoadMib); 128 129 131 statusToolbar = new JToolBar(); 132 statusToolbar.add(btnClear); 133 statusToolbar.add(btnOidDetails); 134 statusToolbar.addSeparator(); 135 statusToolbar.add(selectedTreeOid); 136 137 btnGet.setToolTipText("Get the values for selected element"); 139 btnSet.setToolTipText("Set the value of selected element"); 140 btnStop.setToolTipText("Stop the current action"); 141 142 JPanel paneBtn = new JPanel(new FlowLayout()); 143 btnGet.addActionListener(this); 144 btnSet.addActionListener(this); 145 btnStop.addActionListener(this); 146 chkScroll.setSelected(true); 147 chkScroll.addActionListener(new ActionListener() { 148 public void actionPerformed(ActionEvent e) { 149 output.setAutoScroll(chkScroll.isSelected()); 150 } 151 }); 152 153 paneBtn.add(btnGet); 154 paneBtn.add(btnSet); 155 paneBtn.add(btnStop); 156 paneBtn.add(chkScroll); 157 158 JPanel paneStatus = new JPanel(new BorderLayout()); 159 paneStatus.add("South", paneBtn); 160 paneStatus.add("North", statusToolbar); 161 paneStatus.add("Center", resultPane); 162 163 mainToolbar = new JToolBar(); 165 toolbarBtnIP = new JButton("Select Server"); 166 toolbarBtnAbout = new JButton("About"); 167 168 toolbarBtnIP.addActionListener(this); 169 toolbarBtnAbout.addActionListener(this); 170 btnLoadMib.addActionListener(this); 171 172 mainToolbar.add(toolbarBtnIP); 173 mainToolbar.add(toolbarBtnAbout); 174 175 JSplitPane paneContent = new JSplitPane(); 177 paneContent.setLeftComponent(treePane); 178 paneContent.setRightComponent(paneStatus); 179 paneContent.setDividerLocation(250); 180 181 paneMain.add("Center", paneContent); 183 paneMain.add("North", mainToolbar); 184 185 return paneMain; 186 } 187 188 190 public JPanel getTreePane() { 191 return treePane; 192 } 193 194 public void setTreePane(JPanel treePanel) { 195 196 } 197 198 201 202 public void valueChanged(TreeSelectionEvent e) { 203 DefaultMutableTreeNode node = (DefaultMutableTreeNode) myTree. 204 getLastSelectedPathComponent(); 205 if (node == null) { 206 selectedTreeOid.setText(" "); 207 return; 208 } 209 selectedTreeOid.setText(treeSupport.oidSupport.getNodeOid(node)); 210 } 211 212 214 public void mouseClicked(MouseEvent evt) { 215 Object source = evt.getSource(); 216 if (source == selectedTreeOid) { 217 DwSnmpMibRecord node = getSelectedTreeNode(); 218 if(node != null) 219 outputText(node.getCompleteString()); 220 } 221 } 222 private DwSnmpMibRecord getSelectedTreeNode() { 223 DwSnmpMibRecord ret=null; 224 DefaultMutableTreeNode node = (DefaultMutableTreeNode) myTree.getLastSelectedPathComponent(); 225 if(node!=null) { 226 ret=(DwSnmpMibRecord) node.getUserObject(); 227 } 228 return ret; 229 } 230 231 public void mouseEntered(MouseEvent e) {} 232 233 public void mouseExited(MouseEvent e) {} 234 235 public void mousePressed(MouseEvent e) {} 236 237 public void mouseReleased(MouseEvent e) {} 238 239 public void actionPerformed(ActionEvent evt) { 240 Object source = evt.getSource(); 241 242 try { 243 if (source == btnGet) { 244 sendGetRequest(selectedTreeOid.getText()); 245 return; 246 } 247 else if (source == btnSet) { 248 DwSnmpMibRecord node=getSelectedTreeNode(); 249 if(!node.isWritable()) { 250 JOptionPane.showMessageDialog(getMainPane(), "The selected node is not writable.", "Error", JOptionPane.ERROR_MESSAGE); 251 return; 252 } 253 254 String oid = selectedTreeOid.getText(); 255 String oidText=getOidSupport().resolveOidName(oid); 256 String setValue = ""; 257 String message="Enter new value for " + oidText; 258 if(node.getSyntaxID()!=DwSnmpMibRecord.VALUE_TYPE_NONE) { 259 message=message+"\nValue Type: " + node.syntax.trim() + " [" + node.getSyntaxIDString() + "]"; 260 } else { 261 message=message+"\nValue type " + node.syntax.trim() + " unknown, will use STRING."; 262 } 263 setValue = JOptionPane.showInputDialog(message); 264 if(setValue!=null && node.checkValidValue(setValue)) { 265 outputText("Request : Set " + oid + " Value : " + setValue); 266 if (snmp.processSetRequest(node, oid, setValue) == null) { 267 outputError("Error in processing variable data/set request"); 268 return; 269 } 270 outputText("Set command executed..."); 272 outputText("Getting new value of " + oid + " ..."); 273 sendGetRequest(oid); 274 } 275 return; 276 } 277 else if (source == btnStop) { 278 snmp.destroySession(); 279 outputText(" ******** Cancelled *********\n"); 280 return; 281 } 282 else if (source == btnClear) { 283 resultText.setText(""); 284 return; 285 } 286 else if (source == btnOidDetails) { 287 mouseClicked(new MouseEvent(selectedTreeOid, 0, 0, 0, 0, 0, 0, true)); 288 return; 289 } 290 else if (source == toolbarBtnAbout) { 291 JOptionPane.showMessageDialog(paneMain, 292 "JMibBrowser version 1.1 Copyright (C) 2005 Dwipal Desai\n\n" + 293 "This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are\n" + 294 "welcome to redistribute it under certain conditions. See License.txt for details.\n\n" + 295 "This software uses snmp4j from http://www.snmp4j.org.\n\n" + 296 "Please email your suggestions to mibbrowser@dwipal.com.\n", 297 "About JMibBrowser", 298 JOptionPane.INFORMATION_MESSAGE); 299 300 return; 301 } 302 else if (source == toolbarBtnIP) { 303 try { 304 String newIP = new String (" "); 305 if (snmp == null) { 306 snmp = new DwSnmpMibBrowserFunctions(); 307 snmp.setOidSupport(treeSupport.oidSupport); 308 } 309 getNewIPInfo(); 310 } 311 catch (Exception e) { 312 System.err.println("Error in changing IP..\n" + e.toString()); 313 } 314 return; 315 } 316 else if (source == btnLoadMib) { 317 loadNewMib(); 318 return; 319 } 320 } 321 catch (Exception e) { 322 outputError("\nError in processing user request : \n" + e.toString()); 323 } 324 } 325 326 void getNewIPInfo() { 327 DwSnmpSelectServerDialog ipSrv = new DwSnmpSelectServerDialog(); 328 329 String ipInfo[] = ipSrv.show(snmp.getIP(), snmp.getPort(), 330 snmp.getReadCommunity(), 331 snmp.getWriteCommunity()); 332 333 if (ipInfo == null) { 334 return; 335 } 336 337 snmp.setIP(ipInfo[0]); 338 snmp.setPort(Integer.parseInt(ipInfo[1])); 339 snmp.setCommunity(ipInfo[2], ipInfo[3]); 340 341 } 342 343 public void loadNewMib() { 344 345 try { 346 JFileChooser fileChooser = new JFileChooser(); 347 fileChooser.setVisible(true); 348 fileChooser.setDialogTitle("Select the MIB File to Load"); 349 fileChooser.setDialogType(JFileChooser.OPEN_DIALOG); 350 fileChooser.setCurrentDirectory(new File(".")); 351 fileChooser.setMultiSelectionEnabled(true); 352 353 String strFileName = ""; 354 int returnVal = fileChooser.showOpenDialog(null); 355 if (returnVal == JFileChooser.APPROVE_OPTION) { 356 File[] files = fileChooser.getSelectedFiles(); 357 if (files != null && files.length > 0) { 358 for (int i = 0; i < files.length; i++) { 359 try { 360 loadSingleFile(files[i]); 361 } 362 catch (Exception e) { 363 outputError("Error in loading file: " + files[i].getAbsolutePath()); 364 } 365 } 366 } 367 } 368 else { 369 return; 370 } 371 372 } 374 catch (Exception e) { 375 System.out.println("Error in getting new MIB Filenames.\n" + e.toString()); 376 } 377 378 410 } 411 412 private void loadSingleFile(File file) { 413 String strFileName = file.getAbsolutePath(); 414 treeSupport.loadNewFile(strFileName); 415 } 416 417 public static void main(String [] args) { 418 try { 419 UIManager.setLookAndFeel( 420 UIManager.getSystemLookAndFeelClassName()); 421 } 422 catch (Exception e) {} 423 424 JFrame frame = new JFrame("MIB Browser"); 426 frame.setSize(700, 550); 427 DwSnmpMibTreeGUI tree1 = new DwSnmpMibTreeGUI(); 428 Component comp = tree1.createComponents(); 429 if (comp != null) { 430 frame.getContentPane().add(comp); 431 } 432 else { 433 JOptionPane.showMessageDialog(frame.getContentPane(), 434 "Error in loading default MIBs."); 435 } 436 frame.addWindowListener(new WindowAdapter() { 438 public void windowClosing(WindowEvent e) { 439 System.exit(0); 440 } 441 }); 442 443 frame.setVisible(true); 445 } 446 447 void sendGetRequest(String strReq) { 448 if (strReq.endsWith("0")) { 449 strReq = strReq.substring(0, strReq.lastIndexOf(".")); 450 outputText("Request : Get " + strReq + "\n"); 451 } else if (strReq.endsWith("*")) { 452 strReq = strReq.substring(0, strReq.lastIndexOf("*") - 1); 453 outputText("Request : Walk " + strReq + "\n"); 454 } else if (strReq.endsWith(")")) { 455 strReq = strReq.substring(0, strReq.indexOf("(") - 1); 456 outputText("Request : Walk " + strReq + "\n"); 457 } else { 458 outputError("Error in request. Please check the OID."); 459 } 460 461 final String strReqFin=strReq; 462 Thread t=new Thread (new Runnable () { 463 public void run() { 464 snmp.snmpRequestGet(strReqFin); 465 } 466 }); 467 t.start(); 468 } 469 470 471 void outputText(String s) { 472 if (output != null) { 473 output.println(s); 474 } 475 else { 476 System.out.println(s); 477 } 478 } 479 480 void outputError(String e) { 481 if (output != null) { 482 output.printError(e); 483 } 484 else { 485 System.out.println(e); 486 487 } 488 } 489 490 private void jbInit() throws Exception { 491 } 492 } | Popular Tags |