44 *******************************************************************************/
55package com .espressif .idf .ui .tools .watcher ;
66
7+ import java .io .IOException ;
78import java .nio .file .Path ;
89
910import org .eclipse .jface .dialogs .MessageDialog ;
1011import org .eclipse .swt .widgets .Display ;
12+ import org .eclipse .swt .widgets .Shell ;
13+ import org .eclipse .ui .IWorkbenchWindow ;
14+ import org .eclipse .ui .PartInitException ;
15+ import org .eclipse .ui .console .MessageConsoleStream ;
16+ import org .eclipse .ui .ide .IDE ;
1117import org .osgi .service .prefs .Preferences ;
1218
19+ import com .espressif .idf .core .logging .Logger ;
20+ import com .espressif .idf .core .tools .EimIdfConfiguratinParser ;
21+ import com .espressif .idf .core .tools .SetupToolsInIde ;
22+ import com .espressif .idf .core .tools .vo .EimJson ;
1323import com .espressif .idf .core .tools .watcher .EimJsonChangeListener ;
1424import com .espressif .idf .core .tools .watcher .EimJsonStateChecker ;
25+ import com .espressif .idf .ui .IDFConsole ;
26+ import com .espressif .idf .ui .handlers .EclipseHandler ;
1527import com .espressif .idf .ui .tools .Messages ;
28+ import com .espressif .idf .ui .tools .SetupToolsJobListener ;
29+ import com .espressif .idf .ui .tools .manager .ESPIDFManagerEditor ;
30+ import com .espressif .idf .ui .tools .manager .EimEditorInput ;
31+ import com .espressif .idf .ui .tools .manager .pages .ESPIDFMainTablePage ;
1632
1733/**
1834 * eim_idf.json file ui change handler to notify user for changes.
2339public class EimJsonUiChangeHandler implements EimJsonChangeListener
2440{
2541 private Preferences preferences ;
42+ private EimJson eimJson ;
2643
2744 public EimJsonUiChangeHandler (Preferences preferences )
2845 {
@@ -32,13 +49,102 @@ public EimJsonUiChangeHandler(Preferences preferences)
3249 @ Override
3350 public void onJsonFileChanged (Path file )
3451 {
35- Display .getDefault ().asyncExec (() -> {
36- MessageDialog .openWarning (Display .getDefault ().getActiveShell (), Messages .EimJsonChangedMsgTitle ,
37- Messages .EimJsonChangedMsgDetail );
52+ int response = displayMessageToUser ();
53+ handleUserResponse (response );
54+ }
55+
56+ public int displayMessageToUser ()
57+ {
58+ final int [] response = new int [] { -1 };
59+ Display display = Display .getDefault ();
60+ display .syncExec (() -> {
61+ Shell shell = display .getActiveShell ();
62+ if (shell == null )
63+ {
64+ shell = new Shell (display );
65+ }
66+ MessageDialog messageDialog = new MessageDialog (shell , Messages .EimJsonChangedMsgTitle , null ,
67+ Messages .EimJsonChangedMsgDetail , MessageDialog .WARNING , 0 ,
68+ new String [] { Messages .MsgYes , Messages .MsgNo });
69+ response [0 ] = messageDialog .open ();
3870 });
3971
72+ return response [0 ];
73+ }
74+
75+ public void handleUserResponse (int response )
76+ {
77+ if (response == 0 )
78+ {
79+ try
80+ {
81+ loadEimJson ();
82+ if (eimJson .getIdfInstalled ().size () == 1 )
83+ {
84+ // only one entry in eimJson so we can simply refresh the IDE environment with that.
85+ setupToolsInIde ();
86+ }
87+ else
88+ {
89+ // multiple entries in json so launch manager for user to handle this
90+ launchEspIdfManager ();
91+ }
92+ }
93+ catch (
94+ IOException
95+ | PartInitException e )
96+ {
97+ Logger .log (e );
98+ }
99+ }
100+
40101 EimJsonStateChecker checker = new EimJsonStateChecker (preferences );
41102 checker .updateLastSeenTimestamp ();
42103 }
43104
105+ private void loadEimJson () throws IOException
106+ {
107+ EimIdfConfiguratinParser eimIdfConfiguratinParser = new EimIdfConfiguratinParser ();
108+ eimJson = eimIdfConfiguratinParser .getEimJson (true );
109+ }
110+
111+ private void setupToolsInIde ()
112+ {
113+ SetupToolsInIde setupToolsInIde = new SetupToolsInIde (eimJson .getIdfInstalled ().get (0 ), eimJson ,
114+ getConsoleStream (true ), getConsoleStream (false ));
115+ SetupToolsJobListener toolsActivationJobListener = new SetupToolsJobListener (ESPIDFMainTablePage .getInstance (eimJson ),
116+ setupToolsInIde );
117+ setupToolsInIde .addJobChangeListener (toolsActivationJobListener );
118+ setupToolsInIde .schedule ();
119+ }
120+
121+ private void launchEspIdfManager () throws PartInitException
122+ {
123+ Display .getDefault ().asyncExec (() -> {
124+ IWorkbenchWindow activeww = EclipseHandler .getActiveWorkbenchWindow ();
125+ if (activeww == null || activeww .getActivePage () == null )
126+ {
127+ Logger .log ("Cannot open ESP-IDF Manager. No active workbench window or active page." );
128+ return ;
129+ }
130+
131+ try
132+ {
133+ IDE .openEditor (activeww .getActivePage (), new EimEditorInput (eimJson ), ESPIDFManagerEditor .EDITOR_ID ,
134+ true );
135+ }
136+ catch (PartInitException e )
137+ {
138+ Logger .log ("Failed to open ESP-IDF Manager Editor." );
139+ Logger .log (e );
140+ }
141+ });
142+
143+ }
144+
145+ private MessageConsoleStream getConsoleStream (boolean errorStream )
146+ {
147+ IDFConsole idfConsole = new IDFConsole ();
148+ return idfConsole .getConsoleStream (Messages .IDFToolsHandler_ToolsManagerConsole , null , errorStream , true );
149+ }
44150}
0 commit comments