diff --git a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/tools/watcher/EimJsonWatchService.java b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/tools/watcher/EimJsonWatchService.java index b590e2500..9d751b0db 100644 --- a/bundles/com.espressif.idf.core/src/com/espressif/idf/core/tools/watcher/EimJsonWatchService.java +++ b/bundles/com.espressif.idf.core/src/com/espressif/idf/core/tools/watcher/EimJsonWatchService.java @@ -129,6 +129,7 @@ public void run() try { watchService.close(); + Logger.log("File Watch Service close"); //$NON-NLS-1$ } catch (IOException e) { diff --git a/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/EspressifToolStartup.java b/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/EspressifToolStartup.java index 570521136..c5630141b 100644 --- a/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/EspressifToolStartup.java +++ b/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/EspressifToolStartup.java @@ -6,7 +6,6 @@ import java.text.MessageFormat; -import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.MessageBox; @@ -41,13 +40,17 @@ */ public class EspressifToolStartup implements IStartup { + private EimJsonUiChangeHandler eimJsonUiChangeHandler; @Override public void earlyStartup() { Preferences preferences = org.eclipse.core.runtime.preferences.InstanceScope.INSTANCE .getNode(UIPlugin.PLUGIN_ID); ToolInitializer toolInitializer = new ToolInitializer(preferences); - + EimJsonStateChecker stateChecker = new EimJsonStateChecker(preferences); + eimJsonUiChangeHandler = new EimJsonUiChangeHandler(preferences); + stateChecker.updateLastSeenTimestamp(); + EimJsonWatchService.getInstance().addEimJsonChangeListener(eimJsonUiChangeHandler); if (!toolInitializer.isEimInstalled()) { notifyMissingTools(); @@ -65,23 +68,16 @@ public void earlyStartup() promptUserToOpenToolManager(eimJson); } - EimJsonStateChecker stateChecker = new EimJsonStateChecker(preferences); if (stateChecker.wasModifiedSinceLastRun()) { showEimJsonStateChangeNotification(); } - - stateChecker.updateLastSeenTimestamp(); - EimJsonWatchService.getInstance().addEimJsonChangeListener(new EimJsonUiChangeHandler(preferences)); } private void showEimJsonStateChangeNotification() { - Display.getDefault().asyncExec(() -> { - MessageDialog.openInformation(Display.getDefault().getActiveShell(), - com.espressif.idf.ui.tools.Messages.EimJsonChangedMsgTitle, - com.espressif.idf.ui.tools.Messages.EimJsonStateChangedMsgDetail); - }); + int response = eimJsonUiChangeHandler.displayMessageToUser(); + eimJsonUiChangeHandler.handleUserResponse(response); } private void notifyMissingTools() diff --git a/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/Messages.java b/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/Messages.java index 5dbcf3f2b..a6659697c 100644 --- a/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/Messages.java +++ b/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/Messages.java @@ -30,6 +30,9 @@ public class Messages extends NLS public static String EimJsonChangedMsgDetail; public static String EimJsonStateChangedMsgDetail; + public static String MsgYes; + public static String MsgNo; + static { // initialize resource bundle diff --git a/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/messages.properties b/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/messages.properties index 6a42ecb88..025d33362 100644 --- a/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/messages.properties +++ b/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/messages.properties @@ -7,6 +7,7 @@ EspIdfManagerReloadBtnToolTip=Reload from disk IDFToolsHandler_ToolsManagerConsole=Espressif IDF Tools Console IDFGuideLinkLabel_Text=Select the version of ESP-IDF you want to use. Click the radio button in Active column next to the version you want. For help in choosing the correct version, visit ESP-IDF Version Guide. EimJsonChangedMsgTitle=ESP-IDF Installation Changed -EimJsonChangedMsgDetail=It looks like the ESP-IDF tools are modified. The Espressif IDE cannot guarantee the consistency. Kindly refresh the installation via ESP-IDF Manager. +EimJsonChangedMsgDetail=It seems that the ESP-IDF tools have been modified. If you just installed ESP-IDF we recommend refereshing via ESP-IDF Manager . Would you like to proceed with the update? EimJsonStateChangedMsgDetail=It looks like the ESP-IDF installation was modified since last start. The Espressif IDE cannot guarantee the consistency. Kindly refresh the installation via ESP-IDF Manager. - \ No newline at end of file + MsgYes=Yes + MsgNo=No \ No newline at end of file diff --git a/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/watcher/EimJsonUiChangeHandler.java b/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/watcher/EimJsonUiChangeHandler.java index aa0b6495f..6bf8c4734 100644 --- a/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/watcher/EimJsonUiChangeHandler.java +++ b/bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/watcher/EimJsonUiChangeHandler.java @@ -4,15 +4,31 @@ *******************************************************************************/ package com.espressif.idf.ui.tools.watcher; +import java.io.IOException; import java.nio.file.Path; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.IWorkbenchWindow; +import org.eclipse.ui.PartInitException; +import org.eclipse.ui.console.MessageConsoleStream; +import org.eclipse.ui.ide.IDE; import org.osgi.service.prefs.Preferences; +import com.espressif.idf.core.logging.Logger; +import com.espressif.idf.core.tools.EimIdfConfiguratinParser; +import com.espressif.idf.core.tools.SetupToolsInIde; +import com.espressif.idf.core.tools.vo.EimJson; import com.espressif.idf.core.tools.watcher.EimJsonChangeListener; import com.espressif.idf.core.tools.watcher.EimJsonStateChecker; +import com.espressif.idf.ui.IDFConsole; +import com.espressif.idf.ui.handlers.EclipseHandler; import com.espressif.idf.ui.tools.Messages; +import com.espressif.idf.ui.tools.SetupToolsJobListener; +import com.espressif.idf.ui.tools.manager.ESPIDFManagerEditor; +import com.espressif.idf.ui.tools.manager.EimEditorInput; +import com.espressif.idf.ui.tools.manager.pages.ESPIDFMainTablePage; /** * eim_idf.json file ui change handler to notify user for changes. @@ -23,6 +39,7 @@ public class EimJsonUiChangeHandler implements EimJsonChangeListener { private Preferences preferences; + private EimJson eimJson; public EimJsonUiChangeHandler(Preferences preferences) { @@ -32,13 +49,102 @@ public EimJsonUiChangeHandler(Preferences preferences) @Override public void onJsonFileChanged(Path file) { - Display.getDefault().asyncExec(() -> { - MessageDialog.openWarning(Display.getDefault().getActiveShell(), Messages.EimJsonChangedMsgTitle, - Messages.EimJsonChangedMsgDetail); + int response = displayMessageToUser(); + handleUserResponse(response); + } + + public int displayMessageToUser() + { + final int[] response = new int[] { -1 }; + Display display = Display.getDefault(); + display.syncExec(() -> { + Shell shell = display.getActiveShell(); + if (shell == null) + { + shell = new Shell(display); + } + MessageDialog messageDialog = new MessageDialog(shell, Messages.EimJsonChangedMsgTitle, null, + Messages.EimJsonChangedMsgDetail, MessageDialog.WARNING, 0, + new String[] { Messages.MsgYes, Messages.MsgNo }); + response[0] = messageDialog.open(); }); + return response[0]; + } + + public void handleUserResponse(int response) + { + if (response == 0) + { + try + { + loadEimJson(); + if (eimJson.getIdfInstalled().size() == 1) + { + // only one entry in eimJson so we can simply refresh the IDE environment with that. + setupToolsInIde(); + } + else + { + // multiple entries in json so launch manager for user to handle this + launchEspIdfManager(); + } + } + catch ( + IOException + | PartInitException e) + { + Logger.log(e); + } + } + EimJsonStateChecker checker = new EimJsonStateChecker(preferences); checker.updateLastSeenTimestamp(); } + private void loadEimJson() throws IOException + { + EimIdfConfiguratinParser eimIdfConfiguratinParser = new EimIdfConfiguratinParser(); + eimJson = eimIdfConfiguratinParser.getEimJson(true); + } + + private void setupToolsInIde() + { + SetupToolsInIde setupToolsInIde = new SetupToolsInIde(eimJson.getIdfInstalled().get(0), eimJson, + getConsoleStream(true), getConsoleStream(false)); + SetupToolsJobListener toolsActivationJobListener = new SetupToolsJobListener(ESPIDFMainTablePage.getInstance(eimJson), + setupToolsInIde); + setupToolsInIde.addJobChangeListener(toolsActivationJobListener); + setupToolsInIde.schedule(); + } + + private void launchEspIdfManager() throws PartInitException + { + Display.getDefault().asyncExec(() -> { + IWorkbenchWindow activeww = EclipseHandler.getActiveWorkbenchWindow(); + if (activeww == null || activeww.getActivePage() == null) + { + Logger.log("Cannot open ESP-IDF Manager. No active workbench window or active page."); + return; + } + + try + { + IDE.openEditor(activeww.getActivePage(), new EimEditorInput(eimJson), ESPIDFManagerEditor.EDITOR_ID, + true); + } + catch (PartInitException e) + { + Logger.log("Failed to open ESP-IDF Manager Editor."); + Logger.log(e); + } + }); + + } + + private MessageConsoleStream getConsoleStream(boolean errorStream) + { + IDFConsole idfConsole = new IDFConsole(); + return idfConsole.getConsoleStream(Messages.IDFToolsHandler_ToolsManagerConsole, null, errorStream, true); + } }