Dedup some code in the SketchRunner
MODIFY
runtime/src/jycessing/mode/run/SketchRunner.java
runtime/src/jycessing/mode/run/SketchRunner.java
@@ -68,19 +68,12 @@ public class SketchRunner implements SketchService {
System.exit(0);
}
- @Override
- public void startSketch(final PdeSketch sketch) {
- runner =
- new Thread(
- () -> {
- try {
- try {
- final Printer stdout =
- new Printer() {
- @Override
+ private abstract class RemotePrinter implements Printer {
+ abstract protected void doPrint(String s) throws RemoteException;
+
public void print(final Object o) {
try {
- modeService.printStdOut(id, String.valueOf(o));
+ doPrint(String.valueOf(o));
} catch (final RemoteException e) {
System.err.println(e);
}
@@ -90,25 +83,26 @@ public class SketchRunner implements SketchService {
public void flush() {
// no-op
}
- };
- final Printer stderr =
- new Printer() {
+ }
+
@Override
- public void print(final Object o) {
+ public void startSketch(final PdeSketch sketch) {
+ runner = new Thread(() -> {
try {
- modeService.printStdErr(id, String.valueOf(o));
- } catch (final RemoteException e) {
- System.err.println(e);
- }
+ try {
+ final Printer stdout = new RemotePrinter() {
+ @Override
+ protected void doPrint(final String s) throws RemoteException {
+ modeService.printStdOut(id, s);
}
-
+ };
+ final Printer stderr = new RemotePrinter() {
@Override
- public void flush() {
- // no-op
+ protected void doPrint(final String s) throws RemoteException {
+ modeService.printStdErr(id, s);
}
};
- final SketchPositionListener sketchPositionListener =
- leftTop -> {
+ final SketchPositionListener sketchPositionListener = leftTop -> {
try {
modeService.handleSketchMoved(id, leftTop);
} catch (final RemoteException e) {
@@ -121,15 +115,13 @@ public class SketchRunner implements SketchService {
if (e1.getMessage().startsWith("SystemExit")) {
// Someone called sys.exit(). No-op.
} else {
- modeService.handleSketchException(
- id, convertPythonSketchError(e1, sketch.codeFileNames));
+ modeService.handleSketchException(id,
+ convertPythonSketchError(e1, sketch.codeFileNames));
}
} catch (final Exception e2) {
if (e2.getCause() != null && e2.getCause() instanceof PythonSketchError) {
- modeService.handleSketchException(
- id,
- convertPythonSketchError(
- (PythonSketchError) e2.getCause(), sketch.codeFileNames));
+ modeService.handleSketchException(id,
+ convertPythonSketchError((PythonSketchError)e2.getCause(), sketch.codeFileNames));
} else {
modeService.handleSketchException(id, e2);
}
@@ -142,8 +134,7 @@ public class SketchRunner implements SketchService {
}
// Exiting; no need to interrupt and join it later.
runner = null;
- },
- "processing.py mode runner");
+ }, "processing.py mode runner");
runner.start();
}
@@ -215,10 +206,7 @@ public class SketchRunner implements SketchService {
final SketchService stub = (SketchService)RMIUtils.export(sketchRunner);
log("Calling mode's handleReady().");
modeService.handleReady(id, stub);
- Runtime.getRuntime()
- .addShutdownHook(
- new Thread(
- () -> {
+ Runtime.getRuntime().addShutdownHook(new Thread(() -> {
log("Exiting; telling modeService.");
try {
modeService.handleSketchStopped(id);
@@ -228,8 +216,8 @@ public class SketchRunner implements SketchService {
}));
}
- private SketchException convertPythonSketchError(
- final PythonSketchError e, final String[] fileNames) {
+ private SketchException convertPythonSketchError(final PythonSketchError e,
+ final String[] fileNames) {
if (e.fileName == null) {
return new SketchException(e.getMessage());
}

Dedup some code in the SketchRunner