4
4
*******************************************************************************/
5
5
package com .espressif .idf .ui .tools .watcher ;
6
6
7
+ import java .io .IOException ;
7
8
import java .nio .file .Path ;
8
9
9
- import org .eclipse .jface . dialogs . MessageDialog ;
10
+ import org .eclipse .swt . SWT ;
10
11
import org .eclipse .swt .widgets .Display ;
12
+ import org .eclipse .swt .widgets .MessageBox ;
13
+ import org .eclipse .swt .widgets .Shell ;
14
+ import org .eclipse .ui .IWorkbenchWindow ;
15
+ import org .eclipse .ui .PartInitException ;
16
+ import org .eclipse .ui .console .MessageConsoleStream ;
17
+ import org .eclipse .ui .ide .IDE ;
11
18
import org .osgi .service .prefs .Preferences ;
12
19
20
+ import com .espressif .idf .core .logging .Logger ;
21
+ import com .espressif .idf .core .tools .EimIdfConfiguratinParser ;
22
+ import com .espressif .idf .core .tools .SetupToolsInIde ;
23
+ import com .espressif .idf .core .tools .vo .EimJson ;
13
24
import com .espressif .idf .core .tools .watcher .EimJsonChangeListener ;
14
25
import com .espressif .idf .core .tools .watcher .EimJsonStateChecker ;
26
+ import com .espressif .idf .ui .IDFConsole ;
27
+ import com .espressif .idf .ui .handlers .EclipseHandler ;
15
28
import com .espressif .idf .ui .tools .Messages ;
29
+ import com .espressif .idf .ui .tools .manager .ESPIDFManagerEditor ;
30
+ import com .espressif .idf .ui .tools .manager .EimEditorInput ;
16
31
17
32
/**
18
33
* eim_idf.json file ui change handler to notify user for changes.
23
38
public class EimJsonUiChangeHandler implements EimJsonChangeListener
24
39
{
25
40
private Preferences preferences ;
26
-
41
+ private EimJson eimJson ;
42
+
27
43
public EimJsonUiChangeHandler (Preferences preferences )
28
44
{
29
45
this .preferences = preferences ;
@@ -32,13 +48,99 @@ public EimJsonUiChangeHandler(Preferences preferences)
32
48
@ Override
33
49
public void onJsonFileChanged (Path file )
34
50
{
35
- Display .getDefault ().asyncExec (() -> {
36
- MessageDialog .openWarning (Display .getDefault ().getActiveShell (), Messages .EimJsonChangedMsgTitle ,
37
- Messages .EimJsonChangedMsgDetail );
51
+ int response = displayMessageToUser ();
52
+ handleUserResponse (response );
53
+ }
54
+
55
+ public int displayMessageToUser ()
56
+ {
57
+ final int [] response = new int [] {-1 };
58
+ Display display = Display .getDefault ();
59
+ display .syncExec (() -> {
60
+ Shell shell = display .getActiveShell ();
61
+ if (shell == null )
62
+ {
63
+ shell = new Shell (display );
64
+ }
65
+ MessageBox messageBox = new MessageBox (shell , SWT .ICON_WARNING | SWT .YES | SWT .NO );
66
+ messageBox .setText (Messages .EimJsonChangedMsgTitle );
67
+ messageBox .setMessage (Messages .EimJsonChangedMsgDetail );
68
+ response [0 ] = messageBox .open ();
38
69
});
70
+
71
+ return response [0 ];
72
+ }
39
73
74
+ public void handleUserResponse (int response )
75
+ {
76
+ if (response == SWT .YES )
77
+ {
78
+ try
79
+ {
80
+ loadEimJson ();
81
+ if (eimJson .getIdfInstalled ().size () == 1 )
82
+ {
83
+ // only one entry in eimJson so we can simply refresh the IDE environment with that.
84
+ setupToolsInIde ();
85
+ }
86
+ else
87
+ {
88
+ // multiple entries in json so launch manager for user to handle this
89
+ launchEspIdfManager ();
90
+ }
91
+ }
92
+ catch (IOException | PartInitException e )
93
+ {
94
+ Logger .log (e );
95
+ }
96
+ }
97
+
40
98
EimJsonStateChecker checker = new EimJsonStateChecker (preferences );
41
99
checker .updateLastSeenTimestamp ();
42
100
}
101
+
102
+ private void loadEimJson () throws IOException
103
+ {
104
+ EimIdfConfiguratinParser eimIdfConfiguratinParser = new EimIdfConfiguratinParser ();
105
+ eimJson = eimIdfConfiguratinParser .getEimJson (true );
106
+ }
107
+
108
+
109
+
110
+ private void setupToolsInIde ()
111
+ {
112
+ SetupToolsInIde setupToolsInIde = new SetupToolsInIde (eimJson .getIdfInstalled ().get (0 ), eimJson , getConsoleStream (true ), getConsoleStream (false ));
113
+ setupToolsInIde .schedule ();
114
+ }
115
+
116
+ private void launchEspIdfManager () throws PartInitException
117
+ {
118
+ Display .getDefault ().asyncExec (() -> {
119
+ IWorkbenchWindow activeww = EclipseHandler .getActiveWorkbenchWindow ();
120
+ if (activeww != null && activeww .getActivePage () != null )
121
+ {
122
+ try
123
+ {
124
+ IDE .openEditor (activeww .getActivePage (), new EimEditorInput (eimJson ),
125
+ ESPIDFManagerEditor .EDITOR_ID , true );
126
+ }
127
+ catch (PartInitException e )
128
+ {
129
+ Logger .log ("Failed to open ESP-IDF Manager Editor." );
130
+ Logger .log (e );
131
+ }
132
+ }
133
+ else
134
+ {
135
+ Logger .log ("Cannot open ESP-IDF Manager Editor. No active workbench window yet." );
136
+ }
137
+ });
43
138
139
+ }
140
+
141
+ private MessageConsoleStream getConsoleStream (boolean errorStream )
142
+ {
143
+ IDFConsole idfConsole = new IDFConsole ();
144
+ return idfConsole .getConsoleStream (Messages .IDFToolsHandler_ToolsManagerConsole , null , errorStream , true );
145
+ }
44
146
}
0 commit comments