From 46641780bfbe77b213af1d90652bc99f52c683d7 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 12 Oct 2017 11:07:28 +0200 Subject: [PATCH] Refactored notification popup --- .../cc/arduino/view/NotificationPopup.java | 46 +++++++++++++++++-- app/src/processing/app/Editor.java | 4 ++ app/src/processing/app/NewBoardListener.java | 25 +--------- 3 files changed, 47 insertions(+), 28 deletions(-) diff --git a/app/src/cc/arduino/view/NotificationPopup.java b/app/src/cc/arduino/view/NotificationPopup.java index 2de079c85..2e6e294dc 100644 --- a/app/src/cc/arduino/view/NotificationPopup.java +++ b/app/src/cc/arduino/view/NotificationPopup.java @@ -37,6 +37,13 @@ import java.awt.Frame; import java.awt.Image; import java.awt.Point; import java.awt.event.*; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.awt.event.WindowFocusListener; import java.util.Timer; import java.util.TimerTask; @@ -51,6 +58,8 @@ import javax.swing.event.HyperlinkListener; import cc.arduino.Constants; import processing.app.PreferencesData; +import processing.app.Base; +import processing.app.Editor; import processing.app.Theme; import java.awt.event.KeyEvent; @@ -66,18 +75,20 @@ public class NotificationPopup extends JDialog { void onOptionalButton1Callback(); void onOptionalButton2Callback(); } + private Editor editor; - public NotificationPopup(Frame parent, HyperlinkListener hyperlinkListener, + public NotificationPopup(Editor parent, HyperlinkListener hyperlinkListener, String message) { this(parent, hyperlinkListener, message, true, null, null, null); + editor = parent; } - public NotificationPopup(Frame parent, HyperlinkListener hyperlinkListener, + public NotificationPopup(Editor parent, HyperlinkListener hyperlinkListener, String message, boolean _autoClose) { this(parent, hyperlinkListener, message, _autoClose, null, null, null); } - public NotificationPopup(Frame parent, HyperlinkListener hyperlinkListener, + public NotificationPopup(Editor parent, HyperlinkListener hyperlinkListener, String message, boolean _autoClose, OptionalButtonCallbacks listener, String button1Name, String button2Name) { super(parent, false); @@ -88,6 +99,7 @@ public class NotificationPopup extends JDialog { else { autoClose = false; } + editor = parent; optionalButtonCallbacks = listener; setLayout(new FlowLayout()); @@ -106,7 +118,9 @@ public class NotificationPopup extends JDialog { text.setEditable(false); text.setText(" " + message + " "); - text.addHyperlinkListener(hyperlinkListener); + if (hyperlinkListener != null) { + text.addHyperlinkListener(hyperlinkListener); + } add(text); if (button1Name != null) { @@ -271,4 +285,28 @@ public class NotificationPopup extends JDialog { setModal(true); } } + + public void beginWhenFocused() { + if (editor.isFocused()) { + begin(); + return; + } + Base base = editor.getBase(); + + // If the IDE is not focused wait until it is focused again to + // display the notification, this avoids the annoying side effect + // to "steal" the focus from another application. + WindowFocusListener wfl = new WindowFocusListener() { + @Override + public void windowLostFocus(WindowEvent evt) { + } + + @Override + public void windowGainedFocus(WindowEvent evt) { + begin(); + base.getEditors().forEach(e -> e.removeWindowFocusListener(this)); + } + }; + base.getEditors().forEach(e -> e.addWindowFocusListener(wfl)); + } } diff --git a/app/src/processing/app/Editor.java b/app/src/processing/app/Editor.java index 885f3379a..d6cca158b 100644 --- a/app/src/processing/app/Editor.java +++ b/app/src/processing/app/Editor.java @@ -403,6 +403,10 @@ public class Editor extends JFrame implements RunnerListener { EditorConsole.setCurrentEditorConsole(console); } + public Base getBase() { + return base; + } + /** * Handles files dragged & dropped from the desktop and into the editor * window. Dragging files into the editor window is the same as using diff --git a/app/src/processing/app/NewBoardListener.java b/app/src/processing/app/NewBoardListener.java index 7e0fe61d7..423262df2 100644 --- a/app/src/processing/app/NewBoardListener.java +++ b/app/src/processing/app/NewBoardListener.java @@ -73,34 +73,11 @@ public class NewBoardListener implements PropertyChangeListener, Runnable { } SwingUtilities.invokeLater(() -> { - ed = base.getActiveEditor(); NotificationPopup notificationPopup = new NotificationPopup(ed, new UpdatableBoardsLibsFakeURLsHandler(base), newBoardManagerLink, false); - if (ed.isFocused()) { - notificationPopup.begin(); - return; - } - - // If the IDE is not focused wait until it is focused again to - // display the notification, this avoids the annoying side effect - // to "steal" the focus from another application. - WindowFocusListener wfl = new WindowFocusListener() { - @Override - public void windowLostFocus(WindowEvent evt) { - } - - @Override - public void windowGainedFocus(WindowEvent evt) { - notificationPopup.begin(); - for (Editor e : base.getEditors()) - e.removeWindowFocusListener(this); - } - }; - - for (Editor e : base.getEditors()) - e.addWindowFocusListener(wfl); + notificationPopup.beginWhenFocused(); }); } }