KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > demo > scripting > jconsole > ScriptShellPanel


1 /*
2  * @(#)ScriptShellPanel.java 1.2 06/07/18 06:21:13
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 package com.sun.demo.scripting.jconsole;
38
39 import java.awt.*;
40 import java.awt.event.*;
41 import java.util.concurrent.ExecutorService JavaDoc;
42 import java.util.concurrent.Executors JavaDoc;
43 import javax.swing.*;
44 import javax.swing.event.*;
45 import javax.swing.text.*;
46
47
48 /**
49  * A JPanel subclass containing a scrollable text area displaying the
50  * jconsole's script console.
51  */

52 class ScriptShellPanel extends JPanel {
53
54     // interface to evaluate script command and script prompt
55
interface CommandProcessor {
56         // execute given String as script and return the result
57
public String JavaDoc executeCommand(String JavaDoc cmd);
58         // get prompt used for interactive read-eval-loop
59
public String JavaDoc getPrompt();
60     }
61
62     // my script command processor
63
private CommandProcessor commandProcessor;
64     // editor component for command editing
65
private JTextComponent editor;
66     
67     private final ExecutorService JavaDoc commandExecutor =
68             Executors.newSingleThreadExecutor();
69
70     // document management
71
private boolean updating;
72
73     public ScriptShellPanel(CommandProcessor cmdProc) {
74         setLayout(new BorderLayout());
75         this.commandProcessor = cmdProc;
76         this.editor = new JTextArea();
77         editor.setDocument(new EditableAtEndDocument());
78         JScrollPane scroller = new JScrollPane();
79         scroller.getViewport().add(editor);
80         add(scroller, BorderLayout.CENTER);
81
82         editor.getDocument().addDocumentListener(new DocumentListener() {
83             public void changedUpdate(DocumentEvent e) {
84             }
85
86             public void insertUpdate(DocumentEvent e) {
87                 if (updating) return;
88                 beginUpdate();
89                 editor.setCaretPosition(editor.getDocument().getLength());
90                 if (insertContains(e, '\n')) {
91                     String JavaDoc cmd = getMarkedText();
92                     // Handle multi-line input
93
if ((cmd.length() == 0) ||
94                         (cmd.charAt(cmd.length() - 1) != '\\')) {
95                         // Trim "\\n" combinations
96
final String JavaDoc cmd1 = trimContinuations(cmd);
97                         commandExecutor.execute(new Runnable JavaDoc() {
98                             public void run() {
99                                 final String JavaDoc result = executeCommand(cmd1);
100                                 
101                                 SwingUtilities.invokeLater(new Runnable JavaDoc() {
102                                     public void run() {
103                                         if (result != null) {
104                                             print(result + "\n");
105                                         }
106                                         printPrompt();
107                                         setMark();
108                                         endUpdate();
109                                     }
110                                 });
111                             }
112                         });
113                     } else {
114                         endUpdate();
115                     }
116                 } else {
117                     endUpdate();
118                 }
119             }
120
121             public void removeUpdate(DocumentEvent e) {
122             }
123         });
124
125         // This is a bit of a hack but is probably better than relying on
126
// the JEditorPane to update the caret's position precisely the
127
// size of the insertion
128
editor.addCaretListener(new CaretListener() {
129             public void caretUpdate(CaretEvent e) {
130                 int len = editor.getDocument().getLength();
131                 if (e.getDot() > len) {
132                     editor.setCaretPosition(len);
133                 }
134             }
135         });
136
137         Box hbox = Box.createHorizontalBox();
138         hbox.add(Box.createGlue());
139         JButton button = new JButton("Clear"); // FIXME: i18n?
140
button.addActionListener(new ActionListener() {
141             public void actionPerformed(ActionEvent e) {
142                 clear();
143             }
144         });
145         hbox.add(button);
146         hbox.add(Box.createGlue());
147         add(hbox, BorderLayout.SOUTH);
148
149         clear();
150     }
151
152     public void dispose() {
153         commandExecutor.shutdown();
154     }
155     
156     public void requestFocus() {
157         editor.requestFocus();
158     }
159
160     public void clear() {
161         clear(true);
162     }
163
164     public void clear(boolean prompt) {
165         EditableAtEndDocument d = (EditableAtEndDocument) editor.getDocument();
166         d.clear();
167         if (prompt) printPrompt();
168         setMark();
169         editor.requestFocus();
170     }
171
172     public void setMark() {
173         ((EditableAtEndDocument) editor.getDocument()).setMark();
174     }
175
176     public String JavaDoc getMarkedText() {
177         try {
178             String JavaDoc s = ((EditableAtEndDocument) editor.getDocument()).getMarkedText();
179             int i = s.length();
180             while ((i > 0) && (s.charAt(i - 1) == '\n')) {
181                 i--;
182             }
183             return s.substring(0, i);
184         } catch (BadLocationException e) {
185             e.printStackTrace();
186             return null;
187         }
188     }
189
190     public void print(String JavaDoc s) {
191         Document d = editor.getDocument();
192         try {
193             d.insertString(d.getLength(), s, null);
194         } catch (BadLocationException e) {
195             e.printStackTrace();
196         }
197     }
198
199
200     //
201
// Internals only below this point
202
//
203

204     private String JavaDoc executeCommand(String JavaDoc cmd) {
205         return commandProcessor.executeCommand(cmd);
206     }
207
208     private String JavaDoc getPrompt() {
209         return commandProcessor.getPrompt();
210     }
211
212     private void beginUpdate() {
213         editor.setEditable(false);
214         updating = true;
215     }
216
217     private void endUpdate() {
218         editor.setEditable(true);
219         updating = false;
220     }
221
222     private void printPrompt() {
223         print(getPrompt());
224     }
225
226     private boolean insertContains(DocumentEvent e, char c) {
227         String JavaDoc s = null;
228         try {
229             s = editor.getText(e.getOffset(), e.getLength());
230             for (int i = 0; i < e.getLength(); i++) {
231                 if (s.charAt(i) == c) {
232                     return true;
233                 }
234             }
235         } catch (BadLocationException ex) {
236             ex.printStackTrace();
237         }
238         return false;
239     }
240
241     private String JavaDoc trimContinuations(String JavaDoc text) {
242         int i;
243         while ((i = text.indexOf("\\\n")) >= 0) {
244             text = text.substring(0, i) + text.substring(i+1, text.length());
245         }
246         return text;
247     }
248 }
249
Popular Tags