1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-02-06 01:08:25 +01:00

Export sketch: refactored to reuse existing code

This commit is contained in:
Federico Fissore 2015-05-07 12:32:52 +02:00
parent 4d029de000
commit fd6d9561a8
2 changed files with 16 additions and 63 deletions

View File

@ -619,7 +619,7 @@ public class Editor extends JFrame implements RunnerListener {
JMenuItem item = newJMenuItem(_("Verify / Compile"), 'R'); JMenuItem item = newJMenuItem(_("Verify / Compile"), 'R');
item.addActionListener(new ActionListener() { item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
handleRun(false); handleRun(false, Editor.this.presentHandler, Editor.this.runHandler);
} }
}); });
sketchMenu.add(item); sketchMenu.add(item);
@ -644,7 +644,7 @@ public class Editor extends JFrame implements RunnerListener {
item = newJMenuItemAlt(_("Export compiled Binary"), 'S'); item = newJMenuItemAlt(_("Export compiled Binary"), 'S');
item.addActionListener(new ActionListener() { item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
handleRunAndSave(true); handleRun(false, Editor.this.presentAndSaveHandler, Editor.this.runAndSaveHandler);
} }
}); });
sketchMenu.add(item); sketchMenu.add(item);
@ -1531,8 +1531,8 @@ public class Editor extends JFrame implements RunnerListener {
public void resetHandlers() { public void resetHandlers() {
runHandler = new BuildHandler(); runHandler = new BuildHandler();
presentHandler = new BuildHandler(true); presentHandler = new BuildHandler(true);
runAndSaveHandler = new BuildAndSaveHandler(); runAndSaveHandler = new BuildHandler(false, true);
presentAndSaveHandler = new BuildAndSaveHandler(true); presentAndSaveHandler = new BuildHandler(true, true);
stopHandler = new DefaultStopHandler(); stopHandler = new DefaultStopHandler();
exportHandler = new DefaultExportHandler(); exportHandler = new DefaultExportHandler();
exportAppHandler = new DefaultExportAppHandler(); exportAppHandler = new DefaultExportAppHandler();
@ -1999,8 +1999,10 @@ public class Editor extends JFrame implements RunnerListener {
/** /**
* Implements Sketch → Run. * Implements Sketch → Run.
* @param verbose Set true to run with verbose output. * @param verbose Set true to run with verbose output.
* @param verboseHandler
* @param nonVerboseHandler
*/ */
public void handleRun(final boolean verbose) { public void handleRun(final boolean verbose, Runnable verboseHandler, Runnable nonVerboseHandler) {
internalCloseRunner(); internalCloseRunner();
if (PreferencesData.getBoolean("editor.save_on_verify")) { if (PreferencesData.getBoolean("editor.save_on_verify")) {
if (sketch.isModified() && !sketch.isReadOnly()) { if (sketch.isModified() && !sketch.isReadOnly()) {
@ -2021,81 +2023,32 @@ public class Editor extends JFrame implements RunnerListener {
// Cannot use invokeLater() here, otherwise it gets // Cannot use invokeLater() here, otherwise it gets
// placed on the event thread and causes a hang--bad idea all around. // placed on the event thread and causes a hang--bad idea all around.
new Thread(verbose ? presentHandler : runHandler).start(); new Thread(verbose ? verboseHandler : nonVerboseHandler).start();
}
/**
* Implements Sketch → Run and Save.
* @param verbose Set true to run with verbose output.
*/
public void handleRunAndSave(final boolean verbose) {
internalCloseRunner();
running = true;
toolbar.activate(EditorToolbar.RUN);
status.progress(_("Compiling sketch..."));
// do this to advance/clear the terminal window / dos prompt / etc
for (int i = 0; i < 10; i++) System.out.println();
// clear the console on each run, unless the user doesn't want to
if (Preferences.getBoolean("console.auto_clear")) {
console.clear();
}
// Cannot use invokeLater() here, otherwise it gets
// placed on the event thread and causes a hang--bad idea all around.
new Thread(verbose ? presentAndSaveHandler : runAndSaveHandler).start();
} }
class BuildHandler implements Runnable { class BuildHandler implements Runnable {
private final boolean verbose; private final boolean verbose;
private final boolean saveHex;
public BuildHandler() { public BuildHandler() {
this(false); this(false);
} }
public BuildHandler(boolean verbose) { public BuildHandler(boolean verbose) {
this(verbose, false);
}
public BuildHandler(boolean verbose, boolean saveHex) {
this.verbose = verbose; this.verbose = verbose;
this.saveHex = saveHex;
} }
@Override @Override
public void run() { public void run() {
try { try {
sketch.prepare(); sketch.prepare();
sketch.build(verbose, false); sketch.build(verbose, saveHex);
statusNotice(_("Done compiling."));
} catch (PreferencesMapException e) {
statusError(I18n.format(
_("Error while compiling: missing '{0}' configuration parameter"),
e.getMessage()));
} catch (Exception e) {
status.unprogress();
statusError(e);
}
status.unprogress();
toolbar.deactivate(EditorToolbar.RUN);
}
}
class BuildAndSaveHandler implements Runnable {
private final boolean verbose;
public BuildAndSaveHandler() {
this(false);
}
public BuildAndSaveHandler(boolean verbose) {
this.verbose = verbose;
}
@Override
public void run() {
try {
sketch.prepare();
sketch.build(verbose, true);
statusNotice(_("Done compiling.")); statusNotice(_("Done compiling."));
} catch (PreferencesMapException e) { } catch (PreferencesMapException e) {
statusError(I18n.format( statusError(I18n.format(

View File

@ -321,7 +321,7 @@ public class EditorToolbar extends JComponent implements MouseInputListener, Key
switch (sel) { switch (sel) {
case RUN: case RUN:
editor.handleRun(false); editor.handleRun(false, editor.presentHandler, editor.runHandler);
break; break;
// case STOP: // case STOP: