37
37
import java .awt .Image ;
38
38
import java .awt .Point ;
39
39
import java .awt .event .*;
40
+ import java .awt .event .ComponentAdapter ;
41
+ import java .awt .event .ComponentEvent ;
42
+ import java .awt .event .MouseAdapter ;
43
+ import java .awt .event .MouseEvent ;
44
+ import java .awt .event .WindowAdapter ;
45
+ import java .awt .event .WindowEvent ;
46
+ import java .awt .event .WindowFocusListener ;
40
47
import java .util .Timer ;
41
48
import java .util .TimerTask ;
42
49
51
58
52
59
import cc .arduino .Constants ;
53
60
import processing .app .PreferencesData ;
61
+ import processing .app .Base ;
62
+ import processing .app .Editor ;
54
63
import processing .app .Theme ;
55
64
56
65
import java .awt .event .KeyEvent ;
@@ -66,18 +75,20 @@ public interface OptionalButtonCallbacks {
66
75
void onOptionalButton1Callback ();
67
76
void onOptionalButton2Callback ();
68
77
}
78
+ private Editor editor ;
69
79
70
- public NotificationPopup (Frame parent , HyperlinkListener hyperlinkListener ,
80
+ public NotificationPopup (Editor parent , HyperlinkListener hyperlinkListener ,
71
81
String message ) {
72
82
this (parent , hyperlinkListener , message , true , null , null , null );
83
+ editor = parent ;
73
84
}
74
85
75
- public NotificationPopup (Frame parent , HyperlinkListener hyperlinkListener ,
86
+ public NotificationPopup (Editor parent , HyperlinkListener hyperlinkListener ,
76
87
String message , boolean _autoClose ) {
77
88
this (parent , hyperlinkListener , message , _autoClose , null , null , null );
78
89
}
79
90
80
- public NotificationPopup (Frame parent , HyperlinkListener hyperlinkListener ,
91
+ public NotificationPopup (Editor parent , HyperlinkListener hyperlinkListener ,
81
92
String message , boolean _autoClose , OptionalButtonCallbacks listener , String button1Name , String button2Name ) {
82
93
super (parent , false );
83
94
@@ -88,6 +99,7 @@ public NotificationPopup(Frame parent, HyperlinkListener hyperlinkListener,
88
99
else {
89
100
autoClose = false ;
90
101
}
102
+ editor = parent ;
91
103
optionalButtonCallbacks = listener ;
92
104
93
105
setLayout (new FlowLayout ());
@@ -106,7 +118,9 @@ public NotificationPopup(Frame parent, HyperlinkListener hyperlinkListener,
106
118
text .setEditable (false );
107
119
text .setText ("<html><body style=\" font-family:sans-serif; font-size: "
108
120
+ scale (14 ) + ";\" > " + message + " </body></html>" );
109
- text .addHyperlinkListener (hyperlinkListener );
121
+ if (hyperlinkListener != null ) {
122
+ text .addHyperlinkListener (hyperlinkListener );
123
+ }
110
124
add (text );
111
125
112
126
if (button1Name != null ) {
@@ -271,4 +285,28 @@ public void run() {
271
285
setModal (true );
272
286
}
273
287
}
288
+
289
+ public void beginWhenFocused () {
290
+ if (editor .isFocused ()) {
291
+ begin ();
292
+ return ;
293
+ }
294
+ Base base = editor .getBase ();
295
+
296
+ // If the IDE is not focused wait until it is focused again to
297
+ // display the notification, this avoids the annoying side effect
298
+ // to "steal" the focus from another application.
299
+ WindowFocusListener wfl = new WindowFocusListener () {
300
+ @ Override
301
+ public void windowLostFocus (WindowEvent evt ) {
302
+ }
303
+
304
+ @ Override
305
+ public void windowGainedFocus (WindowEvent evt ) {
306
+ begin ();
307
+ base .getEditors ().forEach (e -> e .removeWindowFocusListener (this ));
308
+ }
309
+ };
310
+ base .getEditors ().forEach (e -> e .addWindowFocusListener (wfl ));
311
+ }
274
312
}
0 commit comments