001: package com.dwipal;
002:
003: import javax.swing.*;
004: import java.awt.*;
005: import java.awt.event.*;
006: import javax.swing.JTree;
007: import javax.swing.tree.*;
008: import javax.swing.event.*;
009: import java.io.*;
010: import java.util.*;
011:
012: public class DwSnmpMibTreeGUI implements ActionListener, MouseListener,
013: TreeSelectionListener {
014:
015: DwSnmpMibTreeBuilder treeSupport;
016: //DwSnmpOidSupport oidSupport;
017: DwSnmpMibBrowserFunctions snmp;
018: DwSnmpMibOutputHandler output = new DwSnmpMibOutputHandler();
019:
020: JTree myTree;
021: JScrollPane treeScrollPane;
022: JPanel treePane;
023:
024: JButton btnLoadMib;
025:
026: JPanel paneMain = new JPanel(new BorderLayout());
027:
028: // Other GUI stuff
029: JTextField selectedTreeOid = new JTextField("Selected oid..");
030: JTextArea resultText;
031: JButton btnGet = new JButton("Get");
032: JButton btnSet = new JButton("Set");
033: JButton btnStop = new JButton("Stop");
034: JCheckBox chkScroll = new JCheckBox("Scroll Display");
035: JButton btnOidDetails = new JButton("Details");
036: JButton btnClear = new JButton("Clear");
037:
038: // Tooltips and Toolbars
039: JToolBar mainToolbar;
040: JButton toolbarBtnIP;
041: JButton toolbarBtnAbout;
042:
043: JToolBar statusToolbar;
044: // Initial Vars
045:
046: PipedInputStream pin;
047: PipedOutputStream pout;
048: PrintStream out;
049: BufferedReader in;
050:
051: public DwSnmpMibTreeGUI() {
052: output.setLogging(true);
053: try {
054: jbInit();
055: } catch (Exception ex) {
056: ex.printStackTrace();
057: }
058: }
059:
060: public DwSnmpOidSupport getOidSupport() {
061: return treeSupport.oidSupport;
062: }
063:
064: public JPanel getMainPane() {
065: return paneMain;
066: }
067:
068: public JTree getTree() {
069: return myTree;
070: }
071:
072: public Component createComponents() {
073: // First initialise the output text area and Status areas
074: btnOidDetails.setToolTipText("Get details of selected element");
075: btnClear.setToolTipText("Clear the contents of result window");
076: btnOidDetails.addActionListener(this );
077: btnClear.addActionListener(this );
078:
079: resultText = new JTextArea();
080: JScrollPane resultPane = new JScrollPane(resultText);
081:
082: // Set everyone's output to resulttext
083: output = new DwSnmpMibOutputHandler();
084: output.setOutput(resultText);
085: output.setOutputError(resultText);
086: snmp = new DwSnmpMibBrowserFunctions();
087: snmp.setOutput(output);
088:
089: selectedTreeOid = new JTextField("Your Selection");
090:
091: // Create a tooltip for jlabel, and also add a message handler to it.
092: selectedTreeOid
093: .setToolTipText("Click here for more information on this variable");
094: selectedTreeOid.setText("Selected Element's OID");
095: selectedTreeOid.addMouseListener(this );
096:
097: // Create the TREE and Tree pane.
098:
099: outputText("Building tree..");
100: treeSupport = new DwSnmpMibTreeBuilder();
101: treeSupport.setOutput(output);
102: String projectdir = System.getProperty("ProjectDir");
103: if (projectdir == null) {
104: projectdir = ".";
105: }
106: if (treeSupport.addDirectory(projectdir + "/mibs/") == false) {
107: outputError("Directory " + projectdir
108: + "/mibs/ not found, or it is an empty directory!");
109: }
110:
111: //treeSupport.addFile("mib_core.txt");
112: // treeSupport.addFile("mib_II.txt");
113:
114: myTree = treeSupport.buildTree();
115: if (myTree == null || treeSupport.oidSupport == null) {
116: outputError("Error in loading MIB tree, quitting");
117: return null;
118: }
119: snmp.setOidSupport(treeSupport.oidSupport);
120: myTree.addTreeSelectionListener(this );
121: treeScrollPane = new JScrollPane(myTree);
122:
123: btnLoadMib = new JButton("Load MIB");
124: treePane = new JPanel(new BorderLayout());
125: treePane.add("Center", treeScrollPane);
126: treePane.add("South", btnLoadMib);
127:
128: //buildOidToNameResolutionTable(rootNode,oidResolveHash);
129:
130: statusToolbar = new JToolBar();
131: statusToolbar.add(btnClear);
132: statusToolbar.add(btnOidDetails);
133: statusToolbar.addSeparator();
134: statusToolbar.add(selectedTreeOid);
135:
136: // Create the Right pane containing buttons,status and textbox
137: btnGet.setToolTipText("Get the values for selected element");
138: btnSet.setToolTipText("Set the value of selected element");
139: btnStop.setToolTipText("Stop the current action");
140:
141: JPanel paneBtn = new JPanel(new FlowLayout());
142: btnGet.addActionListener(this );
143: btnSet.addActionListener(this );
144: btnStop.addActionListener(this );
145: chkScroll.setSelected(true);
146: chkScroll.addActionListener(new ActionListener() {
147: public void actionPerformed(ActionEvent e) {
148: output.setAutoScroll(chkScroll.isSelected());
149: }
150: });
151:
152: paneBtn.add(btnGet);
153: paneBtn.add(btnSet);
154: paneBtn.add(btnStop);
155: paneBtn.add(chkScroll);
156:
157: JPanel paneStatus = new JPanel(new BorderLayout());
158: paneStatus.add("South", paneBtn);
159: paneStatus.add("North", statusToolbar);
160: paneStatus.add("Center", resultPane);
161:
162: // Create the Main Toolbar
163: mainToolbar = new JToolBar();
164: toolbarBtnIP = new JButton("Select Server");
165: toolbarBtnAbout = new JButton("About");
166:
167: toolbarBtnIP.addActionListener(this );
168: toolbarBtnAbout.addActionListener(this );
169: btnLoadMib.addActionListener(this );
170:
171: mainToolbar.add(toolbarBtnIP);
172: mainToolbar.add(toolbarBtnAbout);
173:
174: // Create the Content pane and add other panes to it :)
175: JSplitPane paneContent = new JSplitPane();
176: paneContent.setLeftComponent(treePane);
177: paneContent.setRightComponent(paneStatus);
178: paneContent.setDividerLocation(250);
179:
180: // Finally create the Main Pane with the toolbar and content pane in it
181: paneMain.add("Center", paneContent);
182: paneMain.add("North", mainToolbar);
183:
184: return paneMain;
185: }
186:
187: /** Returns the tree pane
188: */
189: public JPanel getTreePane() {
190: return treePane;
191: }
192:
193: public void setTreePane(JPanel treePanel) {
194:
195: }
196:
197: /** TREE SELECTION LISTENER.
198: * LISTENS TO THE EVENTS IN THE TREE "myTree"
199: */
200:
201: public void valueChanged(TreeSelectionEvent e) {
202: DefaultMutableTreeNode node = (DefaultMutableTreeNode) myTree
203: .getLastSelectedPathComponent();
204: if (node == null) {
205: selectedTreeOid.setText(" ");
206: return;
207: }
208: selectedTreeOid
209: .setText(treeSupport.oidSupport.getNodeOid(node));
210: }
211:
212: /** END OF TREE SELECTION EVENT LISTENER
213: */
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:
223: private DwSnmpMibRecord getSelectedTreeNode() {
224: DwSnmpMibRecord ret = null;
225: DefaultMutableTreeNode node = (DefaultMutableTreeNode) myTree
226: .getLastSelectedPathComponent();
227: if (node != null) {
228: ret = (DwSnmpMibRecord) node.getUserObject();
229: }
230: return ret;
231: }
232:
233: public void mouseEntered(MouseEvent e) {
234: }
235:
236: public void mouseExited(MouseEvent e) {
237: }
238:
239: public void mousePressed(MouseEvent e) {
240: }
241:
242: public void mouseReleased(MouseEvent e) {
243: }
244:
245: public void actionPerformed(ActionEvent evt) {
246: Object source = evt.getSource();
247:
248: try {
249: if (source == btnGet) {
250: sendGetRequest(selectedTreeOid.getText());
251: return;
252: } else if (source == btnSet) {
253: DwSnmpMibRecord node = getSelectedTreeNode();
254: if (!node.isWritable()) {
255: JOptionPane.showMessageDialog(getMainPane(),
256: "The selected node is not writable.",
257: "Error", JOptionPane.ERROR_MESSAGE);
258: return;
259: }
260:
261: String oid = selectedTreeOid.getText();
262: String oidText = getOidSupport().resolveOidName(oid);
263: String setValue = "";
264: String message = "Enter new value for " + oidText;
265: if (node.getSyntaxID() != DwSnmpMibRecord.VALUE_TYPE_NONE) {
266: message = message + "\nValue Type: "
267: + node.syntax.trim() + " ["
268: + node.getSyntaxIDString() + "]";
269: } else {
270: message = message + "\nValue type "
271: + node.syntax.trim()
272: + " unknown, will use STRING.";
273: }
274: setValue = JOptionPane.showInputDialog(message);
275: if (setValue != null && node.checkValidValue(setValue)) {
276: outputText("Request : Set " + oid + " Value : "
277: + setValue);
278: if (snmp.processSetRequest(node, oid, setValue) == null) {
279: outputError("Error in processing variable data/set request");
280: return;
281: }
282: //DwSnmpRequestSet(oid,setValue);
283: outputText("Set command executed...");
284: outputText("Getting new value of " + oid + " ...");
285: sendGetRequest(oid);
286: }
287: return;
288: } else if (source == btnStop) {
289: snmp.destroySession();
290: outputText(" ******** Cancelled *********\n");
291: return;
292: } else if (source == btnClear) {
293: resultText.setText("");
294: return;
295: } else if (source == btnOidDetails) {
296: mouseClicked(new MouseEvent(selectedTreeOid, 0, 0, 0,
297: 0, 0, 0, true));
298: return;
299: } else if (source == toolbarBtnAbout) {
300: JOptionPane
301: .showMessageDialog(
302: paneMain,
303: "JMibBrowser version 1.1 Copyright (C) 2005 Dwipal Desai\n\n"
304: + "This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are\n"
305: + "welcome to redistribute it under certain conditions. See License.txt for details.\n\n"
306: + "This software uses snmp4j from http://www.snmp4j.org.\n\n"
307: + "Please email your suggestions to mibbrowser@dwipal.com.\n",
308: "About JMibBrowser",
309: JOptionPane.INFORMATION_MESSAGE);
310:
311: return;
312: } else if (source == toolbarBtnIP) {
313: try {
314: String newIP = new String(" ");
315: if (snmp == null) {
316: snmp = new DwSnmpMibBrowserFunctions();
317: snmp.setOidSupport(treeSupport.oidSupport);
318: }
319: getNewIPInfo();
320: } catch (Exception e) {
321: System.err.println("Error in changing IP..\n"
322: + e.toString());
323: }
324: return;
325: } else if (source == btnLoadMib) {
326: loadNewMib();
327: return;
328: }
329: } catch (Exception e) {
330: outputError("\nError in processing user request : \n"
331: + e.toString());
332: }
333: }
334:
335: void getNewIPInfo() {
336: DwSnmpSelectServerDialog ipSrv = new DwSnmpSelectServerDialog();
337:
338: String ipInfo[] = ipSrv.show(snmp.getIP(), snmp.getPort(), snmp
339: .getReadCommunity(), snmp.getWriteCommunity());
340:
341: if (ipInfo == null) {
342: return;
343: }
344:
345: snmp.setIP(ipInfo[0]);
346: snmp.setPort(Integer.parseInt(ipInfo[1]));
347: snmp.setCommunity(ipInfo[2], ipInfo[3]);
348:
349: }
350:
351: public void loadNewMib() {
352:
353: try {
354: JFileChooser fileChooser = new JFileChooser();
355: fileChooser.setVisible(true);
356: fileChooser.setDialogTitle("Select the MIB File to Load");
357: fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
358: fileChooser.setCurrentDirectory(new File("."));
359: fileChooser.setMultiSelectionEnabled(true);
360:
361: String strFileName = "";
362: int returnVal = fileChooser.showOpenDialog(null);
363: if (returnVal == JFileChooser.APPROVE_OPTION) {
364: File[] files = fileChooser.getSelectedFiles();
365: if (files != null && files.length > 0) {
366: for (int i = 0; i < files.length; i++) {
367: try {
368: loadSingleFile(files[i]);
369: } catch (Exception e) {
370: outputError("Error in loading file: "
371: + files[i].getAbsolutePath());
372: }
373: }
374: }
375: } else {
376: return;
377: }
378:
379: // treeSupport.addFile(strFileName);
380: } catch (Exception e) {
381: System.out.println("Error in getting new MIB Filenames.\n"
382: + e.toString());
383: }
384:
385: /* try {
386: myTree.removeTreeSelectionListener(this);
387: myTree.removeAll();
388: treeScrollPane.remove(myTree);
389: System.out.println("Building tree..");
390: String fileNames[]=treeSupport.getFiles();
391: treeSupport=new DwSnmpMibTreeBuilder();
392: treeSupport.setOutput(output);
393: try {
394: for(int i=0;i<fileNames.length;i++) {
395: treeSupport.addFile(fileNames[i]);
396: }
397: } catch(Exception e) {
398: System.out.println("Error in loadin new MIB Filenames.\n" + e.toString());
399: }
400:
401: try{
402: myTree=treeSupport.buildTree();
403: } catch(Exception e) {
404: System.out.println("Error in building new MIB tree.\n" + e.toString());
405: }
406:
407: myTree.addTreeSelectionListener(this);
408: treeScrollPane.setEnabled(true);
409: //treeScrollPane.add(myTree);
410: treeScrollPane.setViewportView(myTree);
411: treeScrollPane.repaint();
412: }catch(Exception e){
413: System.out.println("Error in loading new MIB.\n" + e.toString());
414: }
415:
416: */
417: }
418:
419: private void loadSingleFile(File file) {
420: String strFileName = file.getAbsolutePath();
421: treeSupport.loadNewFile(strFileName);
422: }
423:
424: public static void main(String[] args) {
425: try {
426: UIManager.setLookAndFeel(UIManager
427: .getSystemLookAndFeelClassName());
428: } catch (Exception e) {
429: }
430:
431: //Create the top-level container and add contents to it.
432: JFrame frame = new JFrame("MIB Browser");
433: frame.setSize(700, 550);
434: DwSnmpMibTreeGUI tree1 = new DwSnmpMibTreeGUI();
435: Component comp = tree1.createComponents();
436: if (comp != null) {
437: frame.getContentPane().add(comp);
438: } else {
439: JOptionPane.showMessageDialog(frame.getContentPane(),
440: "Error in loading default MIBs.");
441: }
442: //Finish setting up the frame, and show it.
443: frame.addWindowListener(new WindowAdapter() {
444: public void windowClosing(WindowEvent e) {
445: System.exit(0);
446: }
447: });
448:
449: //frame.pack();
450: frame.setVisible(true);
451: }
452:
453: void sendGetRequest(String strReq) {
454: if (strReq.endsWith("0")) {
455: strReq = strReq.substring(0, strReq.lastIndexOf("."));
456: outputText("Request : Get " + strReq + "\n");
457: } else if (strReq.endsWith("*")) {
458: strReq = strReq.substring(0, strReq.lastIndexOf("*") - 1);
459: outputText("Request : Walk " + strReq + "\n");
460: } else if (strReq.endsWith(")")) {
461: strReq = strReq.substring(0, strReq.indexOf("(") - 1);
462: outputText("Request : Walk " + strReq + "\n");
463: } else {
464: outputError("Error in request. Please check the OID.");
465: }
466:
467: final String strReqFin = strReq;
468: Thread t = new Thread(new Runnable() {
469: public void run() {
470: snmp.snmpRequestGet(strReqFin);
471: }
472: });
473: t.start();
474: }
475:
476: void outputText(String s) {
477: if (output != null) {
478: output.println(s);
479: } else {
480: System.out.println(s);
481: }
482: }
483:
484: void outputError(String e) {
485: if (output != null) {
486: output.printError(e);
487: } else {
488: System.out.println(e);
489:
490: }
491: }
492:
493: private void jbInit() throws Exception {
494: }
495: } // END OF CLASS *****************************************
|