KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > tools > example > debug > gui > MonitorTool


1 /*
2  * @(#)MonitorTool.java 1.9 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * Copyright (c) 1997-1999 by Sun Microsystems, Inc. All Rights Reserved.
9  *
10  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
11  * modify and redistribute this software in source and binary code form,
12  * provided that i) this copyright notice and license appear on all copies of
13  * the software; and ii) Licensee does not utilize the software in a manner
14  * which is disparaging to Sun.
15  *
16  * This software is provided "AS IS," without a warranty of any kind. ALL
17  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
18  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
19  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
20  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
21  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
22  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
23  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
24  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
25  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGES.
27  *
28  * This software is not designed or intended for use in on-line control of
29  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
30  * the design, construction, operation or maintenance of any nuclear
31  * facility. Licensee represents and warrants that it will not use or
32  * redistribute the Software for such purposes.
33  */

34
35 package com.sun.tools.example.debug.gui;
36
37 import java.io.*;
38 import java.util.*;
39
40 import javax.swing.*;
41 import javax.swing.tree.*;
42 import javax.swing.event.*;
43 import java.awt.*;
44 import java.awt.event.*;
45
46 import com.sun.jdi.*;
47 import com.sun.tools.example.debug.bdi.*;
48 import com.sun.tools.example.debug.expr.ExpressionParser;
49 import com.sun.tools.example.debug.expr.ParseException;
50
51 public class MonitorTool extends JPanel {
52
53     private ExecutionManager runtime;
54     private ContextManager context;
55
56     private JList list;
57
58     public MonitorTool(Environment env) {
59     super(new BorderLayout());
60     this.runtime = env.getExecutionManager();
61     this.context = env.getContextManager();
62     
63         list = new JList(env.getMonitorListModel());
64     list.setCellRenderer(new MonitorRenderer());
65
66     JScrollPane listView = new JScrollPane(list);
67     add(listView);
68
69     // Create listener.
70
MonitorToolListener listener = new MonitorToolListener();
71         list.addListSelectionListener(listener);
72         //### remove listeners on exit!
73
}
74
75     private class MonitorToolListener implements ListSelectionListener {
76         public void valueChanged(ListSelectionEvent e) {
77             int index = list.getSelectedIndex();
78             if (index != -1) {
79             }
80         }
81     }
82
83     private Value evaluate(String JavaDoc expr) throws ParseException,
84                                             InvocationException,
85                                             InvalidTypeException,
86                                             ClassNotLoadedException,
87                                             IncompatibleThreadStateException {
88         ExpressionParser.GetFrame frameGetter =
89             new ExpressionParser.GetFrame() {
90                 public StackFrame get()
91                     throws IncompatibleThreadStateException
92                 {
93                     try {
94                         return context.getCurrentFrame();
95                     } catch (VMNotInterruptedException exc) {
96                         throw new IncompatibleThreadStateException();
97                     }
98                 }
99             };
100         return ExpressionParser.evaluate(expr, runtime.vm(), frameGetter);
101     }
102
103     private class MonitorRenderer extends DefaultListCellRenderer {
104
105     public Component getListCellRendererComponent(JList list,
106                               Object JavaDoc value,
107                               int index,
108                               boolean isSelected,
109                               boolean cellHasFocus) {
110
111         //### We should indicate the current thread independently of the
112
//### selection, e.g., with an icon, because the user may change
113
//### the selection graphically without affecting the current
114
//### thread.
115

116         super.getListCellRendererComponent(list, value, index,
117                                                isSelected, cellHasFocus);
118             if (value == null) {
119                 this.setText("<unavailable>");
120             } else {
121                 String JavaDoc expr = (String JavaDoc)value;
122                 try {
123                     Value result = evaluate(expr);
124                     this.setText(expr + " = " + result);
125                 } catch (ParseException exc) {
126                     this.setText(expr + " ? " + exc.getMessage());
127                 } catch (IncompatibleThreadStateException exc) {
128                     this.setText(expr + " ...");
129                 } catch (Exception JavaDoc exc) {
130                     this.setText(expr + " ? " + exc);
131                 }
132             }
133         return this;
134     }
135     }
136 }
137
Popular Tags