annotate agent/src/share/classes/sun/jvm/hotspot/ui/CommandProcessorPanel.java @ 152:c70a245cad3a

6670684: 4/5 SA command universe did not print out CMS space information Summary: Forward port of Yumin's fix for 6670684 from HSX-11; Yumin verified the port was correct. Reviewed-by: dcubed
author dcubed
date Fri, 09 May 2008 08:55:13 -0700
parents a61af66fc99e
children d1605aabd0a1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
2 * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
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.io.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
28 import java.awt.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
29 import java.awt.event.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
30 import javax.swing.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
31 import javax.swing.event.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
32 import javax.swing.text.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
33
a61af66fc99e Initial load
duke
parents:
diff changeset
34 import sun.jvm.hotspot.CommandProcessor;
a61af66fc99e Initial load
duke
parents:
diff changeset
35 import sun.jvm.hotspot.debugger.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
36 import sun.jvm.hotspot.utilities.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
37
a61af66fc99e Initial load
duke
parents:
diff changeset
38 /** A JPanel subclass containing a scrollable text area displaying the
a61af66fc99e Initial load
duke
parents:
diff changeset
39 debugger's console, if it has one. This should not be created for
a61af66fc99e Initial load
duke
parents:
diff changeset
40 a debugger which does not have a console. */
a61af66fc99e Initial load
duke
parents:
diff changeset
41
a61af66fc99e Initial load
duke
parents:
diff changeset
42 public class CommandProcessorPanel extends JPanel {
a61af66fc99e Initial load
duke
parents:
diff changeset
43 private CommandProcessor commands;
a61af66fc99e Initial load
duke
parents:
diff changeset
44 private JTextArea editor;
a61af66fc99e Initial load
duke
parents:
diff changeset
45 private boolean updating;
a61af66fc99e Initial load
duke
parents:
diff changeset
46 private int mark;
a61af66fc99e Initial load
duke
parents:
diff changeset
47 private String curText; // handles multi-line input via '\'
a61af66fc99e Initial load
duke
parents:
diff changeset
48
a61af66fc99e Initial load
duke
parents:
diff changeset
49 // Don't run the "main" method of this class unless this flag is set to true first
a61af66fc99e Initial load
duke
parents:
diff changeset
50 private static final boolean DEBUGGING = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
51
a61af66fc99e Initial load
duke
parents:
diff changeset
52 ByteArrayOutputStream baos = new ByteArrayOutputStream(10240);
a61af66fc99e Initial load
duke
parents:
diff changeset
53
a61af66fc99e Initial load
duke
parents:
diff changeset
54
a61af66fc99e Initial load
duke
parents:
diff changeset
55 public CommandProcessorPanel(CommandProcessor cp) {
a61af66fc99e Initial load
duke
parents:
diff changeset
56
a61af66fc99e Initial load
duke
parents:
diff changeset
57 commands = cp;
a61af66fc99e Initial load
duke
parents:
diff changeset
58
a61af66fc99e Initial load
duke
parents:
diff changeset
59 setLayout(new BorderLayout());
a61af66fc99e Initial load
duke
parents:
diff changeset
60
a61af66fc99e Initial load
duke
parents:
diff changeset
61 editor = new JTextArea();
a61af66fc99e Initial load
duke
parents:
diff changeset
62 editor.setDocument(new EditableAtEndDocument());
a61af66fc99e Initial load
duke
parents:
diff changeset
63 editor.setFont(GraphicsUtilities.lookupFont("Courier"));
a61af66fc99e Initial load
duke
parents:
diff changeset
64 JScrollPane scroller = new JScrollPane();
a61af66fc99e Initial load
duke
parents:
diff changeset
65 scroller.getViewport().add(editor);
a61af66fc99e Initial load
duke
parents:
diff changeset
66 add(scroller, BorderLayout.CENTER);
a61af66fc99e Initial load
duke
parents:
diff changeset
67
a61af66fc99e Initial load
duke
parents:
diff changeset
68 // Set up out
a61af66fc99e Initial load
duke
parents:
diff changeset
69 PrintStream o = new PrintStream(baos, true);
a61af66fc99e Initial load
duke
parents:
diff changeset
70 cp.setOutput(o);
a61af66fc99e Initial load
duke
parents:
diff changeset
71 cp.setErr(o);
a61af66fc99e Initial load
duke
parents:
diff changeset
72
a61af66fc99e Initial load
duke
parents:
diff changeset
73 editor.getDocument().addDocumentListener(new DocumentListener() {
a61af66fc99e Initial load
duke
parents:
diff changeset
74 public void changedUpdate(DocumentEvent e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
75 }
a61af66fc99e Initial load
duke
parents:
diff changeset
76
a61af66fc99e Initial load
duke
parents:
diff changeset
77 public void insertUpdate(DocumentEvent e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
78 if (updating) return;
a61af66fc99e Initial load
duke
parents:
diff changeset
79 beginUpdate();
a61af66fc99e Initial load
duke
parents:
diff changeset
80 editor.setCaretPosition(editor.getDocument().getLength());
a61af66fc99e Initial load
duke
parents:
diff changeset
81 if (insertContains(e, '\n')) {
a61af66fc99e Initial load
duke
parents:
diff changeset
82 String cmd = getMarkedText();
a61af66fc99e Initial load
duke
parents:
diff changeset
83 // Handle multi-line input
a61af66fc99e Initial load
duke
parents:
diff changeset
84 if ((cmd.length() == 0) || (cmd.charAt(cmd.length() - 1) != '\\')) {
a61af66fc99e Initial load
duke
parents:
diff changeset
85 // Trim "\\n" combinations
a61af66fc99e Initial load
duke
parents:
diff changeset
86 final String ln = trimContinuations(cmd);
a61af66fc99e Initial load
duke
parents:
diff changeset
87 SwingUtilities.invokeLater(new Runnable() {
a61af66fc99e Initial load
duke
parents:
diff changeset
88 public void run() {
a61af66fc99e Initial load
duke
parents:
diff changeset
89 beginUpdate();
a61af66fc99e Initial load
duke
parents:
diff changeset
90 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
91 commands.executeCommand(ln);
a61af66fc99e Initial load
duke
parents:
diff changeset
92 commands.printPrompt();
a61af66fc99e Initial load
duke
parents:
diff changeset
93 Document d = editor.getDocument();
a61af66fc99e Initial load
duke
parents:
diff changeset
94 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
95 d.insertString(d.getLength(), baos.toString(), null);
a61af66fc99e Initial load
duke
parents:
diff changeset
96 }
a61af66fc99e Initial load
duke
parents:
diff changeset
97 catch (BadLocationException ble) {
a61af66fc99e Initial load
duke
parents:
diff changeset
98 ble.printStackTrace();
a61af66fc99e Initial load
duke
parents:
diff changeset
99 }
a61af66fc99e Initial load
duke
parents:
diff changeset
100 baos.reset();
a61af66fc99e Initial load
duke
parents:
diff changeset
101 editor.setCaretPosition(editor.getDocument().getLength());
a61af66fc99e Initial load
duke
parents:
diff changeset
102 setMark();
a61af66fc99e Initial load
duke
parents:
diff changeset
103 } finally {
a61af66fc99e Initial load
duke
parents:
diff changeset
104 endUpdate();
a61af66fc99e Initial load
duke
parents:
diff changeset
105 }
a61af66fc99e Initial load
duke
parents:
diff changeset
106 }
a61af66fc99e Initial load
duke
parents:
diff changeset
107 });
a61af66fc99e Initial load
duke
parents:
diff changeset
108 }
a61af66fc99e Initial load
duke
parents:
diff changeset
109 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
110 endUpdate();
a61af66fc99e Initial load
duke
parents:
diff changeset
111 }
a61af66fc99e Initial load
duke
parents:
diff changeset
112 }
a61af66fc99e Initial load
duke
parents:
diff changeset
113
a61af66fc99e Initial load
duke
parents:
diff changeset
114 public void removeUpdate(DocumentEvent e) {
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 // This is a bit of a hack but is probably better than relying on
a61af66fc99e Initial load
duke
parents:
diff changeset
119 // the JEditorPane to update the caret's position precisely the
a61af66fc99e Initial load
duke
parents:
diff changeset
120 // size of the insertion
a61af66fc99e Initial load
duke
parents:
diff changeset
121 editor.addCaretListener(new CaretListener() {
a61af66fc99e Initial load
duke
parents:
diff changeset
122 public void caretUpdate(CaretEvent e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
123 int len = editor.getDocument().getLength();
a61af66fc99e Initial load
duke
parents:
diff changeset
124 if (e.getDot() > len) {
a61af66fc99e Initial load
duke
parents:
diff changeset
125 editor.setCaretPosition(len);
a61af66fc99e Initial load
duke
parents:
diff changeset
126 }
a61af66fc99e Initial load
duke
parents:
diff changeset
127 }
a61af66fc99e Initial load
duke
parents:
diff changeset
128 });
a61af66fc99e Initial load
duke
parents:
diff changeset
129
a61af66fc99e Initial load
duke
parents:
diff changeset
130 Box hbox = Box.createHorizontalBox();
a61af66fc99e Initial load
duke
parents:
diff changeset
131 hbox.add(Box.createGlue());
a61af66fc99e Initial load
duke
parents:
diff changeset
132 JButton button = new JButton("Clear Saved Text");
a61af66fc99e Initial load
duke
parents:
diff changeset
133 button.addActionListener(new ActionListener() {
a61af66fc99e Initial load
duke
parents:
diff changeset
134 public void actionPerformed(ActionEvent e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
135 clear();
a61af66fc99e Initial load
duke
parents:
diff changeset
136 }
a61af66fc99e Initial load
duke
parents:
diff changeset
137 });
a61af66fc99e Initial load
duke
parents:
diff changeset
138 hbox.add(button);
a61af66fc99e Initial load
duke
parents:
diff changeset
139 hbox.add(Box.createGlue());
a61af66fc99e Initial load
duke
parents:
diff changeset
140 add(hbox, BorderLayout.SOUTH);
a61af66fc99e Initial load
duke
parents:
diff changeset
141
a61af66fc99e Initial load
duke
parents:
diff changeset
142 clear();
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 requestFocus() {
a61af66fc99e Initial load
duke
parents:
diff changeset
146 editor.requestFocus();
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 void clear() {
a61af66fc99e Initial load
duke
parents:
diff changeset
150 EditableAtEndDocument d = (EditableAtEndDocument) editor.getDocument();
a61af66fc99e Initial load
duke
parents:
diff changeset
151 d.clear();
a61af66fc99e Initial load
duke
parents:
diff changeset
152 commands.executeCommand("");
a61af66fc99e Initial load
duke
parents:
diff changeset
153 setMark();
a61af66fc99e Initial load
duke
parents:
diff changeset
154 editor.requestFocus();
a61af66fc99e Initial load
duke
parents:
diff changeset
155 }
a61af66fc99e Initial load
duke
parents:
diff changeset
156
a61af66fc99e Initial load
duke
parents:
diff changeset
157 public void setMark() {
a61af66fc99e Initial load
duke
parents:
diff changeset
158 ((EditableAtEndDocument) editor.getDocument()).setMark();
a61af66fc99e Initial load
duke
parents:
diff changeset
159 }
a61af66fc99e Initial load
duke
parents:
diff changeset
160
a61af66fc99e Initial load
duke
parents:
diff changeset
161 public String getMarkedText() {
a61af66fc99e Initial load
duke
parents:
diff changeset
162 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
163 String s = ((EditableAtEndDocument) editor.getDocument()).getMarkedText();
a61af66fc99e Initial load
duke
parents:
diff changeset
164 int i = s.length();
a61af66fc99e Initial load
duke
parents:
diff changeset
165 while ((i > 0) && (s.charAt(i - 1) == '\n')) {
a61af66fc99e Initial load
duke
parents:
diff changeset
166 i--;
a61af66fc99e Initial load
duke
parents:
diff changeset
167 }
a61af66fc99e Initial load
duke
parents:
diff changeset
168 return s.substring(0, i);
a61af66fc99e Initial load
duke
parents:
diff changeset
169 }
a61af66fc99e Initial load
duke
parents:
diff changeset
170 catch (BadLocationException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
171 e.printStackTrace();
a61af66fc99e Initial load
duke
parents:
diff changeset
172 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
173 }
a61af66fc99e Initial load
duke
parents:
diff changeset
174 }
a61af66fc99e Initial load
duke
parents:
diff changeset
175
a61af66fc99e Initial load
duke
parents:
diff changeset
176 //--------------------------------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
177 // Internals only below this point
a61af66fc99e Initial load
duke
parents:
diff changeset
178 //
a61af66fc99e Initial load
duke
parents:
diff changeset
179
a61af66fc99e Initial load
duke
parents:
diff changeset
180 private void beginUpdate() {
a61af66fc99e Initial load
duke
parents:
diff changeset
181 updating = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
182 }
a61af66fc99e Initial load
duke
parents:
diff changeset
183
a61af66fc99e Initial load
duke
parents:
diff changeset
184 private void endUpdate() {
a61af66fc99e Initial load
duke
parents:
diff changeset
185 updating = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
186 }
a61af66fc99e Initial load
duke
parents:
diff changeset
187
a61af66fc99e Initial load
duke
parents:
diff changeset
188 private boolean insertContains(DocumentEvent e, char c) {
a61af66fc99e Initial load
duke
parents:
diff changeset
189 String s = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
190 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
191 s = editor.getText(e.getOffset(), e.getLength());
a61af66fc99e Initial load
duke
parents:
diff changeset
192 for (int i = 0; i < e.getLength(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
193 if (s.charAt(i) == c) {
a61af66fc99e Initial load
duke
parents:
diff changeset
194 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
195 }
a61af66fc99e Initial load
duke
parents:
diff changeset
196 }
a61af66fc99e Initial load
duke
parents:
diff changeset
197 }
a61af66fc99e Initial load
duke
parents:
diff changeset
198 catch (BadLocationException ex) {
a61af66fc99e Initial load
duke
parents:
diff changeset
199 ex.printStackTrace();
a61af66fc99e Initial load
duke
parents:
diff changeset
200 }
a61af66fc99e Initial load
duke
parents:
diff changeset
201 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
202 }
a61af66fc99e Initial load
duke
parents:
diff changeset
203
a61af66fc99e Initial load
duke
parents:
diff changeset
204 private String trimContinuations(String text) {
a61af66fc99e Initial load
duke
parents:
diff changeset
205 int i;
a61af66fc99e Initial load
duke
parents:
diff changeset
206 while ((i = text.indexOf("\\\n")) >= 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
207 text = text.substring(0, i) + text.substring(i+2, text.length());
a61af66fc99e Initial load
duke
parents:
diff changeset
208 }
a61af66fc99e Initial load
duke
parents:
diff changeset
209 return text;
a61af66fc99e Initial load
duke
parents:
diff changeset
210 }
a61af66fc99e Initial load
duke
parents:
diff changeset
211
a61af66fc99e Initial load
duke
parents:
diff changeset
212 public static void main(String[] args) {
a61af66fc99e Initial load
duke
parents:
diff changeset
213 JFrame frame = new JFrame();
a61af66fc99e Initial load
duke
parents:
diff changeset
214 frame.getContentPane().setLayout(new BorderLayout());
a61af66fc99e Initial load
duke
parents:
diff changeset
215 CommandProcessorPanel panel = new CommandProcessorPanel(null);
a61af66fc99e Initial load
duke
parents:
diff changeset
216 frame.getContentPane().add(panel, BorderLayout.CENTER);
a61af66fc99e Initial load
duke
parents:
diff changeset
217 frame.addWindowListener(new WindowAdapter() {
a61af66fc99e Initial load
duke
parents:
diff changeset
218 public void windowClosing(WindowEvent e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
219 System.exit(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
220 }
a61af66fc99e Initial load
duke
parents:
diff changeset
221 });
a61af66fc99e Initial load
duke
parents:
diff changeset
222 frame.setSize(500, 500);
152
c70a245cad3a 6670684: 4/5 SA command universe did not print out CMS space information
dcubed
parents: 0
diff changeset
223 frame.setVisible(true);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
224 panel.requestFocus();
a61af66fc99e Initial load
duke
parents:
diff changeset
225 }
a61af66fc99e Initial load
duke
parents:
diff changeset
226 }