annotate agent/src/share/classes/sun/jvm/hotspot/ui/DebuggerConsolePanel.java @ 196:d1605aabd0a1 jdk7-b30

6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
author xdono
date Wed, 02 Jul 2008 12:55:16 -0700
parents c70a245cad3a
children c18cbe5936b8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
196
d1605aabd0a1 6719955: Update copyright year
xdono
parents: 152
diff changeset
2 * Copyright 2000-2008 Sun Microsystems, Inc. All Rights Reserved.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
a61af66fc99e Initial load
duke
parents:
diff changeset
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
a61af66fc99e Initial load
duke
parents:
diff changeset
20 * CA 95054 USA or visit www.sun.com if you need additional information or
a61af66fc99e Initial load
duke
parents:
diff changeset
21 * have any questions.
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 package sun.jvm.hotspot.ui;
a61af66fc99e Initial load
duke
parents:
diff changeset
26
a61af66fc99e Initial load
duke
parents:
diff changeset
27 import java.awt.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
28 import java.awt.event.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
29 import javax.swing.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
30 import javax.swing.event.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
31 import javax.swing.text.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
32
a61af66fc99e Initial load
duke
parents:
diff changeset
33 import sun.jvm.hotspot.debugger.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
34 import sun.jvm.hotspot.utilities.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
35
a61af66fc99e Initial load
duke
parents:
diff changeset
36 /** A JPanel subclass containing a scrollable text area displaying the
a61af66fc99e Initial load
duke
parents:
diff changeset
37 debugger's console, if it has one. This should not be created for
a61af66fc99e Initial load
duke
parents:
diff changeset
38 a debugger which does not have a console. */
a61af66fc99e Initial load
duke
parents:
diff changeset
39
a61af66fc99e Initial load
duke
parents:
diff changeset
40 public class DebuggerConsolePanel extends JPanel {
a61af66fc99e Initial load
duke
parents:
diff changeset
41 private Debugger debugger;
a61af66fc99e Initial load
duke
parents:
diff changeset
42 private JTextComponent editor;
a61af66fc99e Initial load
duke
parents:
diff changeset
43 private boolean updating;
a61af66fc99e Initial load
duke
parents:
diff changeset
44 private int mark;
a61af66fc99e Initial load
duke
parents:
diff changeset
45 private String curText; // handles multi-line input via '\'
a61af66fc99e Initial load
duke
parents:
diff changeset
46 // Don't run the "main" method of this class unless this flag is set to true first
a61af66fc99e Initial load
duke
parents:
diff changeset
47 private static final boolean DEBUGGING = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
48
a61af66fc99e Initial load
duke
parents:
diff changeset
49 public DebuggerConsolePanel(Debugger debugger) {
a61af66fc99e Initial load
duke
parents:
diff changeset
50 this.debugger = debugger;
a61af66fc99e Initial load
duke
parents:
diff changeset
51 if (!DEBUGGING) {
a61af66fc99e Initial load
duke
parents:
diff changeset
52 if (Assert.ASSERTS_ENABLED) {
a61af66fc99e Initial load
duke
parents:
diff changeset
53 Assert.that(debugger.hasConsole(), "should not create a DebuggerConsolePanel for non-console debuggers");
a61af66fc99e Initial load
duke
parents:
diff changeset
54 }
a61af66fc99e Initial load
duke
parents:
diff changeset
55 }
a61af66fc99e Initial load
duke
parents:
diff changeset
56
a61af66fc99e Initial load
duke
parents:
diff changeset
57 setLayout(new BorderLayout());
a61af66fc99e Initial load
duke
parents:
diff changeset
58
a61af66fc99e Initial load
duke
parents:
diff changeset
59 editor = new JTextArea();
a61af66fc99e Initial load
duke
parents:
diff changeset
60 editor.setDocument(new EditableAtEndDocument());
a61af66fc99e Initial load
duke
parents:
diff changeset
61 editor.setFont(GraphicsUtilities.lookupFont("Courier"));
a61af66fc99e Initial load
duke
parents:
diff changeset
62 JScrollPane scroller = new JScrollPane();
a61af66fc99e Initial load
duke
parents:
diff changeset
63 scroller.getViewport().add(editor);
a61af66fc99e Initial load
duke
parents:
diff changeset
64 add(scroller, BorderLayout.CENTER);
a61af66fc99e Initial load
duke
parents:
diff changeset
65
a61af66fc99e Initial load
duke
parents:
diff changeset
66 editor.getDocument().addDocumentListener(new DocumentListener() {
a61af66fc99e Initial load
duke
parents:
diff changeset
67 public void changedUpdate(DocumentEvent e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
68 }
a61af66fc99e Initial load
duke
parents:
diff changeset
69
a61af66fc99e Initial load
duke
parents:
diff changeset
70 public void insertUpdate(DocumentEvent e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
71 if (updating) return;
a61af66fc99e Initial load
duke
parents:
diff changeset
72 beginUpdate();
a61af66fc99e Initial load
duke
parents:
diff changeset
73 editor.setCaretPosition(editor.getDocument().getLength());
a61af66fc99e Initial load
duke
parents:
diff changeset
74 if (insertContains(e, '\n')) {
a61af66fc99e Initial load
duke
parents:
diff changeset
75 String cmd = getMarkedText();
a61af66fc99e Initial load
duke
parents:
diff changeset
76 // Handle multi-line input
a61af66fc99e Initial load
duke
parents:
diff changeset
77 if ((cmd.length() == 0) || (cmd.charAt(cmd.length() - 1) != '\\')) {
a61af66fc99e Initial load
duke
parents:
diff changeset
78 // Trim "\\n" combinations
a61af66fc99e Initial load
duke
parents:
diff changeset
79 cmd = trimContinuations(cmd);
a61af66fc99e Initial load
duke
parents:
diff changeset
80 final String result;
a61af66fc99e Initial load
duke
parents:
diff changeset
81 if (DEBUGGING) {
a61af66fc99e Initial load
duke
parents:
diff changeset
82 System.err.println("Entered command: \"" + cmd + "\"");
a61af66fc99e Initial load
duke
parents:
diff changeset
83 result = "";
a61af66fc99e Initial load
duke
parents:
diff changeset
84 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
85 result = DebuggerConsolePanel.this.debugger.consoleExecuteCommand(cmd);
a61af66fc99e Initial load
duke
parents:
diff changeset
86 }
a61af66fc99e Initial load
duke
parents:
diff changeset
87
a61af66fc99e Initial load
duke
parents:
diff changeset
88 SwingUtilities.invokeLater(new Runnable() {
a61af66fc99e Initial load
duke
parents:
diff changeset
89 public void run() {
a61af66fc99e Initial load
duke
parents:
diff changeset
90 print(result);
a61af66fc99e Initial load
duke
parents:
diff changeset
91 printPrompt();
a61af66fc99e Initial load
duke
parents:
diff changeset
92 setMark();
a61af66fc99e Initial load
duke
parents:
diff changeset
93 endUpdate();
a61af66fc99e Initial load
duke
parents:
diff changeset
94 }
a61af66fc99e Initial load
duke
parents:
diff changeset
95 });
a61af66fc99e Initial load
duke
parents:
diff changeset
96 }
a61af66fc99e Initial load
duke
parents:
diff changeset
97 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
98 endUpdate();
a61af66fc99e Initial load
duke
parents:
diff changeset
99 }
a61af66fc99e Initial load
duke
parents:
diff changeset
100 }
a61af66fc99e Initial load
duke
parents:
diff changeset
101
a61af66fc99e Initial load
duke
parents:
diff changeset
102 public void removeUpdate(DocumentEvent e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
103 }
a61af66fc99e Initial load
duke
parents:
diff changeset
104 });
a61af66fc99e Initial load
duke
parents:
diff changeset
105
a61af66fc99e Initial load
duke
parents:
diff changeset
106 // This is a bit of a hack but is probably better than relying on
a61af66fc99e Initial load
duke
parents:
diff changeset
107 // the JEditorPane to update the caret's position precisely the
a61af66fc99e Initial load
duke
parents:
diff changeset
108 // size of the insertion
a61af66fc99e Initial load
duke
parents:
diff changeset
109 editor.addCaretListener(new CaretListener() {
a61af66fc99e Initial load
duke
parents:
diff changeset
110 public void caretUpdate(CaretEvent e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
111 int len = editor.getDocument().getLength();
a61af66fc99e Initial load
duke
parents:
diff changeset
112 if (e.getDot() > len) {
a61af66fc99e Initial load
duke
parents:
diff changeset
113 editor.setCaretPosition(len);
a61af66fc99e Initial load
duke
parents:
diff changeset
114 }
a61af66fc99e Initial load
duke
parents:
diff changeset
115 }
a61af66fc99e Initial load
duke
parents:
diff changeset
116 });
a61af66fc99e Initial load
duke
parents:
diff changeset
117
a61af66fc99e Initial load
duke
parents:
diff changeset
118 Box hbox = Box.createHorizontalBox();
a61af66fc99e Initial load
duke
parents:
diff changeset
119 hbox.add(Box.createGlue());
a61af66fc99e Initial load
duke
parents:
diff changeset
120 JButton button = new JButton("Clear Saved Text");
a61af66fc99e Initial load
duke
parents:
diff changeset
121 button.addActionListener(new ActionListener() {
a61af66fc99e Initial load
duke
parents:
diff changeset
122 public void actionPerformed(ActionEvent e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
123 clear();
a61af66fc99e Initial load
duke
parents:
diff changeset
124 }
a61af66fc99e Initial load
duke
parents:
diff changeset
125 });
a61af66fc99e Initial load
duke
parents:
diff changeset
126 hbox.add(button);
a61af66fc99e Initial load
duke
parents:
diff changeset
127 hbox.add(Box.createGlue());
a61af66fc99e Initial load
duke
parents:
diff changeset
128 add(hbox, BorderLayout.SOUTH);
a61af66fc99e Initial load
duke
parents:
diff changeset
129
a61af66fc99e Initial load
duke
parents:
diff changeset
130 clear();
a61af66fc99e Initial load
duke
parents:
diff changeset
131 }
a61af66fc99e Initial load
duke
parents:
diff changeset
132
a61af66fc99e Initial load
duke
parents:
diff changeset
133 public void requestFocus() {
a61af66fc99e Initial load
duke
parents:
diff changeset
134 editor.requestFocus();
a61af66fc99e Initial load
duke
parents:
diff changeset
135 }
a61af66fc99e Initial load
duke
parents:
diff changeset
136
a61af66fc99e Initial load
duke
parents:
diff changeset
137 public void clear() {
a61af66fc99e Initial load
duke
parents:
diff changeset
138 EditableAtEndDocument d = (EditableAtEndDocument) editor.getDocument();
a61af66fc99e Initial load
duke
parents:
diff changeset
139 d.clear();
a61af66fc99e Initial load
duke
parents:
diff changeset
140 printPrompt();
a61af66fc99e Initial load
duke
parents:
diff changeset
141 setMark();
a61af66fc99e Initial load
duke
parents:
diff changeset
142 editor.requestFocus();
a61af66fc99e Initial load
duke
parents:
diff changeset
143 }
a61af66fc99e Initial load
duke
parents:
diff changeset
144
a61af66fc99e Initial load
duke
parents:
diff changeset
145 public void setMark() {
a61af66fc99e Initial load
duke
parents:
diff changeset
146 ((EditableAtEndDocument) editor.getDocument()).setMark();
a61af66fc99e Initial load
duke
parents:
diff changeset
147 }
a61af66fc99e Initial load
duke
parents:
diff changeset
148
a61af66fc99e Initial load
duke
parents:
diff changeset
149 public String getMarkedText() {
a61af66fc99e Initial load
duke
parents:
diff changeset
150 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
151 String s = ((EditableAtEndDocument) editor.getDocument()).getMarkedText();
a61af66fc99e Initial load
duke
parents:
diff changeset
152 int i = s.length();
a61af66fc99e Initial load
duke
parents:
diff changeset
153 while ((i > 0) && (s.charAt(i - 1) == '\n')) {
a61af66fc99e Initial load
duke
parents:
diff changeset
154 i--;
a61af66fc99e Initial load
duke
parents:
diff changeset
155 }
a61af66fc99e Initial load
duke
parents:
diff changeset
156 return s.substring(0, i);
a61af66fc99e Initial load
duke
parents:
diff changeset
157 }
a61af66fc99e Initial load
duke
parents:
diff changeset
158 catch (BadLocationException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
159 e.printStackTrace();
a61af66fc99e Initial load
duke
parents:
diff changeset
160 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
161 }
a61af66fc99e Initial load
duke
parents:
diff changeset
162 }
a61af66fc99e Initial load
duke
parents:
diff changeset
163
a61af66fc99e Initial load
duke
parents:
diff changeset
164 //--------------------------------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
165 // Internals only below this point
a61af66fc99e Initial load
duke
parents:
diff changeset
166 //
a61af66fc99e Initial load
duke
parents:
diff changeset
167
a61af66fc99e Initial load
duke
parents:
diff changeset
168 private void beginUpdate() {
a61af66fc99e Initial load
duke
parents:
diff changeset
169 updating = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
170 }
a61af66fc99e Initial load
duke
parents:
diff changeset
171
a61af66fc99e Initial load
duke
parents:
diff changeset
172 private void endUpdate() {
a61af66fc99e Initial load
duke
parents:
diff changeset
173 updating = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
174 }
a61af66fc99e Initial load
duke
parents:
diff changeset
175
a61af66fc99e Initial load
duke
parents:
diff changeset
176 private void print(String s) {
a61af66fc99e Initial load
duke
parents:
diff changeset
177 Document d = editor.getDocument();
a61af66fc99e Initial load
duke
parents:
diff changeset
178 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
179 d.insertString(d.getLength(), s, null);
a61af66fc99e Initial load
duke
parents:
diff changeset
180 }
a61af66fc99e Initial load
duke
parents:
diff changeset
181 catch (BadLocationException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
182 e.printStackTrace();
a61af66fc99e Initial load
duke
parents:
diff changeset
183 }
a61af66fc99e Initial load
duke
parents:
diff changeset
184 }
a61af66fc99e Initial load
duke
parents:
diff changeset
185
a61af66fc99e Initial load
duke
parents:
diff changeset
186 private void printPrompt() {
a61af66fc99e Initial load
duke
parents:
diff changeset
187 if (DEBUGGING) {
a61af66fc99e Initial load
duke
parents:
diff changeset
188 print("foo> ");
a61af66fc99e Initial load
duke
parents:
diff changeset
189 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
190 print(debugger.getConsolePrompt());
a61af66fc99e Initial load
duke
parents:
diff changeset
191 }
a61af66fc99e Initial load
duke
parents:
diff changeset
192 }
a61af66fc99e Initial load
duke
parents:
diff changeset
193
a61af66fc99e Initial load
duke
parents:
diff changeset
194 private boolean insertContains(DocumentEvent e, char c) {
a61af66fc99e Initial load
duke
parents:
diff changeset
195 String s = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
196 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
197 s = editor.getText(e.getOffset(), e.getLength());
a61af66fc99e Initial load
duke
parents:
diff changeset
198 for (int i = 0; i < e.getLength(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
199 if (s.charAt(i) == c) {
a61af66fc99e Initial load
duke
parents:
diff changeset
200 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
201 }
a61af66fc99e Initial load
duke
parents:
diff changeset
202 }
a61af66fc99e Initial load
duke
parents:
diff changeset
203 }
a61af66fc99e Initial load
duke
parents:
diff changeset
204 catch (BadLocationException ex) {
a61af66fc99e Initial load
duke
parents:
diff changeset
205 ex.printStackTrace();
a61af66fc99e Initial load
duke
parents:
diff changeset
206 }
a61af66fc99e Initial load
duke
parents:
diff changeset
207 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
208 }
a61af66fc99e Initial load
duke
parents:
diff changeset
209
a61af66fc99e Initial load
duke
parents:
diff changeset
210 private String trimContinuations(String text) {
a61af66fc99e Initial load
duke
parents:
diff changeset
211 int i;
a61af66fc99e Initial load
duke
parents:
diff changeset
212 while ((i = text.indexOf("\\\n")) >= 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
213 text = text.substring(0, i) + text.substring(i+2, text.length());
a61af66fc99e Initial load
duke
parents:
diff changeset
214 }
a61af66fc99e Initial load
duke
parents:
diff changeset
215 return text;
a61af66fc99e Initial load
duke
parents:
diff changeset
216 }
a61af66fc99e Initial load
duke
parents:
diff changeset
217
a61af66fc99e Initial load
duke
parents:
diff changeset
218 public static void main(String[] args) {
a61af66fc99e Initial load
duke
parents:
diff changeset
219 JFrame frame = new JFrame();
a61af66fc99e Initial load
duke
parents:
diff changeset
220 frame.getContentPane().setLayout(new BorderLayout());
a61af66fc99e Initial load
duke
parents:
diff changeset
221 DebuggerConsolePanel panel = new DebuggerConsolePanel(null);
a61af66fc99e Initial load
duke
parents:
diff changeset
222 frame.getContentPane().add(panel, BorderLayout.CENTER);
a61af66fc99e Initial load
duke
parents:
diff changeset
223 frame.addWindowListener(new WindowAdapter() {
a61af66fc99e Initial load
duke
parents:
diff changeset
224 public void windowClosing(WindowEvent e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
225 System.exit(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
226 }
a61af66fc99e Initial load
duke
parents:
diff changeset
227 });
a61af66fc99e Initial load
duke
parents:
diff changeset
228 frame.setSize(500, 500);
152
c70a245cad3a 6670684: 4/5 SA command universe did not print out CMS space information
dcubed
parents: 0
diff changeset
229 frame.setVisible(true);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
230 panel.requestFocus();
a61af66fc99e Initial load
duke
parents:
diff changeset
231 }
a61af66fc99e Initial load
duke
parents:
diff changeset
232 }