2424import org .eclipse .ui .PlatformUI ;
2525
2626import com .microsoft .azuretools .azurecommons .helpers .NotNull ;
27+ import com .microsoft .azuretools .azurecommons .helpers .Nullable ;
2728import com .microsoft .azuretools .hdinsight .Activator ;
2829import com .microsoft .azuretools .hdinsight .SparkSubmissionToolWindowView ;
30+
31+ import rx .subjects .ReplaySubject ;
32+
2933import com .microsoft .azure .hdinsight .common .MessageInfoType ;
3034import static com .microsoft .azure .hdinsight .common .MessageInfoType .*;
3135
36+ import java .util .AbstractMap .SimpleImmutableEntry ;
37+ import java .util .Optional ;
38+
3239public class HDInsightUtil {
3340 private static SparkSubmissionToolWindowView sparkSubmissionToolWindowView ;
34-
41+
42+ // The replay subject for the message showed in HDInsight tool window
43+ // The replay subject will replay all notifications before the initialization is
44+ // done
45+ // The replay buffer size is 1MB.
46+ @ NotNull
47+ @ SuppressWarnings ("null" )
48+ private static ReplaySubject <SimpleImmutableEntry <MessageInfoType , String >> toolWindowMessageSubject =
49+ ReplaySubject .create (1024 * 1024 );
50+
51+ @ NotNull
52+ public static ReplaySubject <SimpleImmutableEntry <MessageInfoType , String >> getToolWindowMessageSubject () {
53+ return toolWindowMessageSubject ;
54+ }
55+
56+ @ Nullable
3557 public static SparkSubmissionToolWindowView getSparkSubmissionToolWindowView () {
3658 if (sparkSubmissionToolWindowView == null ) {
3759 Display .getDefault ().syncExec (new Runnable () {
@@ -41,6 +63,9 @@ public void run() {
4163 sparkSubmissionToolWindowView = (SparkSubmissionToolWindowView ) PlatformUI .getWorkbench ()
4264 .getActiveWorkbenchWindow ().getActivePage ()
4365 .showView (SparkSubmissionToolWindowView .class .getName ());
66+
67+ Optional .ofNullable (sparkSubmissionToolWindowView )
68+ .ifPresent (view -> resetToolWindowMessageSubject (view ));
4469 } catch (PartInitException e ) {
4570 Activator .getDefault ().log (e .getMessage (), e );
4671 }
@@ -54,15 +79,15 @@ public static synchronized void setHyperLinkWithText(final String text, final St
5479 Display .getDefault ().syncExec (new Runnable () {
5580 @ Override
5681 public void run () {
57- getSparkSubmissionToolWindowView ().setHyperLinkWithText (text , hyperlinkUrl , anchorText );
82+ SparkSubmissionToolWindowView view = getSparkSubmissionToolWindowView ();
83+
84+ if (view != null ) {
85+ view .setHyperLinkWithText (text , hyperlinkUrl , anchorText );
86+ }
5887 }
5988 });
6089 }
6190
62- public static void showInfoOnSubmissionMessageWindow (/*@NotNull */ final String message , boolean isNeedClear ) {
63- showInfoOnSubmissionMessageWindow (Info , message , isNeedClear );
64- }
65-
6691 public static void showInfoOnSubmissionMessageWindow (@ NotNull final String message ) {
6792 showInfoOnSubmissionMessageWindow (Info , message , false );
6893 }
@@ -75,30 +100,52 @@ public static void showWarningMessageOnSubmissionMessageWindow(@NotNull final St
75100 showInfoOnSubmissionMessageWindow (Warning , message , false );
76101 }
77102
78- private static void showInfoOnSubmissionMessageWindow (@ NotNull final MessageInfoType type , @ NotNull final String message , @ NotNull final boolean isNeedClear ) {
103+ private static void showInfoOnSubmissionMessageWindow (@ NotNull final MessageInfoType type ,
104+ @ NotNull final String message ,
105+ final boolean isNeedClear ) {
79106 Display .getDefault ().syncExec (new Runnable () {
80107 @ Override
81108 public void run () {
82- showSubmissionMessage (getSparkSubmissionToolWindowView (), message , type , isNeedClear );
109+ showSubmissionMessage (getSparkSubmissionToolWindowView (), message , type , isNeedClear );
83110 }
84111 });
85112 }
86113
87- private static void showSubmissionMessage ( @ NotNull SparkSubmissionToolWindowView sparkSubmissionView , @ NotNull String message , @ NotNull MessageInfoType type , @ NotNull final boolean isNeedClear ) {
88- if (isNeedClear ) {
89- sparkSubmissionView . clearAll () ;
114+ private static void resetToolWindowMessageSubject ( @ Nullable SparkSubmissionToolWindowView sparkSubmissionToolWindowView ) {
115+ if (sparkSubmissionToolWindowView == null ) {
116+ return ;
90117 }
91118
92- switch (type ) {
119+ toolWindowMessageSubject = ReplaySubject .create (1024 * 1024 );
120+
121+ getToolWindowMessageSubject ().subscribe (entry -> {
122+ String message = entry .getValue ();
123+
124+ switch (entry .getKey ()) {
93125 case Error :
94- sparkSubmissionView .setError (message );
126+ sparkSubmissionToolWindowView .setError (message );
95127 break ;
96128 case Info :
97- sparkSubmissionView .setInfo (message );
129+ sparkSubmissionToolWindowView .setInfo (message );
98130 break ;
99131 case Warning :
100- sparkSubmissionView .setWarning (message );
132+ sparkSubmissionToolWindowView .setWarning (message );
133+ break ;
134+ default :
101135 break ;
136+ }
137+ });
138+ }
139+
140+ private static void showSubmissionMessage (@ Nullable SparkSubmissionToolWindowView sparkSubmissionView ,
141+ @ NotNull String message ,
142+ @ NotNull MessageInfoType type ,
143+ final boolean isNeedClear ) {
144+ if (isNeedClear && sparkSubmissionView != null ) {
145+ sparkSubmissionView .clearAll ();
146+ resetToolWindowMessageSubject (sparkSubmissionView );
102147 }
148+
149+ getToolWindowMessageSubject ().onNext (new SimpleImmutableEntry <>(type , message ));
103150 }
104151}
0 commit comments