PageRenderTime 15ms CodeModel.GetById 7ms app.highlight 7ms RepoModel.GetById 0ms app.codeStats 0ms

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