diff --git a/app/src/processing/app/AbstractMonitor.java b/app/src/processing/app/AbstractMonitor.java index 6469809ba..97fd79324 100644 --- a/app/src/processing/app/AbstractMonitor.java +++ b/app/src/processing/app/AbstractMonitor.java @@ -44,6 +44,8 @@ public abstract class AbstractMonitor extends JFrame implements ActionListener { protected JCheckBox autoscrollBox; protected JComboBox lineEndings; protected JComboBox serialRates; + private boolean monitorEnabled; + private boolean closed; private Timer updateTimer; private StringBuffer updateBuffer; @@ -54,6 +56,7 @@ public abstract class AbstractMonitor extends JFrame implements ActionListener { addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent event) { try { + closed = true; close(); } catch (Exception e) { // ignore @@ -173,10 +176,57 @@ public abstract class AbstractMonitor extends JFrame implements ActionListener { } } } - + updateBuffer = new StringBuffer(1048576); updateTimer = new Timer(33, this); // redraw serial monitor at 30 Hz updateTimer.start(); + + monitorEnabled = true; + closed = false; + } + + public void enableWindow(boolean enable) + { + textArea.setEnabled(enable); + scrollPane.setEnabled(enable); + textField.setEnabled(enable); + sendButton.setEnabled(enable); + autoscrollBox.setEnabled(enable); + lineEndings.setEnabled(enable); + serialRates.setEnabled(enable); + + monitorEnabled = enable; + } + + // Puts the window in suspend state, closing the serial port + // to allow other entity (the programmer) to use it + public void suspend() + { + enableWindow(false); + + try { + close(); + } + catch(Exception e) { + //throw new SerialException("Failed closing the port"); + } + + } + + public void resume() throws SerialException + { + // Enable the window + enableWindow(true); + + // If the window is visible, try to open the serial port + if (isVisible()) + try { + open(); + } + catch(Exception e) { + throw new SerialException("Failed opening the port"); + } + } public void onSerialRateChange(ActionListener listener) { @@ -224,10 +274,14 @@ public abstract class AbstractMonitor extends JFrame implements ActionListener { return null; } + public boolean isClosed() { + return closed; + } + public abstract void open() throws Exception; public abstract void close() throws Exception; - + public synchronized void addToUpdateBuffer(char buff[], int n) { updateBuffer.append(buff, 0, n); } diff --git a/app/src/processing/app/Editor.java b/app/src/processing/app/Editor.java index 954c539ba..798d2bdb4 100644 --- a/app/src/processing/app/Editor.java +++ b/app/src/processing/app/Editor.java @@ -2517,8 +2517,7 @@ public class Editor extends JFrame implements RunnerListener { try { if (serialMonitor != null) { - serialMonitor.close(); - serialMonitor.setVisible(false); + serialMonitor.suspend(); } uploading = true; @@ -2550,7 +2549,17 @@ public class Editor extends JFrame implements RunnerListener { uploading = false; //toolbar.clear(); toolbar.deactivate(EditorToolbar.EXPORT); - } + + // Return the serial monitor window to its initial state + try { + if (serialMonitor != null) + serialMonitor.resume(); + } + catch (SerialException e) { + statusError(e); + } + + } } // DAM: in Arduino, this is upload (with verbose output) @@ -2559,8 +2568,7 @@ public class Editor extends JFrame implements RunnerListener { try { if (serialMonitor != null) { - serialMonitor.close(); - serialMonitor.setVisible(false); + serialMonitor.suspend(); } uploading = true; @@ -2592,6 +2600,16 @@ public class Editor extends JFrame implements RunnerListener { uploading = false; //toolbar.clear(); toolbar.deactivate(EditorToolbar.EXPORT); + + if (serialMonitor != null) { + try { + if (serialMonitor != null) + serialMonitor.resume(); + } + catch (SerialException e) { + statusError(e); + } + } } } @@ -2631,14 +2649,23 @@ public class Editor extends JFrame implements RunnerListener { public void handleSerial() { - if (uploading) return; - if (serialMonitor != null) { - try { - serialMonitor.close(); - serialMonitor.setVisible(false); - } catch (Exception e) { - // noop + // The serial monitor already exists + + if (serialMonitor.isClosed()) { + // If it's closed, clear the refrence to the existing + // monitor and create a new one + serialMonitor = null; + } + else { + // If it's not closed, give it the focus + try { + serialMonitor.toFront(); + serialMonitor.requestFocus(); + return; + } catch (Exception e) { + // noop + } } } @@ -2652,6 +2679,11 @@ public class Editor extends JFrame implements RunnerListener { serialMonitor = new MonitorFactory().newMonitor(port); serialMonitor.setIconImage(getIconImage()); + // If currently uploading, disable the monitor (it will be later + // enabled when done uploading) + if (uploading) + serialMonitor.suspend(); + boolean success = false; do { if (serialMonitor.requiresAuthorization() && !PreferencesData.has(serialMonitor.getAuthorizationKey())) {