Skip to content

Commit fd6d956

Browse files
author
Federico Fissore
committed
Export sketch: refactored to reuse existing code
1 parent 4d029de commit fd6d956

File tree

2 files changed

+16
-63
lines changed

2 files changed

+16
-63
lines changed

app/src/processing/app/Editor.java

Lines changed: 15 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ protected JMenu buildSketchMenu() {
619619
JMenuItem item = newJMenuItem(_("Verify / Compile"), 'R');
620620
item.addActionListener(new ActionListener() {
621621
public void actionPerformed(ActionEvent e) {
622-
handleRun(false);
622+
handleRun(false, Editor.this.presentHandler, Editor.this.runHandler);
623623
}
624624
});
625625
sketchMenu.add(item);
@@ -644,7 +644,7 @@ public void actionPerformed(ActionEvent e) {
644644
item = newJMenuItemAlt(_("Export compiled Binary"), 'S');
645645
item.addActionListener(new ActionListener() {
646646
public void actionPerformed(ActionEvent e) {
647-
handleRunAndSave(true);
647+
handleRun(false, Editor.this.presentAndSaveHandler, Editor.this.runAndSaveHandler);
648648
}
649649
});
650650
sketchMenu.add(item);
@@ -1531,8 +1531,8 @@ public void setHandlers(Runnable runHandler,
15311531
public void resetHandlers() {
15321532
runHandler = new BuildHandler();
15331533
presentHandler = new BuildHandler(true);
1534-
runAndSaveHandler = new BuildAndSaveHandler();
1535-
presentAndSaveHandler = new BuildAndSaveHandler(true);
1534+
runAndSaveHandler = new BuildHandler(false, true);
1535+
presentAndSaveHandler = new BuildHandler(true, true);
15361536
stopHandler = new DefaultStopHandler();
15371537
exportHandler = new DefaultExportHandler();
15381538
exportAppHandler = new DefaultExportAppHandler();
@@ -1999,8 +1999,10 @@ protected void handleFindReference() {
19991999
/**
20002000
* Implements Sketch → Run.
20012001
* @param verbose Set true to run with verbose output.
2002+
* @param verboseHandler
2003+
* @param nonVerboseHandler
20022004
*/
2003-
public void handleRun(final boolean verbose) {
2005+
public void handleRun(final boolean verbose, Runnable verboseHandler, Runnable nonVerboseHandler) {
20042006
internalCloseRunner();
20052007
if (PreferencesData.getBoolean("editor.save_on_verify")) {
20062008
if (sketch.isModified() && !sketch.isReadOnly()) {
@@ -2021,49 +2023,32 @@ public void handleRun(final boolean verbose) {
20212023

20222024
// Cannot use invokeLater() here, otherwise it gets
20232025
// placed on the event thread and causes a hang--bad idea all around.
2024-
new Thread(verbose ? presentHandler : runHandler).start();
2026+
new Thread(verbose ? verboseHandler : nonVerboseHandler).start();
20252027
}
20262028

2027-
/**
2028-
* Implements Sketch → Run and Save.
2029-
* @param verbose Set true to run with verbose output.
2030-
*/
2031-
public void handleRunAndSave(final boolean verbose) {
2032-
internalCloseRunner();
2033-
running = true;
2034-
toolbar.activate(EditorToolbar.RUN);
2035-
status.progress(_("Compiling sketch..."));
2036-
2037-
// do this to advance/clear the terminal window / dos prompt / etc
2038-
for (int i = 0; i < 10; i++) System.out.println();
2039-
2040-
// clear the console on each run, unless the user doesn't want to
2041-
if (Preferences.getBoolean("console.auto_clear")) {
2042-
console.clear();
2043-
}
2044-
2045-
// Cannot use invokeLater() here, otherwise it gets
2046-
// placed on the event thread and causes a hang--bad idea all around.
2047-
new Thread(verbose ? presentAndSaveHandler : runAndSaveHandler).start();
2048-
}
2049-
20502029
class BuildHandler implements Runnable {
20512030

20522031
private final boolean verbose;
2032+
private final boolean saveHex;
20532033

20542034
public BuildHandler() {
20552035
this(false);
20562036
}
20572037

20582038
public BuildHandler(boolean verbose) {
2039+
this(verbose, false);
2040+
}
2041+
2042+
public BuildHandler(boolean verbose, boolean saveHex) {
20592043
this.verbose = verbose;
2044+
this.saveHex = saveHex;
20602045
}
20612046

20622047
@Override
20632048
public void run() {
20642049
try {
20652050
sketch.prepare();
2066-
sketch.build(verbose, false);
2051+
sketch.build(verbose, saveHex);
20672052
statusNotice(_("Done compiling."));
20682053
} catch (PreferencesMapException e) {
20692054
statusError(I18n.format(
@@ -2078,38 +2063,6 @@ public void run() {
20782063
toolbar.deactivate(EditorToolbar.RUN);
20792064
}
20802065
}
2081-
2082-
class BuildAndSaveHandler implements Runnable {
2083-
2084-
private final boolean verbose;
2085-
2086-
public BuildAndSaveHandler() {
2087-
this(false);
2088-
}
2089-
2090-
public BuildAndSaveHandler(boolean verbose) {
2091-
this.verbose = verbose;
2092-
}
2093-
2094-
@Override
2095-
public void run() {
2096-
try {
2097-
sketch.prepare();
2098-
sketch.build(verbose, true);
2099-
statusNotice(_("Done compiling."));
2100-
} catch (PreferencesMapException e) {
2101-
statusError(I18n.format(
2102-
_("Error while compiling: missing '{0}' configuration parameter"),
2103-
e.getMessage()));
2104-
} catch (Exception e) {
2105-
status.unprogress();
2106-
statusError(e);
2107-
}
2108-
2109-
status.unprogress();
2110-
toolbar.deactivate(EditorToolbar.RUN);
2111-
}
2112-
}
21132066

21142067
class DefaultStopHandler implements Runnable {
21152068
public void run() {

app/src/processing/app/EditorToolbar.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ public void mousePressed(MouseEvent e) {
321321

322322
switch (sel) {
323323
case RUN:
324-
editor.handleRun(false);
324+
editor.handleRun(false, editor.presentHandler, editor.runHandler);
325325
break;
326326

327327
// case STOP:

0 commit comments

Comments
 (0)