4242import java .awt .event .MouseEvent ;
4343import java .awt .event .WindowAdapter ;
4444import java .awt .event .WindowEvent ;
45+ import java .awt .event .WindowFocusListener ;
4546import java .util .Timer ;
4647import java .util .TimerTask ;
4748
5556import javax .swing .event .HyperlinkListener ;
5657
5758import cc .arduino .Constants ;
59+ import processing .app .Base ;
60+ import processing .app .Editor ;
5861import processing .app .Theme ;
5962
6063public class NotificationPopup extends JDialog {
6164
6265 private Timer autoCloseTimer = new Timer (false );
6366 private boolean autoClose = true ;
67+ private Editor editor ;
6468
65- public NotificationPopup (Frame parent , HyperlinkListener hyperlinkListener ,
69+ public NotificationPopup (Editor parent , HyperlinkListener hyperlinkListener ,
6670 String message ) {
6771 this (parent , hyperlinkListener , message , true );
72+ editor = parent ;
6873 }
6974
70- public NotificationPopup (Frame parent , HyperlinkListener hyperlinkListener ,
75+ public NotificationPopup (Editor parent , HyperlinkListener hyperlinkListener ,
7176 String message , boolean _autoClose ) {
7277 super (parent , false );
78+ editor = parent ;
7379 autoClose = _autoClose ;
7480 setLayout (new FlowLayout ());
7581 setDefaultCloseOperation (WindowConstants .DISPOSE_ON_CLOSE );
@@ -87,7 +93,9 @@ public NotificationPopup(Frame parent, HyperlinkListener hyperlinkListener,
8793 text .setEditable (false );
8894 text .setText ("<html><body style=\" font-family:sans-serif; font-size: "
8995 + scale (14 ) + ";\" > " + message + " </body></html>" );
90- text .addHyperlinkListener (hyperlinkListener );
96+ if (hyperlinkListener != null ) {
97+ text .addHyperlinkListener (hyperlinkListener );
98+ }
9199 add (text );
92100
93101 Image close = Theme .getThemeImage ("close" , this , scale (22 ), scale (22 ));
@@ -159,4 +167,28 @@ public void run() {
159167 }
160168 setVisible (true );
161169 }
170+
171+ public void beginWhenFocused () {
172+ if (editor .isFocused ()) {
173+ begin ();
174+ return ;
175+ }
176+ Base base = editor .getBase ();
177+
178+ // If the IDE is not focused wait until it is focused again to
179+ // display the notification, this avoids the annoying side effect
180+ // to "steal" the focus from another application.
181+ WindowFocusListener wfl = new WindowFocusListener () {
182+ @ Override
183+ public void windowLostFocus (WindowEvent evt ) {
184+ }
185+
186+ @ Override
187+ public void windowGainedFocus (WindowEvent evt ) {
188+ begin ();
189+ base .getEditors ().forEach (e -> e .removeWindowFocusListener (this ));
190+ }
191+ };
192+ base .getEditors ().forEach (e -> e .addWindowFocusListener (wfl ));
193+ }
162194}
0 commit comments