4040import javax .script .ScriptContext ;
4141import javax .script .ScriptException ;
4242import javax .swing .JButton ;
43+ import javax .swing .JComponent ;
4344import javax .swing .JPanel ;
4445import javax .swing .JScrollPane ;
4546import javax .swing .JSplitPane ;
4647
4748import net .miginfocom .swing .MigLayout ;
4849
50+ import org .scijava .Context ;
4951import org .scijava .log .LogService ;
50- import org .scijava .prefs .PrefService ;
51- import org .scijava .script .DefaultScriptInterpreter ;
52- import org .scijava .script .ScriptInterpreter ;
53- import org .scijava .script .ScriptLanguage ;
54- import org .scijava .script .ScriptService ;
52+ import org .scijava .plugin .Parameter ;
53+ import org .scijava .script .ScriptREPL ;
54+ import org .scijava .widget .UIComponent ;
5555
5656/**
57- * A Swing UI pane for a language-specific script interpreter .
57+ * A Swing UI pane for the SciJava scripting REPL .
5858 *
5959 * @author Curtis Rueden
6060 * @author Johannes Schindelin
6161 */
62- public class InterpreterPane {
62+ public class InterpreterPane implements UIComponent < JComponent > {
6363
64- private final ScriptInterpreter interpreter ;
64+ private final ScriptREPL repl ;
6565
6666 private final JSplitPane splitPane ;
6767 private final OutputPane output ;
6868 private final PromptPane prompt ;
6969
70- /**
71- * Constructs an interpreter UI pane for a specific scripting language.
72- *
73- * @param prefs service to use for persisting the history
74- * @param scriptService service to use for scripting
75- * @param language scripting language for which to construct a UI pane
76- * @param log service to use for logging
77- */
78- public InterpreterPane (final PrefService prefs ,
79- final ScriptService scriptService , final ScriptLanguage language ,
80- final LogService log )
81- {
82- this (createInterpreter (prefs , scriptService , language ), log );
83- }
70+ @ Parameter (required = false )
71+ private LogService log ;
8472
8573 /**
86- * Constructs an interpreter UI pane for a specific script interpreter .
74+ * Constructs an interpreter UI pane for a SciJava scripting REPL .
8775 *
88- * @param interpreter script interpreter to use for script execution
89- * @param log service to use for logging
76+ * @param context The SciJava application context to use
9077 */
91- public InterpreterPane (final ScriptInterpreter interpreter ,
92- final LogService log )
93- {
94- this .interpreter = interpreter ;
95-
78+ public InterpreterPane (final Context context ) {
79+ context .inject (this );
9680 output = new OutputPane (log );
9781 final JScrollPane outputScroll = new JScrollPane (output );
9882 outputScroll .setPreferredSize (new Dimension (440 , 400 ));
9983
84+ repl = new ScriptREPL (context , output .getOutputStream ());
85+ repl .initialize ();
86+
10087 final Writer writer = output .getOutputWriter ();
101- final ScriptContext context = interpreter .getEngine ().getContext ();
102- context .setErrorWriter (writer );
103- context .setWriter (writer );
88+ final ScriptContext ctx = repl . getInterpreter () .getEngine ().getContext ();
89+ ctx .setErrorWriter (writer );
90+ ctx .setWriter (writer );
10491
105- prompt = new PromptPane (interpreter , output );
106- final JScrollPane promptScroll = new JScrollPane (prompt );
92+ prompt = new PromptPane (repl , output ) {
93+ @ Override
94+ public void quit () {
95+ dispose ();
96+ }
97+ };
98+ final JScrollPane promptScroll = new JScrollPane (prompt .getComponent ());
10799
108100 final JButton clearButton = new JButton ("Clear" );
109101 clearButton .setToolTipText ("Clears the text in the output pane." );
@@ -121,9 +113,8 @@ public void actionPerformed(final ActionEvent e) {
121113 bottomPane .add (promptScroll , "spany 2" );
122114 bottomPane .add (clearButton , "w pref!, h pref!, wrap" );
123115
124- final Object importGenerator =
125- DefaultAutoImporters .getImportGenerator (log .getContext (), interpreter
126- .getLanguage ());
116+ final Object importGenerator = DefaultAutoImporters .getImportGenerator (
117+ log .getContext (), repl .getInterpreter ().getLanguage ());
127118 if (importGenerator != null ) {
128119 final JButton autoImportButton = new JButton ("Auto-Import" );
129120 autoImportButton .setToolTipText ("Auto-imports common classes." );
@@ -132,13 +123,13 @@ public void actionPerformed(final ActionEvent e) {
132123 @ Override
133124 public void actionPerformed (final ActionEvent e ) {
134125 try {
135- interpreter .getEngine ().eval (importGenerator .toString ());
126+ repl . getInterpreter () .getEngine ().eval (importGenerator .toString ());
136127 }
137128 catch (final ScriptException e1 ) {
138129 e1 .printStackTrace (new PrintWriter (output .getOutputWriter ()));
139130 }
140131 autoImportButton .setEnabled (false );
141- prompt .requestFocus ();
132+ prompt .getComponent (). requestFocus ();
142133 }
143134 });
144135 bottomPane .add (autoImportButton , "w pref!, h pref!, wrap" );
@@ -149,23 +140,14 @@ public void actionPerformed(final ActionEvent e) {
149140 splitPane .setResizeWeight (1 );
150141 }
151142
152- public void dispose () throws IOException {
153- output .close ();
154- }
155-
156143 // -- InterpreterPane methods --
157144
158- /** Gets the associated script interpreter . */
159- public ScriptInterpreter getInterpreter () {
160- return interpreter ;
145+ /** Gets the associated script REPL . */
146+ public ScriptREPL getREPL () {
147+ return repl ;
161148 }
162149
163- /** Gets the pane's Swing UI component. */
164- public JSplitPane getComponent () {
165- return splitPane ;
166- }
167-
168- /** Print a message to the output panel */
150+ /** Prints a message to the output panel. */
169151 public void print (final String string ) {
170152 final Writer writer = output .getOutputWriter ();
171153 try {
@@ -176,12 +158,20 @@ public void print(final String string) {
176158 }
177159 }
178160
179- // -- Utility methods --
161+ public void dispose () {
162+ output .close ();
163+ }
164+
165+ // -- UIComponent methods --
166+
167+ @ Override
168+ public JComponent getComponent () {
169+ return splitPane ;
170+ }
180171
181- public static ScriptInterpreter createInterpreter (final PrefService prefs ,
182- final ScriptService scriptService , final ScriptLanguage language )
183- {
184- return new DefaultScriptInterpreter (prefs , scriptService , language );
172+ @ Override
173+ public Class <JComponent > getComponentType () {
174+ return JComponent .class ;
185175 }
186176
187177}
0 commit comments