Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/ui/DebuggerConsolePanel.java
41161 views
/*1* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324package sun.jvm.hotspot.ui;2526import java.awt.*;27import java.awt.event.*;28import javax.swing.*;29import javax.swing.event.*;30import javax.swing.text.*;3132import sun.jvm.hotspot.debugger.*;33import sun.jvm.hotspot.utilities.*;3435/** A JPanel subclass containing a scrollable text area displaying the36debugger's console, if it has one. This should not be created for37a debugger which does not have a console. */3839public class DebuggerConsolePanel extends JPanel {40private Debugger debugger;41private JTextComponent editor;42private boolean updating;43private int mark;44private String curText; // handles multi-line input via '\'45// Don't run the "main" method of this class unless this flag is set to true first46private static final boolean DEBUGGING = false;4748public DebuggerConsolePanel(Debugger debugger) {49this.debugger = debugger;50if (!DEBUGGING) {51if (Assert.ASSERTS_ENABLED) {52Assert.that(debugger.hasConsole(), "should not create a DebuggerConsolePanel for non-console debuggers");53}54}5556setLayout(new BorderLayout());5758editor = new JTextArea();59editor.setDocument(new EditableAtEndDocument());60editor.setFont(GraphicsUtilities.getMonospacedFont());61JScrollPane scroller = new JScrollPane();62scroller.getViewport().add(editor);63add(scroller, BorderLayout.CENTER);6465editor.getDocument().addDocumentListener(new DocumentListener() {66public void changedUpdate(DocumentEvent e) {67}6869public void insertUpdate(DocumentEvent e) {70if (updating) return;71beginUpdate();72editor.setCaretPosition(editor.getDocument().getLength());73if (insertContains(e, '\n')) {74String cmd = getMarkedText();75// Handle multi-line input76if ((cmd.length() == 0) || (cmd.charAt(cmd.length() - 1) != '\\')) {77// Trim "\\n" combinations78cmd = trimContinuations(cmd);79final String result;80if (DEBUGGING) {81System.err.println("Entered command: \"" + cmd + "\"");82result = "";83} else {84result = DebuggerConsolePanel.this.debugger.consoleExecuteCommand(cmd);85}8687SwingUtilities.invokeLater(new Runnable() {88public void run() {89print(result);90printPrompt();91setMark();92endUpdate();93}94});95}96} else {97endUpdate();98}99}100101public void removeUpdate(DocumentEvent e) {102}103});104105// This is a bit of a hack but is probably better than relying on106// the JEditorPane to update the caret's position precisely the107// size of the insertion108editor.addCaretListener(new CaretListener() {109public void caretUpdate(CaretEvent e) {110int len = editor.getDocument().getLength();111if (e.getDot() > len) {112editor.setCaretPosition(len);113}114}115});116117Box hbox = Box.createHorizontalBox();118hbox.add(Box.createGlue());119JButton button = new JButton("Clear Saved Text");120button.addActionListener(new ActionListener() {121public void actionPerformed(ActionEvent e) {122clear();123}124});125hbox.add(button);126hbox.add(Box.createGlue());127add(hbox, BorderLayout.SOUTH);128129clear();130}131132public void requestFocus() {133editor.requestFocus();134}135136public void clear() {137EditableAtEndDocument d = (EditableAtEndDocument) editor.getDocument();138d.clear();139printPrompt();140setMark();141editor.requestFocus();142}143144public void setMark() {145((EditableAtEndDocument) editor.getDocument()).setMark();146}147148public String getMarkedText() {149try {150String s = ((EditableAtEndDocument) editor.getDocument()).getMarkedText();151int i = s.length();152while ((i > 0) && (s.charAt(i - 1) == '\n')) {153i--;154}155return s.substring(0, i);156}157catch (BadLocationException e) {158e.printStackTrace();159return null;160}161}162163//--------------------------------------------------------------------------------164// Internals only below this point165//166167private void beginUpdate() {168updating = true;169}170171private void endUpdate() {172updating = false;173}174175private void print(String s) {176Document d = editor.getDocument();177try {178d.insertString(d.getLength(), s, null);179}180catch (BadLocationException e) {181e.printStackTrace();182}183}184185private void printPrompt() {186if (DEBUGGING) {187print("foo> ");188} else {189print(debugger.getConsolePrompt());190}191}192193private boolean insertContains(DocumentEvent e, char c) {194String s = null;195try {196s = editor.getText(e.getOffset(), e.getLength());197for (int i = 0; i < e.getLength(); i++) {198if (s.charAt(i) == c) {199return true;200}201}202}203catch (BadLocationException ex) {204ex.printStackTrace();205}206return false;207}208209private String trimContinuations(String text) {210int i;211while ((i = text.indexOf("\\\n")) >= 0) {212text = text.substring(0, i) + text.substring(i+2, text.length());213}214return text;215}216217public static void main(String[] args) {218JFrame frame = new JFrame();219frame.getContentPane().setLayout(new BorderLayout());220DebuggerConsolePanel panel = new DebuggerConsolePanel(null);221frame.getContentPane().add(panel, BorderLayout.CENTER);222frame.addWindowListener(new WindowAdapter() {223public void windowClosing(WindowEvent e) {224System.exit(0);225}226});227frame.setSize(500, 500);228frame.setVisible(true);229panel.requestFocus();230}231}232233234