1 /*
   2  * Copyright (c) 1998, 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 java.awt.*;
  39 import java.awt.event.*;
  40 import java.util.Vector;
  41 import java.util.List;
  42 
  43 import com.sun.tools.example.debug.bdi.*;
  44 
  45 //### This is currently just a placeholder!
  46 
  47 class JDBMenuBar extends JMenuBar {
  48 
  49     Environment env;
  50 
  51     ExecutionManager runtime;
  52     ClassManager classManager;
  53     SourceManager sourceManager;
  54 
  55     CommandInterpreter interpreter;
  56 
  57     JDBMenuBar(Environment env) {
  58         this.env = env;
  59         this.runtime = env.getExecutionManager();
  60         this.classManager = env.getClassManager();
  61         this.sourceManager = env.getSourceManager();
  62         this.interpreter = new CommandInterpreter(env, true);
  63 
  64         JMenu fileMenu = new JMenu("File");
  65 
  66         JMenuItem openItem = new JMenuItem("Open...", 'O');
  67         openItem.addActionListener(new ActionListener() {
  68             @Override
  69             public void actionPerformed(ActionEvent e) {
  70                 openCommand();
  71             }
  72         });
  73         fileMenu.add(openItem);
  74         addTool(fileMenu, "Exit debugger", "Exit", "exit");
  75 
  76         JMenu cmdMenu = new JMenu("Commands");
  77 
  78         addTool(cmdMenu, "Step into next line", "Step", "step");
  79         addTool(cmdMenu, "Step over next line", "Next", "next");
  80         cmdMenu.addSeparator();
  81 
  82         addTool(cmdMenu, "Step into next instruction",
  83                 "Step Instruction", "stepi");
  84         addTool(cmdMenu, "Step over next instruction",
  85                 "Next Instruction", "nexti");
  86         cmdMenu.addSeparator();
  87 
  88         addTool(cmdMenu, "Step out of current method call",
  89                 "Step Up", "step up");
  90         cmdMenu.addSeparator();
  91 
  92         addTool(cmdMenu, "Suspend execution", "Interrupt", "interrupt");
  93         addTool(cmdMenu, "Continue execution", "Continue", "cont");
  94         cmdMenu.addSeparator();
  95 
  96         addTool(cmdMenu, "Display current stack", "Where", "where");
  97         cmdMenu.addSeparator();
  98 
  99         addTool(cmdMenu, "Move up one stack frame", "Up", "up");
 100         addTool(cmdMenu, "Move down one stack frame", "Down", "down");
 101         cmdMenu.addSeparator();
 102 
 103         JMenuItem monitorItem = new JMenuItem("Monitor Expression...", 'M');
 104         monitorItem.addActionListener(new ActionListener() {
 105             @Override
 106             public void actionPerformed(ActionEvent e) {
 107                 monitorCommand();
 108             }
 109         });
 110         cmdMenu.add(monitorItem);
 111 
 112         JMenuItem unmonitorItem = new JMenuItem("Unmonitor Expression...");
 113         unmonitorItem.addActionListener(new ActionListener() {
 114             @Override
 115             public void actionPerformed(ActionEvent e) {
 116                 unmonitorCommand();
 117             }
 118         });
 119         cmdMenu.add(unmonitorItem);
 120 
 121         JMenu breakpointMenu = new JMenu("Breakpoint");
 122         JMenuItem stopItem = new JMenuItem("Stop in...", 'S');
 123         stopItem.addActionListener(new ActionListener() {
 124             @Override
 125             public void actionPerformed(ActionEvent e) {
 126                 buildBreakpoint();
 127             }
 128         });
 129         breakpointMenu.add(stopItem);
 130 
 131         JMenu helpMenu = new JMenu("Help");
 132         addTool(helpMenu, "Display command list", "Help", "help");
 133 
 134         this.add(fileMenu);
 135         this.add(cmdMenu);
 136 //      this.add(breakpointMenu);
 137         this.add(helpMenu);
 138     }
 139 
 140     private void buildBreakpoint() {
 141         Frame frame = JOptionPane.getRootFrame();
 142         JDialog dialog = new JDialog(frame, "Specify Breakpoint");
 143         Container contents = dialog.getContentPane();
 144         Vector<String> classes = new Vector<String>();
 145         classes.add("Foo");
 146         classes.add("Bar");
 147         JList list = new JList(classes);
 148         JScrollPane scrollPane = new JScrollPane(list);
 149         contents.add(scrollPane);
 150         dialog.show();
 151 
 152     }
 153 
 154     private void monitorCommand() {
 155         String expr = (String)JOptionPane.showInputDialog(null,
 156                            "Expression to monitor:", "Add Monitor",
 157                            JOptionPane.QUESTION_MESSAGE, null, null, null);
 158         if (expr != null) {
 159             interpreter.executeCommand("monitor " + expr);
 160         }
 161     }
 162 
 163     private void unmonitorCommand() {
 164         List monitors = env.getMonitorListModel().monitors();
 165         String expr = (String)JOptionPane.showInputDialog(null,
 166                            "Expression to unmonitor:", "Remove Monitor",
 167                            JOptionPane.QUESTION_MESSAGE, null,
 168                            monitors.toArray(),
 169                            monitors.get(monitors.size()-1));
 170         if (expr != null) {
 171             interpreter.executeCommand("unmonitor " + expr);
 172         }
 173     }
 174 
 175     private void openCommand() {
 176         JFileChooser chooser = new JFileChooser();
 177         JDBFileFilter filter = new JDBFileFilter("java", "Java source code");
 178         chooser.setFileFilter(filter);
 179         int result = chooser.showOpenDialog(this);
 180         if (result == JFileChooser.APPROVE_OPTION) {
 181             System.out.println("Chose file: " + chooser.getSelectedFile().getName());
 182         }
 183     }
 184 
 185     private void addTool(JMenu menu, String toolTip, String labelText,
 186                          String command) {
 187         JMenuItem mi = new JMenuItem(labelText);
 188         mi.setToolTipText(toolTip);
 189         final String cmd = command;
 190         mi.addActionListener(new ActionListener() {
 191             @Override
 192             public void actionPerformed(ActionEvent e) {
 193                 interpreter.executeCommand(cmd);
 194             }
 195         });
 196         menu.add(mi);
 197     }
 198 
 199 }