1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 private StringBuffer commandBuffer = new StringBuffer (); 39 private PrintWriter pout; 40 private BufferedReader in; 41 private PrintWriter out = new TextAreaPrintWriter(new TextAreaPrintStream()); 42 private TclLib api; 43 44 private Interp tclInterp = new Interp();; 46 47 public JTextArea textArea; 49 50 public String mainPrompt = "% "; 52 public String contPrompt = "> "; 53 54 private int promptCursor = 0; 56 private int commandCursor = 0; 57 58 public int historyLength = 10; 60 private int historyCursor = 0; 61 private Vector historyCommands = new Vector(); 62 63 private static String newline = System.getProperty("line.separator"); 65 private String inputText; 66 private boolean inputMode; 67 private Object lock = new Object (); 68 69 public TclShell() { 70 this(null, null); 71 } 72 73 public TclShell(org.omg.CORBA.Object target, String source) { 75 76 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 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 e) { 92 e.printStackTrace(); 93 } 94 95 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 orb = org.objectweb.openccm.corba.TheORB.getORB(); 101 String ior = orb.object_to_string(target); 102 IorPrinter iorPrinter = new IorPrinter(ior); 103 String 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 112 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 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 public void addNotify() { 143 super.addNotify(); 144 putText(mainPrompt); 145 } 146 147 public void putText(String 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 s) { 157 pout.println(s); 159 } 160 161 protected void evalCommand() { 162 String newText = textArea.getText().substring(promptCursor); 163 promptCursor += newText.length(); 164 evalCommand(newText); 165 } 166 167 public void runScript(String 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 protected void evalCommand(String newtext) { 180 183 if (commandBuffer.length() > 0) { 184 commandBuffer.append("\n"); 185 } 186 187 commandBuffer.append(newtext); 188 String command = commandBuffer.toString(); 189 190 if (tclInterp.commandComplete(command)) { 191 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 } 201 String 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 void updateHistory(String command) { 220 historyCursor = 0; 221 222 if (historyCommands.size() == historyLength) { 223 historyCommands.removeElementAt(0); 224 } 225 226 historyCommands.addElement(command); 227 } 228 229 void nextCommand() { 231 String text; 232 233 if (historyCursor == 0) { 234 text = ""; 235 } else { 236 historyCursor--; 237 text = (String )historyCommands.elementAt(historyCommands.size() - historyCursor - 1); 238 } 239 240 textArea.replaceRange(text, commandCursor, textArea.getText().length()); 241 } 242 243 void previousCommand() { 244 String text; 245 246 if (historyCursor == historyCommands.size()) { 247 return; 248 } else { 249 historyCursor++; 250 text = (String )historyCommands.elementAt(historyCommands.size() - historyCursor); 251 } 252 253 textArea.replaceRange(text, commandCursor, textArea.getText().length()); 254 } 255 256 257 258 class ShellKeyListener extends KeyAdapter { 260 public void keyPressed(KeyEvent keyEvent) { 261 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 } 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 } 294 } 295 } 296 297 class TextAreaPrintStream extends java.io.OutputStream { 298 public synchronized void write(int b) { 299 b &= 0x000000FF; 301 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 (b, offset, length)); 308 } 309 } 310 311 class TextAreaPrintWriter extends java.io.PrintWriter { 312 313 public TextAreaPrintWriter(java.io.OutputStream out) { 314 super(out, true); 315 } 316 317 public void print(String s) { 318 super.print(s); 319 try { 320 flush(); 321 } catch (Exception e) { 322 } 323 } 324 325 public void println(String s) { 326 super.println(s); 327 try { 328 flush(); 329 } catch (Exception e) { 330 } 331 } 332 } 333 } 334 | Popular Tags |