/tags/1.0.1/src/org/getopt/luke/plugins/ScriptingPlugin.java

http://luke.googlecode.com/ · Java · 188 lines · 149 code · 23 blank · 16 comment · 12 complexity · 38808cdc62a215229a3b2ae6a7ae0ab2 MD5 · raw file

  1. /*
  2. * Created on Feb 7, 2005
  3. * Author: Andrzej Bialecki <ab@getopt.org>
  4. *
  5. */
  6. package org.getopt.luke.plugins;
  7. import java.io.BufferedReader;
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.InputStreamReader;
  12. import java.io.PrintStream;
  13. import java.io.PrintWriter;
  14. import java.io.StringWriter;
  15. import org.getopt.luke.Luke;
  16. import org.getopt.luke.LukePlugin;
  17. import org.mozilla.javascript.Context;
  18. import org.mozilla.javascript.Scriptable;
  19. import org.mozilla.javascript.ScriptableObject;
  20. import org.mozilla.javascript.tools.shell.Main;
  21. import thinlet.Thinlet;
  22. /**
  23. * This is a JavaScript interactive shell. All elements
  24. * of Luke framework are made available to the plugin by putting
  25. * them into JS Context.
  26. *
  27. * @author Andrzej Bialecki <ab@getopt.org>
  28. */
  29. public class ScriptingPlugin extends LukePlugin {
  30. private Shell shell = null;
  31. private StringBuffer scroll = new StringBuffer();
  32. public ScriptingPlugin() {
  33. }
  34. public void reset() {
  35. if (shell != null) shell.destroy();
  36. Object ta = app.find(myUi, "console");
  37. app.setString(ta, "text", "");
  38. scroll.setLength(0);
  39. StringWriter writer = new StringWriter();
  40. TAWriter taw = new TAWriter(app, ta, scroll, writer);
  41. // Associate a new Context with this thread
  42. try {
  43. // Initialize the standard objects (Object, Function, etc.)
  44. // This must be done before scripts can be executed.
  45. shell = new Shell(taw, taw, System.in);
  46. ScriptableObject.putProperty(shell, "app", app);
  47. ScriptableObject.putProperty(shell, "ir", ir);
  48. ScriptableObject.putProperty(shell, "dir", dir);
  49. ScriptableObject.putProperty(shell, "myUi", myUi);
  50. shell.prompt();
  51. } catch (Exception e) {
  52. e.printStackTrace();
  53. app.errorMsg("Error initializing Shell:\n" + e.getMessage());
  54. }
  55. }
  56. public void clear() {
  57. scroll.setLength(0);
  58. scroll.append("js> ");
  59. Object console = app.find(myUi, "console");
  60. app.setString(console, "text", scroll.toString());
  61. app.requestFocus(console);
  62. app.setInteger(console, "start", scroll.length());
  63. app.setInteger(console, "end", scroll.length());
  64. }
  65. public String getXULName() {
  66. return "/xml/scr-plugin.xml";
  67. }
  68. public String getPluginName() {
  69. return "Scripting Luke";
  70. }
  71. public String getPluginInfo() {
  72. return "Luke Scripting Plugin; by Andrzej Bialecki";
  73. }
  74. public String getPluginHome() {
  75. return "mailto:ab@getopt.org";
  76. }
  77. public boolean init() throws Exception {
  78. if (shell == null) reset();
  79. ScriptableObject.putProperty(shell, "app", app);
  80. ScriptableObject.putProperty(shell, "ir", ir);
  81. ScriptableObject.putProperty(shell, "dir", dir);
  82. ScriptableObject.putProperty(shell, "myUi", myUi);
  83. return true;
  84. }
  85. public void setWrap(Object cbWrap, Object ta) {
  86. app.setBoolean(ta, "wrap", app.getBoolean(cbWrap, "selected"));
  87. }
  88. public void execute(String cmd) {
  89. //System.out.println("exec '" + cmd + "'");
  90. scroll.append(cmd);
  91. if (!cmd.endsWith("\n")) scroll.append("\n");
  92. shell.processSource(shell.getContext(), cmd.trim());
  93. Object console = app.find(myUi, "console");
  94. app.requestFocus(console);
  95. app.setInteger(console, "start", scroll.length());
  96. app.setInteger(console, "end", scroll.length());
  97. }
  98. public void ins(Object ta) {
  99. String text = app.getString(ta, "text");
  100. if (!text.substring(0, scroll.length()).equals(scroll.toString())) {
  101. text = scroll.toString() + text.substring(scroll.length() + 1);
  102. app.setString(ta, "text", text);
  103. app.setInteger(ta, "start", text.length());
  104. app.setInteger(ta, "end", text.length());
  105. }
  106. String cmd = text.substring(scroll.length());
  107. int idx = cmd.indexOf('\n');
  108. if (cmd.endsWith("\n") && shell.getContext().stringIsCompilableUnit(cmd)) execute(cmd);
  109. }
  110. public void rem(Object ta) {
  111. int start = app.getInteger(ta, "start");
  112. if (start < scroll.length()) {
  113. app.setString(ta, "text", scroll.toString());
  114. app.setInteger(ta, "start", scroll.length());
  115. app.setInteger(ta, "end", scroll.length());
  116. }
  117. }
  118. public void car(Object ta) {
  119. int start = app.getInteger(ta, "start");
  120. if (start < scroll.length()) {
  121. app.setInteger(ta, "start", scroll.length());
  122. app.setInteger(ta, "end", scroll.length());
  123. }
  124. }
  125. public void actionHelp(Object ta) {
  126. String cmd = "help()\n";
  127. execute(cmd);
  128. }
  129. public void actionSample() {
  130. StringBuffer sb = new StringBuffer();
  131. try {
  132. InputStream is = getClass().getResourceAsStream("/xml/SampleScript.js");
  133. BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  134. String line = null;
  135. while ((line = br.readLine()) != null) {
  136. sb.append(line + "\n");
  137. }
  138. br.close();
  139. execute(sb.toString());
  140. } catch (Exception e) {
  141. e.printStackTrace();
  142. app.errorMsg("Loading failed: " + e.getMessage());
  143. return;
  144. }
  145. }
  146. }
  147. class TAWriter extends PrintWriter {
  148. public Luke app;
  149. public Object ta;
  150. public StringBuffer scroll;
  151. public StringWriter writer;
  152. public TAWriter(Luke app, Object ta, StringBuffer scroll, StringWriter writer) {
  153. super(writer);
  154. this.ta = ta;
  155. this.scroll = scroll;
  156. this.app = app;
  157. this.writer = writer;
  158. }
  159. public void flush() {
  160. super.flush();
  161. scroll.append(writer.getBuffer());
  162. app.setString(ta, "text", scroll.toString());
  163. writer.getBuffer().setLength(0);
  164. }
  165. }