Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*******************************************************************************
* Copyright 2025 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
* Use is subject to license terms.
*******************************************************************************/
package com.espressif.idf.ui;

import java.util.concurrent.Semaphore;
import java.util.function.Consumer;
import java.util.function.Supplier;

import org.eclipse.swt.widgets.Display;

public class GlobalModalLock
{
private static final Semaphore lock = new Semaphore(1);

private GlobalModalLock()
{
}

public static <T> void showModal(Supplier<T> dialogSupplier, Consumer<T> callback)
{
new Thread(() -> {
try
{
lock.acquire();
Display.getDefault().syncExec(() -> {
try
{
T result = dialogSupplier.get();
callback.accept(result);
} finally
{
lock.release();
}
});
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
}
}, "GlobalModalLock-DialogThread").start(); //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.ILaunchMode;
Expand Down Expand Up @@ -146,15 +144,17 @@ private void update(String newTarget)
// If both are not same
if (currentTarget != null && !newTarget.equals(currentTarget))
{

boolean isDelete = MessageDialog.openQuestion(EclipseUtil.getShell(),
GlobalModalLock.showModal(() -> MessageDialog.openQuestion(EclipseUtil.getShell(),
Messages.LaunchBarListener_TargetChanged_Title,
MessageFormat.format(Messages.LaunchBarListener_TargetChanged_Msg,
project.getName(), currentTarget, newTarget));
if (isDelete)
{
deleteBuildFolder(project, buildLocation);
}
project.getName(), currentTarget, newTarget)),
isDelete -> {
if (isDelete)
{
deleteBuildFolder(project, buildLocation);

}
});
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.espressif.idf.core.tools.watcher.EimJsonWatchService;
import com.espressif.idf.core.util.IDFUtil;
import com.espressif.idf.core.util.StringUtil;
import com.espressif.idf.ui.GlobalModalLock;
import com.espressif.idf.ui.IDFConsole;
import com.espressif.idf.ui.UIPlugin;
import com.espressif.idf.ui.handlers.EclipseHandler;
Expand Down Expand Up @@ -68,14 +69,13 @@ public class EspressifToolStartup implements IStartup
@Override
public void earlyStartup()
{
preferences = org.eclipse.core.runtime.preferences.InstanceScope.INSTANCE
.getNode(UIPlugin.PLUGIN_ID);
preferences = org.eclipse.core.runtime.preferences.InstanceScope.INSTANCE.getNode(UIPlugin.PLUGIN_ID);
toolInitializer = new ToolInitializer(preferences);
standardConsoleStream = getConsoleStream(false);
errorConsoleStream = getConsoleStream(true);
idfEnvironmentVariables = new IDFEnvironmentVariables();
eimLoader = new EimLoader(new StartupClassDownloadEimDownloadListener(),
standardConsoleStream, errorConsoleStream, Display.getDefault());
eimLoader = new EimLoader(new StartupClassDownloadEimDownloadListener(), standardConsoleStream,
errorConsoleStream, Display.getDefault());
EimJsonStateChecker stateChecker = new EimJsonStateChecker(preferences);
eimJsonUiChangeHandler = new EimJsonUiChangeHandler(preferences);
stateChecker.updateLastSeenTimestamp();
Expand All @@ -87,7 +87,7 @@ public void earlyStartup()
notifyMissingTools();
return;
}

eimJson = toolInitializer.loadEimJson();

if (toolInitializer.isOldEspIdfConfigPresent() && !toolInitializer.isOldConfigExported())
Expand All @@ -98,8 +98,8 @@ public void earlyStartup()
{
promptUserToMoveEimToApplications();
}
EimJsonWatchService.withPausedListeners(()-> handleOldConfigExport());

EimJsonWatchService.withPausedListeners(() -> handleOldConfigExport());
}
else if (toolInitializer.isEimIdfJsonPresent() && !toolInitializer.isEspIdfSet())
{
Expand All @@ -111,7 +111,7 @@ else if (toolInitializer.isEimIdfJsonPresent() && !toolInitializer.isEspIdfSet()
{
idfEnvironmentVariables.addEnvVariable(IDFEnvironmentVariables.EIM_PATH, eimJson.getEimPath());
}
else
else
{
// Fail-safe call to ensure if the eim is in Applications or user.home it is setup in env vars
toolInitializer.findAndSetEimPath();
Expand All @@ -127,113 +127,113 @@ private boolean checkIfEimPathMacOsIsInApplications()
{
if (!Platform.getOS().equals(Platform.OS_MACOSX))
return true;
String eimPath = idfEnvironmentVariables.getEnvValue(IDFEnvironmentVariables.EIM_PATH);

String eimPath = idfEnvironmentVariables.getEnvValue(IDFEnvironmentVariables.EIM_PATH);
if (!StringUtil.isEmpty(eimPath))
{
if (Files.exists(Paths.get(eimPath)))
{
boolean isInApplications = eimPath.startsWith("/Applications/") ||
eimPath.startsWith(System.getProperty("user.home") + "/Applications/");
boolean isInApplications = eimPath.startsWith("/Applications/")
|| eimPath.startsWith(System.getProperty("user.home") + "/Applications/");
if (!isInApplications)
{
Logger.log("EIM_PATH not in applications: " + eimPath);
return false;
}
}
}

return true;
}

private void handleOldConfigExport()
{
final int[] response = new int[] { -1 };
Display display = Display.getDefault();
display.syncExec(() -> {
MessageDialog messageDialog = new MessageDialog(display.getActiveShell(),
Messages.OldConfigFoundMsgBoxTitle, null, Messages.OldConfigFoundMsgBoxMsg, 0, 0,
new String[] { Messages.ToolsInitializationDifferentPathMessageBoxOptionYes,
Messages.ToolsInitializationDifferentPathMessageBoxOptionNo });
response[0] = messageDialog.open();
});

if (response[0] == 0)
GlobalModalLock.showModal(() -> MessageDialog.openQuestion(display.getActiveShell(),
Messages.OldConfigFoundMsgBoxTitle, Messages.OldConfigFoundMsgBoxMsg), response -> {
if (response)
{
startExportOldConfig();
}
});

}

private void startExportOldConfig()
{
try
{
try
// if eim json is present it means that it contains the updated path and we use that else we fallback to
// finding eim in default paths
Path eimPath;
String eimPathEnvVar = idfEnvironmentVariables.getEnvValue(IDFEnvironmentVariables.EIM_PATH);
if (eimJson != null)
{
// if eim json is present it means that it contains the updated path and we use that else we fallback to finding eim in default paths
Path eimPath;
String eimPathEnvVar = idfEnvironmentVariables.getEnvValue(IDFEnvironmentVariables.EIM_PATH);
if (eimJson != null)
{
eimPath = Paths.get(eimJson.getEimPath());
}
else if (!StringUtil.isEmpty(eimPathEnvVar))
{
eimPath = Paths.get(eimPathEnvVar);
}
else
{
eimPath = toolInitializer.getDefaultEimPath();
}

IStatus status = toolInitializer.exportOldConfig(eimPath);
Logger.log("Tools Conversion Process Message: ");
Logger.log(status.getMessage());
if (status.getSeverity() != IStatus.ERROR)
{
preferences.putBoolean(EimConstants.OLD_CONFIG_EXPORTED_FLAG, true);
displayInformationMessageBox(Messages.OldConfigExportCompleteSuccessMsgTitle,
Messages.OldConfigExportCompleteSuccessMsg);
}
else
{
displayInformationMessageBox(Messages.OldConfigExportCompleteFailMsgTitle,
Messages.OldConfigExportCompleteFailMsg);
}
eimPath = Paths.get(eimJson.getEimPath());
}
else if (!StringUtil.isEmpty(eimPathEnvVar))
{
eimPath = Paths.get(eimPathEnvVar);
}
else
{
eimPath = toolInitializer.getDefaultEimPath();
}

IStatus status = toolInitializer.exportOldConfig(eimPath);
Logger.log("Tools Conversion Process Message: ");
Logger.log(status.getMessage());
if (status.getSeverity() != IStatus.ERROR)
{
preferences.putBoolean(EimConstants.OLD_CONFIG_EXPORTED_FLAG, true);
displayInformationMessageBox(Messages.OldConfigExportCompleteSuccessMsgTitle,
Messages.OldConfigExportCompleteSuccessMsg);
}
catch (IOException e)
else
{
Logger.log("Error exporting old configuration", e);
displayInformationMessageBox(Messages.OldConfigExportCompleteFailMsgTitle,
Messages.OldConfigExportCompleteFailMsg);
}
}
catch (IOException e)
{
Logger.log("Error exporting old configuration", e);
displayInformationMessageBox(Messages.OldConfigExportCompleteFailMsgTitle,
Messages.OldConfigExportCompleteFailMsg);
}
}

private void displayInformationMessageBox(String messageTitle, String message)
{
Display display = Display.getDefault();
display.syncExec(() -> {
GlobalModalLock.showModal(() -> {
MessageDialog.openInformation(display.getActiveShell(), messageTitle, message);
return null;
}, ignored -> {
});
}

private void showEimJsonStateChangeNotification()
{
int response = eimJsonUiChangeHandler.displayMessageToUser();
eimJsonUiChangeHandler.handleUserResponse(response);
eimJsonUiChangeHandler.displayMessageToUser();
}

private void notifyMissingTools()
{
boolean [] userAgreed = new boolean[1];
Display.getDefault().syncExec(() -> {
userAgreed[0] = MessageDialog.openQuestion(Display.getDefault().getActiveShell(),
Messages.ToolsInitializationEimMissingMsgBoxTitle,
Messages.ToolsInitializationEimMissingMsgBoxMessage);
});

if (userAgreed[0])
{
// Download Launch EIM
downloadAndLaunchEim();
}
else
{
Logger.log("User selected No to download EIM");
}
GlobalModalLock.showModal(() -> MessageDialog.openQuestion(Display.getDefault().getActiveShell(),
Messages.ToolsInitializationEimMissingMsgBoxTitle, Messages.ToolsInitializationEimMissingMsgBoxMessage),
response -> {
if (response)
{
// Download Launch EIM
downloadAndLaunchEim();
}
else
{
Logger.log("User selected No to download EIM");
}
});
}

private void downloadAndLaunchEim()
Expand Down Expand Up @@ -295,19 +295,22 @@ private void promptUserToOpenToolManager(EimJson eimJson)
messageBox.setText(Messages.NoActiveEspIdfInWorkspaceMsgTitle);
messageBox.setMessage(Messages.NoActiveEspIdfInWorkspaceMsg);

if (messageBox.open() == SWT.YES)
{
openEspIdfManager(eimJson);
}
GlobalModalLock.showModal(messageBox::open, response -> {
if (response == SWT.YES)
{
openEspIdfManager(eimJson);
}
});
});
}

private void promptUserToMoveEimToApplications()
{
Display.getDefault().asyncExec(() -> {
MessageDialog.openInformation(
Display.getDefault().getActiveShell(),
Messages.EIMNotInApplicationsTitle, Messages.EIMNotInApplicationsMessage);
GlobalModalLock.showModal(() -> {
MessageDialog.openInformation(Display.getDefault().getActiveShell(), Messages.EIMNotInApplicationsTitle,
Messages.EIMNotInApplicationsMessage);
return null;
}, ignored -> {
});
}

Expand All @@ -328,7 +331,7 @@ private void openEspIdfManager(EimJson eimJson)
}
});
}

private class StartupClassDownloadEimDownloadListener implements DownloadListener
{

Expand All @@ -347,7 +350,7 @@ public void onProgress(int percent)
Logger.log(e);
}
});

}

@Override
Expand All @@ -363,7 +366,7 @@ public void onCompleted(String filePath)
Logger.log(e);
}
});

Process process = null;
String appToLaunch = filePath;
try
Expand All @@ -372,15 +375,17 @@ public void onCompleted(String filePath)
{
appToLaunch = eimLoader.installAndLaunchDmg(Paths.get(filePath));
}

idfEnvironmentVariables.addEnvVariable(IDFEnvironmentVariables.EIM_PATH, appToLaunch);
process = eimLoader.launchEim(appToLaunch);
}
catch (IOException | InterruptedException e)
catch (
IOException
| InterruptedException e)
{
Logger.log(e);
}

eimLoader.waitForEimClosure(process, () -> {
if (toolInitializer.isOldEspIdfConfigPresent() && !toolInitializer.isOldConfigExported())
{
Expand Down
Loading
Loading