1 /*
   2  * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * This source code is provided to illustrate the usage of a given feature
  28  * or technique and has been deliberately simplified. Additional steps
  29  * required for a production-quality application, such as security checks,
  30  * input validation and proper error handling, might not be present in
  31  * this sample code.
  32  */
  33 
  34 
  35 package com.sun.tools.example.debug.gui;
  36 
  37 import javax.swing.*;
  38 import javax.swing.event.*;
  39 import java.awt.*;
  40 import com.sun.jdi.*;
  41 import com.sun.tools.example.debug.bdi.*;
  42 import com.sun.tools.example.debug.expr.ExpressionParser;
  43 import com.sun.tools.example.debug.expr.ParseException;
  44 
  45 public class MonitorTool extends JPanel {
  46 
  47     private static final long serialVersionUID = -645235951031726647L;
  48     private ExecutionManager runtime;
  49     private ContextManager context;
  50 
  51     private JList list;
  52 
  53     public MonitorTool(Environment env) {
  54         super(new BorderLayout());
  55         this.runtime = env.getExecutionManager();
  56         this.context = env.getContextManager();
  57 
  58         list = new JList(env.getMonitorListModel());
  59         list.setCellRenderer(new MonitorRenderer());
  60 
  61         JScrollPane listView = new JScrollPane(list);
  62         add(listView);
  63 
  64         // Create listener.
  65         MonitorToolListener listener = new MonitorToolListener();
  66         list.addListSelectionListener(listener);
  67         //### remove listeners on exit!
  68     }
  69 
  70     private class MonitorToolListener implements ListSelectionListener {
  71         @Override
  72         public void valueChanged(ListSelectionEvent e) {
  73             int index = list.getSelectedIndex();
  74             if (index != -1) {
  75             }
  76         }
  77     }
  78 
  79     private Value evaluate(String expr) throws ParseException,
  80                                             InvocationException,
  81                                             InvalidTypeException,
  82                                             ClassNotLoadedException,
  83                                             IncompatibleThreadStateException {
  84         ExpressionParser.GetFrame frameGetter =
  85             new ExpressionParser.GetFrame() {
  86                 @Override
  87                 public StackFrame get()
  88                     throws IncompatibleThreadStateException
  89                 {
  90                     try {
  91                         return context.getCurrentFrame();
  92                     } catch (VMNotInterruptedException exc) {
  93                         throw new IncompatibleThreadStateException();
  94                     }
  95                 }
  96             };
  97         return ExpressionParser.evaluate(expr, runtime.vm(), frameGetter);
  98     }
  99 
 100     private class MonitorRenderer extends DefaultListCellRenderer {
 101 
 102         @Override
 103         public Component getListCellRendererComponent(JList list,
 104                                                       Object value,
 105                                                       int index,
 106                                                       boolean isSelected,
 107                                                       boolean cellHasFocus) {
 108 
 109             //### We should indicate the current thread independently of the
 110             //### selection, e.g., with an icon, because the user may change
 111             //### the selection graphically without affecting the current
 112             //### thread.
 113 
 114             super.getListCellRendererComponent(list, value, index,
 115                                                isSelected, cellHasFocus);
 116             if (value == null) {
 117                 this.setText("<unavailable>");
 118             } else {
 119                 String expr = (String)value;
 120                 try {
 121                     Value result = evaluate(expr);
 122                     this.setText(expr + " = " + result);
 123                 } catch (ParseException exc) {
 124                     this.setText(expr + " ? " + exc.getMessage());
 125                 } catch (IncompatibleThreadStateException exc) {
 126                     this.setText(expr + " ...");
 127                 } catch (Exception exc) {
 128                     this.setText(expr + " ? " + exc);
 129                 }
 130             }
 131             return this;
 132         }
 133     }
 134 }