KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > coach > tcl > TclShell


1 /***************************************************************************/
2 /* COACH: Component Based Open Source Architecture for */
3 /* Distributed Telecom Applications */
4 /* See: http://www.objectweb.org/ */
5 /* */
6 /* Copyright (C) 2003 Lucent Technologies Nederland BV */
7 /* Bell Labs Advanced Technologies - EMEA */
8 /* */
9 /* Initial developer(s): Harold Batteram */
10 /* */
11 /* This library is free software; you can redistribute it and/or */
12 /* modify it under the terms of the GNU Lesser General Public */
13 /* License as published by the Free Software Foundation; either */
14 /* version 2.1 of the License, or (at your option) any later version. */
15 /* */
16 /* This library is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
18 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
19 /* Lesser General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public */
22 /* License along with this library; if not, write to the Free Software */
23 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
24 /***************************************************************************/
25 package org.coach.tcl;
26
27 import tcl.lang.*;
28 import java.awt.*;
29 import java.awt.event.*;
30 import java.util.*;
31 import javax.swing.*;
32 import java.io.*;
33 import org.coach.util.IorPrinter;
34 import org.coach.idltree.*;
35
36 public class TclShell extends JPanel {
37     // The command input
38
private StringBuffer JavaDoc commandBuffer = new StringBuffer JavaDoc();
39     private PrintWriter pout;
40     private BufferedReader in;
41     private PrintWriter out = new TextAreaPrintWriter(new TextAreaPrintStream());
42     private TclLib api;
43
44     // The Tcl interpreter
45
private Interp tclInterp = new Interp();;
46
47     // The TextArea widget for displaying commands and results
48
public JTextArea textArea;
49
50     // Prompts
51
public String JavaDoc mainPrompt = "% ";
52     public String JavaDoc contPrompt = "> ";
53
54     // Cursors
55
private int promptCursor = 0;
56     private int commandCursor = 0;
57
58     // History
59
public int historyLength = 10;
60     private int historyCursor = 0;
61     private Vector historyCommands = new Vector();
62
63     // Platform dependent newline
64
private static String JavaDoc newline = System.getProperty("line.separator");
65     private String JavaDoc inputText;
66     private boolean inputMode;
67     private Object JavaDoc lock = new Object JavaDoc();
68
69     public TclShell() {
70         this(null, null);
71     }
72     
73     // Create a new Shell instance
74
public TclShell(org.omg.CORBA.Object JavaDoc target, String JavaDoc source) {
75         
76         // Graphics part
77
super(new BorderLayout());
78         
79         JScrollPane scrollPane = new JScrollPane();
80         textArea = new JTextArea("", 20, 80);
81         scrollPane.getViewport().add(textArea, null);
82         add(scrollPane, BorderLayout.CENTER);
83
84         // Event handling part
85
textArea.addKeyListener(new ShellKeyListener());
86
87         try {
88             PipedWriter pw1 = new PipedWriter();
89             pout = new PrintWriter(pw1, true);
90             in = new BufferedReader(new PipedReader(pw1));
91         } catch (Exception JavaDoc e) {
92             e.printStackTrace();
93         }
94
95         // Initialize the interpreter
96
try {
97             tclInterp.setVar("out", tcl.lang.ReflectObject.newInstance(tclInterp, out.getClass(), out), 0);
98             tclInterp.eval("package require java");
99             if (target != null) {
100                 org.omg.CORBA.ORB JavaDoc orb = org.objectweb.openccm.corba.TheORB.getORB();
101                 String JavaDoc ior = orb.object_to_string(target);
102                 IorPrinter iorPrinter = new IorPrinter(ior);
103                 String JavaDoc id = iorPrinter.getTypeId();
104                 IdlInterface interfaceNode = new IdlInterface(id, false);
105                 interfaceNode.setValue(ior);
106                 tclInterp.setVar("target", tcl.lang.ReflectObject.newInstance(tclInterp, IdlInterface.class, interfaceNode), 0);
107             }
108             tclInterp.setVar("TclShell", tcl.lang.ReflectObject.newInstance(tclInterp, TclShell.class, this), 0);
109             tclInterp.eval("proc puts {s} {global TclShell; $TclShell putText $s\\n}");
110 // tclInterp.eval("proc gets {} {global TclShell; return [$TclShell getText]}");
111

112             // Initialze API's functions (tcl procs) tclInterp
113
api = new TclLib(tclInterp);
114
115             if (source != null) {
116                 putText("source \"" + source + "\"");
117                 evalCommand("source \"" + source + "\"");
118                 textArea.append("\n");
119                 promptCursor -= 2;
120             }
121             
122         } catch (tcl.lang.TclException e) {
123             System.out.println(tclInterp.getResult());
124         }
125     }
126
127     public void setOrb(org.omg.CORBA.ORB JavaDoc orb) {
128         api.setOrb(orb);
129     }
130
131     public BufferedReader getReader() {
132         return in;
133     }
134
135     public PrintWriter getWriter() {
136         return out;
137     }
138
139     // Override this to initialize the prompt. We need to do
140
// because we can't write to the TextArea until the peer
141
// has been created
142
public void addNotify() {
143         super.addNotify();
144         putText(mainPrompt);
145     }
146
147     // Print output to the console, update the prompt cursor
148
public void putText(String JavaDoc s) {
149         if (s.length() != 0) {
150             textArea.append(s);
151             promptCursor += s.length();
152             textArea.setCaretPosition(promptCursor);
153         }
154     }
155
156     public void reply(String JavaDoc s) {
157 // putText(s);
158
pout.println(s);
159     }
160     
161     protected void evalCommand() {
162         String JavaDoc newText = textArea.getText().substring(promptCursor);
163         promptCursor += newText.length();
164         evalCommand(newText);
165     }
166
167     public void runScript(String JavaDoc source) {
168         api.setOrb(org.objectweb.openccm.corba.TheORB.getORB());
169         if (source != null) {
170             putText("source \"" + source + "\"");
171             evalCommand("source \"" + source + "\"");
172             textArea.append("\n");
173             promptCursor -= 2;
174         }
175     }
176     
177     // Evaluate the command so far, if possible, printing
178
// a continuation prompt if not.
179
protected void evalCommand(String JavaDoc newtext) {
180 // String newtext = textArea.getText().substring(promptCursor);
181
// promptCursor += newtext.length();
182

183         if (commandBuffer.length() > 0) {
184             commandBuffer.append("\n");
185         }
186
187         commandBuffer.append(newtext);
188         String JavaDoc command = commandBuffer.toString();
189
190         if (tclInterp.commandComplete(command)) {
191             // Process it
192
putText("\n");
193             Cursor oldCursor = textArea.getCursor();
194             textArea.setCursor(new Cursor(Cursor.WAIT_CURSOR));
195
196             try {
197                 tclInterp.eval(command);
198             } catch (tcl.lang.TclException e) {
199             // ignore
200
}
201             String JavaDoc result = tclInterp.getResult().toString();
202
203             if (result.length() > 0) {
204                 putText(result + "\n" + mainPrompt);
205             } else {
206                 putText(mainPrompt);
207             }
208
209             commandBuffer.setLength(0);
210             commandCursor = promptCursor;
211             textArea.setCursor(oldCursor);
212             updateHistory(command);
213         } else {
214             putText("\n" + contPrompt);
215         }
216     }
217
218     // Update the command history
219
void updateHistory(String JavaDoc command) {
220         historyCursor = 0;
221
222         if (historyCommands.size() == historyLength) {
223             historyCommands.removeElementAt(0);
224         }
225
226         historyCommands.addElement(command);
227     }
228
229     // Replace the command with an entry from the history
230
void nextCommand() {
231         String JavaDoc text;
232
233         if (historyCursor == 0) {
234             text = "";
235         } else {
236             historyCursor--;
237             text = (String JavaDoc)historyCommands.elementAt(historyCommands.size() - historyCursor - 1);
238         }
239
240         textArea.replaceRange(text, commandCursor, textArea.getText().length());
241     }
242
243     void previousCommand() {
244         String JavaDoc text;
245
246         if (historyCursor == historyCommands.size()) {
247             return;
248         } else {
249             historyCursor++;
250             text = (String JavaDoc)historyCommands.elementAt(historyCommands.size() - historyCursor);
251         }
252
253         textArea.replaceRange(text, commandCursor, textArea.getText().length());
254     }
255
256
257
258     // The key listener
259
class ShellKeyListener extends KeyAdapter {
260         public void keyPressed(KeyEvent keyEvent) {
261             // Process keys
262
switch ( keyEvent.getKeyCode() ) {
263                 case KeyEvent.VK_ENTER :
264                     keyEvent.consume();
265                     evalCommand();
266                     break;
267                 case KeyEvent.VK_BACK_SPACE :
268
269                     if (textArea.getCaretPosition() == promptCursor) {
270                         keyEvent.consume();
271                     // don't backspace over prompt!
272
}
273
274                     break;
275                 case KeyEvent.VK_LEFT :
276
277                     if (textArea.getCaretPosition() == promptCursor) {
278                         keyEvent.consume();
279                     }
280
281                     break;
282                 case KeyEvent.VK_UP :
283                     previousCommand();
284                     keyEvent.consume();
285                     break;
286                 case KeyEvent.VK_DOWN :
287                     nextCommand();
288                     keyEvent.consume();
289                     break;
290                 default :
291             // Otherwise we got a regular character. Don't consume it,
292
// and TextArea will take care of displaying it.
293
}
294         }
295     }
296     
297     class TextAreaPrintStream extends java.io.OutputStream JavaDoc {
298         public synchronized void write(int b) {
299             // recall that the int should really just be a byte
300
b &= 0x000000FF;
301             // must convert byte to a char in order to append it
302
char c = (char)b;
303             putText(String.valueOf(c));
304         }
305
306         public synchronized void write(byte[] b, int offset, int length) {
307             putText(new String JavaDoc(b, offset, length));
308         }
309     }
310
311     class TextAreaPrintWriter extends java.io.PrintWriter JavaDoc {
312         
313         public TextAreaPrintWriter(java.io.OutputStream JavaDoc out) {
314             super(out, true);
315         }
316    
317         public void print(String JavaDoc s) {
318             super.print(s);
319             try {
320                 flush();
321             } catch (Exception JavaDoc e) {
322             }
323         }
324
325         public void println(String JavaDoc s) {
326             super.println(s);
327             try {
328                 flush();
329             } catch (Exception JavaDoc e) {
330             }
331         }
332     }
333 }
334
Popular Tags