44 *******************************************************************************/
55package com .espressif .idf .ui .update ;
66
7+ import java .io .BufferedReader ;
8+ import java .io .InputStreamReader ;
79import java .util .Map ;
810
9- import org .eclipse .core .runtime .Platform ;
1011import org .eclipse .core .runtime .preferences .ConfigurationScope ;
1112import org .eclipse .jface .dialogs .Dialog ;
1213import org .eclipse .jface .dialogs .IDialogConstants ;
1920import org .eclipse .swt .layout .GridData ;
2021import org .eclipse .swt .layout .GridLayout ;
2122import org .eclipse .swt .widgets .Button ;
22- import org .eclipse .swt .widgets .Combo ;
2323import org .eclipse .swt .widgets .Composite ;
2424import org .eclipse .swt .widgets .Control ;
2525import org .eclipse .swt .widgets .DirectoryDialog ;
4141public class DirectorySelectionDialog extends TitleAreaDialog
4242{
4343
44- private Shell shell ;
4544 private Text text ;
4645 private String idfDirPath ;
4746 private String pythonExecutablePath ;
48- private Combo pythonVersionCombo ;
4947 private Map <String , String > pythonVersions ;
5048 private String gitPath ;
5149 private Text gitLocationtext ;
5250 private Text pythonLocationtext ;
5351 private String commandId ;
5452 private static final String pythonPathNodeKey = "PYTHON_EXECUTABLE" ; //$NON-NLS-1$
5553 private static final String gitPathNodeKey = "GIT_EXECUTABLE" ; //$NON-NLS-1$
56-
54+
5755 protected DirectorySelectionDialog (Shell parentShell , String commandId , String pythonExecutablePath ,
5856 Map <String , String > pythonVersions , String idfPath , String gitExecutablePath )
5957 {
6058 super (parentShell );
6159 setShellStyle (getShellStyle () | SWT .RESIZE );
62- this .shell = parentShell ;
6360 this .pythonExecutablePath = getPythonPreferenceOrDefault (pythonExecutablePath );
6461 this .pythonVersions = pythonVersions ;
6562 this .idfDirPath = idfPath ;
@@ -154,76 +151,65 @@ public void widgetSelected(SelectionEvent event)
154151 });
155152
156153 // Python version selection
157- if (Platform .OS_WIN32 .equals (Platform .getOS ()) && pythonVersions != null && !pythonVersions .isEmpty ())
158- {
159- new Label (composite , SWT .NONE ).setText (Messages .DirectorySelectionDialog_ChoosePyVersion );
154+ addPythonVersionSelectionControls (composite );
160155
161- pythonVersionCombo = new Combo (composite , SWT .DROP_DOWN | SWT .READ_ONLY );
162- GridData gridData = new GridData (SWT .NONE , SWT .NONE , true , false , 2 , 1 );
163- gridData .widthHint = 250 ;
164- pythonVersionCombo .setLayoutData (gridData );
156+ Dialog .applyDialogFont (composite );
157+ return composite ;
158+ }
165159
166- String [] versions = pythonVersions .keySet ().toArray (new String [pythonVersions .size ()]);
167- pythonVersionCombo .setItems (versions );
168- pythonVersionCombo .select (0 ); // select the first one
160+ private void addPythonVersionSelectionControls (Composite composite )
161+ {
162+ // Python executable location
163+ new Label (composite , SWT .NONE ).setText (Messages .DirectorySelectionDialog_PyExeLocation );
169164
170- }
171- else
165+ pythonLocationtext = new Text (composite , SWT .BORDER );
166+ GridData data = new GridData ();
167+ data .widthHint = convertHorizontalDLUsToPixels (IDialogConstants .ENTRY_FIELD_WIDTH );
168+ pythonLocationtext .setLayoutData (data );
169+
170+ pythonLocationtext .setText (pythonExecutablePath != null ? pythonExecutablePath : StringUtil .EMPTY );
171+ pythonLocationtext .addModifyListener (new ModifyListener ()
172172 {
173- new Label (composite , SWT .NONE ).setText (Messages .DirectorySelectionDialog_PyExeLocation );
174-
175- pythonLocationtext = new Text (composite , SWT .BORDER );
176- data = new GridData ();
177- data .widthHint = convertHorizontalDLUsToPixels (IDialogConstants .ENTRY_FIELD_WIDTH );
178- pythonLocationtext .setLayoutData (data );
179- pythonLocationtext .setText (pythonExecutablePath != null ? pythonExecutablePath : StringUtil .EMPTY );
180- pythonLocationtext .addModifyListener (new ModifyListener ()
173+ @ Override
174+ public void modifyText (ModifyEvent e )
181175 {
182- @ Override
183- public void modifyText (ModifyEvent e )
184- {
185- validate ();
186- }
187- });
176+ validate ();
177+ }
178+ });
188179
189- Button pyBrowseBtn = new Button (composite , SWT .PUSH );
190- pyBrowseBtn .setText (Messages .DirectorySelectionDialog_Browse );
191- pyBrowseBtn .addSelectionListener (new SelectionAdapter ()
180+ Button pyBrowseBtn = new Button (composite , SWT .PUSH );
181+ pyBrowseBtn .setText (Messages .DirectorySelectionDialog_Browse );
182+ pyBrowseBtn .addSelectionListener (new SelectionAdapter ()
183+ {
184+ @ Override
185+ public void widgetSelected (SelectionEvent event )
192186 {
193- @ Override
194- public void widgetSelected (SelectionEvent event )
187+ FileDialog dlg = new FileDialog (Display .getDefault ().getActiveShell ());
188+ dlg .setText (Messages .DirectorySelectionDialog_PyExecutableLocation );
189+ String pythonLocationPathString = dlg .open ();
190+ if (pythonLocationPathString != null )
195191 {
196- FileDialog dlg = new FileDialog (Display .getDefault ().getActiveShell ());
197- dlg .setText (Messages .DirectorySelectionDialog_PyExecutableLocation );
198-
199- String dir = dlg .open ();
200- if (dir != null )
201- {
202- pythonLocationtext .setText (dir );
203- }
192+ pythonLocationtext .setText (pythonLocationPathString );
204193 }
205- });
206- }
207-
208- Dialog .applyDialogFont (composite );
209- return composite ;
194+ }
195+ });
210196 }
211197
212198 protected void validate ()
213199 {
214200 idfDirPath = text .getText ();
215- if (pythonVersionCombo != null )
216- {
217- String version = pythonVersionCombo .getText ();
218- pythonExecutablePath = pythonVersions .getOrDefault (version , null );
219- }
220- else
201+ pythonExecutablePath = pythonLocationtext .getText ();
202+
203+ gitPath = gitLocationtext .getText ();
204+
205+ boolean isValidPythonPath = validatePythonExecutable (pythonExecutablePath );
206+
207+ if (!isValidPythonPath )
221208 {
222- pythonExecutablePath = pythonLocationtext .getText ();
209+ setErrorMessage (Messages .DirectorySelectionDialog_InvalidPythonPath );
210+ getButton (IDialogConstants .OK_ID ).setEnabled (false );
223211 }
224- gitPath = gitLocationtext .getText ();
225-
226- if (StringUtil .isEmpty (pythonExecutablePath ) || StringUtil .isEmpty (gitPath ) || StringUtil .isEmpty (idfDirPath ))
212+ else if (StringUtil .isEmpty (pythonExecutablePath ) || StringUtil .isEmpty (gitPath ) || StringUtil .isEmpty (idfDirPath ))
227213 {
228214 setErrorMessage (Messages .DirectorySelectionDialog_CantbeEmpty );
229215 getButton (IDialogConstants .OK_ID ).setEnabled (false );
@@ -254,12 +240,12 @@ private String getPythonPreferenceOrDefault(String pythonExecutablePath)
254240 {
255241 return getPreferences ().get (pythonPathNodeKey , pythonExecutablePath );
256242 }
257-
258- private String getGitPreferenceOrDefault (String gitExecutablePath )
243+
244+ private String getGitPreferenceOrDefault (String gitExecutablePath )
259245 {
260246 return getPreferences ().get (gitPathNodeKey , gitExecutablePath );
261247 }
262-
248+
263249 public String getIDFDirectory ()
264250 {
265251 return idfDirPath ;
@@ -279,15 +265,8 @@ public String getGitExecutable()
279265 protected void okPressed ()
280266 {
281267 idfDirPath = text .getText ();
282- if (pythonVersionCombo != null )
283- {
284- String version = pythonVersionCombo .getText ();
285- pythonExecutablePath = pythonVersions .getOrDefault (version , null );
286- }
287- else
288- {
289- pythonExecutablePath = pythonLocationtext .getText ();
290- }
268+ pythonExecutablePath = pythonLocationtext .getText ();
269+
291270 gitPath = gitLocationtext .getText ();
292271
293272 super .okPressed ();
@@ -313,4 +292,20 @@ private Preferences getPreferences()
313292 {
314293 return ConfigurationScope .INSTANCE .getNode (UIPlugin .PLUGIN_ID ).node ("preference" ); //$NON-NLS-1$
315294 }
295+
296+ private boolean validatePythonExecutable (String path )
297+ {
298+ try
299+ {
300+ Process process = new ProcessBuilder (path , "--version" ).start ();
301+ BufferedReader reader = new BufferedReader (new InputStreamReader (process .getInputStream ()));
302+ String output = reader .readLine ();
303+ int exitCode = process .waitFor ();
304+ return exitCode == 0 && output .startsWith ("Python" );
305+ }
306+ catch (Exception e )
307+ {
308+ return false ;
309+ }
310+ }
316311}
0 commit comments