From c02d3585f6df2d2c0fc846370a9c30ad161ee167 Mon Sep 17 00:00:00 2001 From: joelmoniz Date: Mon, 2 Jun 2014 23:49:55 +0530 Subject: [PATCH] Basic fnctionality of autosave before running TODO: Add preferences Improve UI --- .../mode/experimental/DebugEditor.java | 80 ++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/pdex/src/processing/mode/experimental/DebugEditor.java b/pdex/src/processing/mode/experimental/DebugEditor.java index 3e4041e70..7ab11b682 100755 --- a/pdex/src/processing/mode/experimental/DebugEditor.java +++ b/pdex/src/processing/mode/experimental/DebugEditor.java @@ -48,6 +48,7 @@ import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.SwingUtilities; +import javax.swing.SwingWorker; import javax.swing.border.EtchedBorder; import javax.swing.table.TableModel; import javax.swing.text.Document; @@ -921,7 +922,7 @@ public class DebugEditor extends JavaEditor implements ActionListener { vi.setTitle(getSketch().getName()); } // if file location has changed, update autosaver - autosaver.reloadAutosaveDir(); +// autosaver.reloadAutosaveDir(); return saved; } @@ -1071,6 +1072,83 @@ public class DebugEditor extends JavaEditor implements ActionListener { return ta; } + /** + * Grab current contents of the sketch window, advance the console, stop any + * other running sketches, auto-save the user's code... not in that order. + */ + @Override + public void prepareRun() { + super.prepareRun(); + try { + if (sketch.isUntitled()) { + if (handleSave(true)) + statusTimedNotice("Saved. Running...", 5); + else + statusTimedNotice("Save Canceled. Running anyway...", 5); + } + else if (sketch.isModified())// TODO: Fix ugly UI + // TODO: Add to preferences + { + Object[] options = { "Save", "Continue Without Saving" }; + int op = JOptionPane + .showOptionDialog( + new Frame(), + "There are unsaved changes in your sketch. Save before proceeding?", + this.getSketch().getName(), JOptionPane.YES_NO_OPTION, + JOptionPane.PLAIN_MESSAGE, null, options, options[0]); + if (op == JOptionPane.YES_OPTION) { + if (handleSave(true)) + statusTimedNotice("Saved. Running...", 5); + } + else + if (op == JOptionPane.NO_OPTION) + statusTimedNotice("Not saved. Running...", 5); + } + } catch (Exception e) { + // show the error as a message in the window + statusError(e); + } + } + + /** + * Shows a notice message in the editor status bar for a certain duration of + * time. + * + * @param msg + * @param secs + */ + public void statusTimedNotice(final String msg, final int secs) { +// EventQueue.invokeLater(new Runnable() { +// +// @Override +// public void run() { +// statusNotice(msg); +// try { +// Thread.sleep(secs * 1000); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// statusEmpty(); +// +// } +// }); + SwingWorker s = new SwingWorker() { + + @Override + protected Void doInBackground() throws Exception { + statusNotice(msg); + try { + Thread.sleep(secs * 1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + statusEmpty(); + return null; + } + }; + s.execute(); + } + /** * Access variable inspector window. *