11/*
22 * Image/J Plugins
3- * Copyright (C) 2002-2012 Jarek Sacha
3+ * Copyright (C) 2002-2014 Jarek Sacha
44 * Author's email: jsacha at users dot sourceforge dot net
55 *
66 * This library is free software; you can redistribute it and/or
2323package net .sf .ij_plugins .scala .console
2424
2525
26- import java .io .{PrintStream , OutputStream , Writer }
26+ import java .io .{PrintStream , Writer }
2727import scala .Enumeration
28+ import scala .collection .mutable .ArrayBuffer
2829import scala .swing .Publisher
2930import scala .swing .event .Event
3031import scala .tools .nsc .interpreter .Results .Result
3132import scala .tools .nsc .interpreter ._
3233import scala .tools .nsc .{NewLinePrintWriter , Settings }
33- import scala .collection .mutable .ArrayBuffer
3434
3535object ScalaInterpreter {
3636
37- /**
38- * Interpreter execution state.
39- */
40- object State extends Enumeration {
41- val Running = Value (" Running..." )
42- val Ready = Value (" Ready" )
43- }
44-
45- /**
46- * Interpreter state changed.
47- */
48- case class StateEvent (state : State .Value ) extends Event
49-
50- /**
51- * Posted after interpreter finished with results returned by the interpreter
52- */
53- case class ResultEvent (result : Result ) extends Event
54-
55- /**
56- * New value `data` in the standard out stream.
57- */
58- case class OutStreamEvent (data : String ) extends Event
59-
60- /**
61- * New value `data` in the standard err stream.
62- */
63- case class ErrStreamEvent (data : String ) extends Event
64-
65- /**
66- * New value `data` in the interpreter log.
67- */
68- case class InterpreterLogEvent (data : String ) extends Event
37+ /**
38+ * Interpreter execution state.
39+ */
40+ object State extends Enumeration {
41+ val Running = Value (" Running..." )
42+ val Ready = Value (" Ready" )
43+ }
44+
45+ /**
46+ * Interpreter state changed.
47+ */
48+ case class StateEvent (state : State .Value ) extends Event
49+
50+ /**
51+ * Posted after interpreter finished with results returned by the interpreter
52+ */
53+ case class ResultEvent (result : Result ) extends Event
54+
55+ /**
56+ * New value `data` in the standard out stream.
57+ */
58+ case class OutStreamEvent (data : String ) extends Event
59+
60+ /**
61+ * New value `data` in the standard err stream.
62+ */
63+ case class ErrStreamEvent (data : String ) extends Event
64+
65+ /**
66+ * New value `data` in the interpreter log.
67+ */
68+ case class InterpreterLogEvent (data : String ) extends Event
6969
7070}
7171
@@ -83,100 +83,98 @@ object ScalaInterpreter {
8383 */
8484class ScalaInterpreter () extends Publisher {
8585
86- import ScalaInterpreter ._
86+ import ScalaInterpreter ._
8787
88- val interpreterOutBuffer = new ArrayBuffer [String ]
88+ val interpreterOutBuffer = new ArrayBuffer [String ]
8989
90- private class LogOutputStream extends OutputStream {
91- def write (b : Int ) {
92- write(Array (b.toByte), 0 , 1 )
93- }
90+ private class LogOutputStream extends OutputStream {
91+ def write (b : Int ) {
92+ write(Array (b.toByte), 0 , 1 )
9493 }
94+ }
9595
96- private object outStream extends LogOutputStream {
97- override def write (b : Array [Byte ], off : Int , len : Int ) {
98- publish(OutStreamEvent (new String (b, off, len)))
99- }
96+ private object outStream extends LogOutputStream {
97+ override def write (b : Array [Byte ], off : Int , len : Int ) {
98+ publish(OutStreamEvent (new String (b, off, len)))
10099 }
100+ }
101101
102- private object errStream extends LogOutputStream {
103- override def write (b : Array [Byte ], off : Int , len : Int ) {
104- publish(ErrStreamEvent (new String (b, off, len)))
105- }
102+ private object errStream extends LogOutputStream {
103+ override def write (b : Array [Byte ], off : Int , len : Int ) {
104+ publish(ErrStreamEvent (new String (b, off, len)))
106105 }
106+ }
107107
108108
109- private object interpreterOut extends Writer {
110- def close () {}
109+ private object interpreterOut extends Writer {
110+ def close () {}
111111
112- def flush () {}
112+ def flush () {}
113113
114- def write (buf : Array [Char ], off : Int , len : Int ) {
115- val s = new String (buf, off, len)
116- interpreterOutBuffer.append(s)
117- }
114+ def write (buf : Array [Char ], off : Int , len : Int ) {
115+ val s = new String (buf, off, len)
116+ interpreterOutBuffer.append(s)
118117 }
118+ }
119119
120120
121- val interpreterSettings = new Settings () {
122- usejavacp.value = true
123- // classpath.value +=
124- }
121+ val interpreterSettings = new Settings () {
122+ usejavacp.value = true
123+ // classpath.value +=
124+ }
125125
126- // Create interpreter
127- val interpreter = new IMain (interpreterSettings, new NewLinePrintWriter (interpreterOut, true ))
126+ // Create interpreter
127+ val interpreter = new IMain (interpreterSettings, new NewLinePrintWriter (interpreterOut, true ))
128128
129- private var _state = State .Ready
129+ private var _state = State .Ready
130130
131- /**
132- * Current state.
133- */
134- def state : State .Value = _state
131+ /**
132+ * Current state.
133+ */
134+ def state : State .Value = _state
135135
136136
137- private def state_= (newState : State .Value ) {
138- _state = newState
139- publish(StateEvent (_state))
140- }
137+ private def state_= (newState : State .Value ) {
138+ _state = newState
139+ publish(StateEvent (_state))
140+ }
141141
142142
143- /**
144- * Interpret `code`
145- * @param code actual text of the code to be interpreted.
146- */
147- def run (code : String ) {
148-
149- interpreterOutBuffer.clear()
150- state = State .Running
151-
152- // TODO: Can scala.swing.SwingWorker be used here?
153-
154- // Setup
155- val worker = new javax.swing.SwingWorker [Result , Result ] {
156- override def doInBackground (): Result = {
157- Console .setOut(outStream)
158- java.lang.System .setOut(new PrintStream (outStream))
159- Console .setErr(errStream)
160- java.lang.System .setErr(new PrintStream (outStream))
161-
162- interpreter.interpret(code)
163- }
164-
165-
166- override def done () {
167- get match {
168- case Results .Error => {
169- ScalaInterpreter .this .publish(ErrStreamEvent (interpreterOutBuffer.mkString))
170- }
171- case _ => {
172- ScalaInterpreter .this .publish(InterpreterLogEvent (" \n " + interpreterOutBuffer.mkString))
173- }
174- }
175- ScalaInterpreter .this .state = State .Ready
176- }
177- }
143+ /**
144+ * Interpret `code`
145+ * @param code actual text of the code to be interpreted.
146+ */
147+ def run (code : String ) {
148+
149+ interpreterOutBuffer.clear()
150+ state = State .Running
151+
152+ // TODO: Can scala.swing.SwingWorker be used here?
178153
179- // Execute
180- worker.execute()
154+ // Setup
155+ val worker = new javax.swing.SwingWorker [Result , Result ] {
156+ override def doInBackground (): Result = {
157+ Console .setOut(outStream)
158+ java.lang.System .setOut(new PrintStream (outStream))
159+ Console .setErr(errStream)
160+ java.lang.System .setErr(new PrintStream (outStream))
161+
162+ interpreter.interpret(code)
163+ }
164+
165+
166+ override def done () {
167+ get match {
168+ case Results .Error =>
169+ ScalaInterpreter .this .publish(ErrStreamEvent (interpreterOutBuffer.mkString))
170+ case _ =>
171+ ScalaInterpreter .this .publish(InterpreterLogEvent (" \n " + interpreterOutBuffer.mkString))
172+ }
173+ ScalaInterpreter .this .state = State .Ready
174+ }
181175 }
176+
177+ // Execute
178+ worker.execute()
179+ }
182180}
0 commit comments