diff --git a/app/src/processing/app/AbstractMonitor.java b/app/src/processing/app/AbstractMonitor.java index 4394ca96401..7650968d912 100644 --- a/app/src/processing/app/AbstractMonitor.java +++ b/app/src/processing/app/AbstractMonitor.java @@ -24,6 +24,8 @@ public abstract class AbstractMonitor extends JFrame implements MessageConsumer protected JCheckBox autoscrollBox; protected JComboBox lineEndings; protected JComboBox serialRates; + private boolean monitorEnabled; + private boolean closed; public AbstractMonitor(String title) { super(title); @@ -31,6 +33,7 @@ public AbstractMonitor(String title) { addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent event) { try { + closed = true; close(); } catch (Exception e) { // ignore @@ -148,6 +151,53 @@ public void actionPerformed(ActionEvent event) { } } } + + 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) { @@ -195,6 +245,10 @@ public String getAuthorizationKey() { return null; } + public boolean isClosed() { + return closed; + } + public abstract void open() throws Exception; public abstract void close() throws Exception; diff --git a/app/src/processing/app/Editor.java b/app/src/processing/app/Editor.java index 278b548409e..de1df6dd8c8 100644 --- a/app/src/processing/app/Editor.java +++ b/app/src/processing/app/Editor.java @@ -2388,8 +2388,7 @@ public void run() { try { if (serialMonitor != null) { - serialMonitor.close(); - serialMonitor.setVisible(false); + serialMonitor.suspend(); } uploading = true; @@ -2421,7 +2420,17 @@ public void run() { 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) @@ -2430,8 +2439,7 @@ public void run() { try { if (serialMonitor != null) { - serialMonitor.close(); - serialMonitor.setVisible(false); + serialMonitor.suspend(); } uploading = true; @@ -2463,6 +2471,16 @@ public void run() { uploading = false; //toolbar.clear(); toolbar.deactivate(EditorToolbar.EXPORT); + + if (serialMonitor != null) { + try { + if (serialMonitor != null) + serialMonitor.resume(); + } + catch (SerialException e) { + statusError(e); + } + } } } @@ -2502,14 +2520,23 @@ protected boolean handleExportCheckModified() { 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 + } } } @@ -2523,6 +2550,11 @@ public void handleSerial() { serialMonitor = new UploaderAndMonitorFactory().newMonitor(port, base); 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() && !Preferences.has(serialMonitor.getAuthorizationKey())) {