From 9580995ff62c5529a9a3221318e00d882dd9958c Mon Sep 17 00:00:00 2001 From: Lars Vogel Date: Fri, 26 Jun 2026 10:11:21 +0200 Subject: [PATCH 1/2] Simplify duplicated workspace-lock error handling in IDEApplication The "already set" branch of checkInstanceLocation repeated hideSplash and the generic "cannot be set" error dialog across a nested if/else. Collapse to a single splash hide and one error path; behavior is unchanged. --- .../ide/application/IDEApplication.java | 34 ++++++++----------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/bundles/org.eclipse.ui.ide.application/src/org/eclipse/ui/internal/ide/application/IDEApplication.java b/bundles/org.eclipse.ui.ide.application/src/org/eclipse/ui/internal/ide/application/IDEApplication.java index 5ae75769acb..1bbbc5be2d3 100644 --- a/bundles/org.eclipse.ui.ide.application/src/org/eclipse/ui/internal/ide/application/IDEApplication.java +++ b/bundles/org.eclipse.ui.ide.application/src/org/eclipse/ui/internal/ide/application/IDEApplication.java @@ -277,27 +277,23 @@ protected Object checkInstanceLocation(Shell shell, Map applicationArguments) { // 1. directory is already in use // 2. directory could not be created File workspaceDirectory = new File(instanceLoc.getURL().getFile()); - if (workspaceDirectory.exists()) { - if (isDevLaunchMode(applicationArguments)) { - return EXIT_WORKSPACE_LOCKED; - } + boolean workspaceDirectoryExists = workspaceDirectory.exists(); - // check if there is a lock info then append it to error message. - String lockInfo = WorkspaceLock.getWorkspaceLockDetails(instanceLoc.getURL()); - if (lockInfo != null) { - hideSplash(shell); - WorkspaceLock.showWorkspaceLockedDialog(shell, workspaceDirectory.getAbsolutePath(), - lockInfo); - } else { - hideSplash(shell); - MessageDialog.openError(shell, - IDEWorkbenchMessages.IDEApplication_workspaceCannotBeSetTitle, - IDEWorkbenchMessages.IDEApplication_workspaceCannotBeSetMessage); - } + // In dev (PDE) launch mode let the launcher report the locked workspace. + if (workspaceDirectoryExists && isDevLaunchMode(applicationArguments)) { + return EXIT_WORKSPACE_LOCKED; + } + + // If the directory exists and carries lock info, show it; otherwise + // report that the workspace location could not be set. + String lockInfo = workspaceDirectoryExists + ? WorkspaceLock.getWorkspaceLockDetails(instanceLoc.getURL()) + : null; + hideSplash(shell); + if (lockInfo != null) { + WorkspaceLock.showWorkspaceLockedDialog(shell, workspaceDirectory.getAbsolutePath(), lockInfo); } else { - hideSplash(shell); - MessageDialog.openError( - shell, + MessageDialog.openError(shell, IDEWorkbenchMessages.IDEApplication_workspaceCannotBeSetTitle, IDEWorkbenchMessages.IDEApplication_workspaceCannotBeSetMessage); } From 08c2b7d7c1a9ba6e23447d54314ed876bdfc8d9d Mon Sep 17 00:00:00 2001 From: Lars Vogel Date: Fri, 26 Jun 2026 12:53:48 +0200 Subject: [PATCH 2/2] Decompose checkInstanceLocation into focused helper methods Extract the already-set lock handling, the prompt-retry loop, and the "workspace in use" dialog into lockExistingWorkspace, promptAndSetWorkspace, and openWorkspaceInUseDialog, leaving checkInstanceLocation as a short orchestrator. Pure extraction; behavior is unchanged. --- .../ide/application/IDEApplication.java | 166 +++++++++++------- 1 file changed, 98 insertions(+), 68 deletions(-) diff --git a/bundles/org.eclipse.ui.ide.application/src/org/eclipse/ui/internal/ide/application/IDEApplication.java b/bundles/org.eclipse.ui.ide.application/src/org/eclipse/ui/internal/ide/application/IDEApplication.java index 1bbbc5be2d3..54afa9e143d 100644 --- a/bundles/org.eclipse.ui.ide.application/src/org/eclipse/ui/internal/ide/application/IDEApplication.java +++ b/bundles/org.eclipse.ui.ide.application/src/org/eclipse/ui/internal/ide/application/IDEApplication.java @@ -263,51 +263,10 @@ protected Object checkInstanceLocation(Shell shell, Map applicationArguments) { return EXIT_OK; } if (result == ReturnCode.VALID) { - // at this point its valid, so try to lock it and update the - // metadata version information if successful - try { - if (instanceLoc.lock()) { - writeWorkspaceVersion(); - writeWsLockInfo(instanceLoc.getURL()); - return null; - } - - // we failed to create the directory. - // Two possibilities: - // 1. directory is already in use - // 2. directory could not be created - File workspaceDirectory = new File(instanceLoc.getURL().getFile()); - boolean workspaceDirectoryExists = workspaceDirectory.exists(); - - // In dev (PDE) launch mode let the launcher report the locked workspace. - if (workspaceDirectoryExists && isDevLaunchMode(applicationArguments)) { - return EXIT_WORKSPACE_LOCKED; - } - - // If the directory exists and carries lock info, show it; otherwise - // report that the workspace location could not be set. - String lockInfo = workspaceDirectoryExists - ? WorkspaceLock.getWorkspaceLockDetails(instanceLoc.getURL()) - : null; - hideSplash(shell); - if (lockInfo != null) { - WorkspaceLock.showWorkspaceLockedDialog(shell, workspaceDirectory.getAbsolutePath(), lockInfo); - } else { - MessageDialog.openError(shell, - IDEWorkbenchMessages.IDEApplication_workspaceCannotBeSetTitle, - IDEWorkbenchMessages.IDEApplication_workspaceCannotBeSetMessage); - } - } catch (IOException e) { - hideSplash(shell); - IDEWorkbenchPlugin.log("Could not obtain lock for workspace location", e); //$NON-NLS-1$ - MessageDialog.openError( - shell, - IDEWorkbenchMessages.InternalError, - e.getMessage()); - } - return EXIT_OK; + return lockExistingWorkspace(shell, instanceLoc, applicationArguments); } if (result == ReturnCode.INVALID) { + // prompt the user for a different location force = true; } } @@ -324,6 +283,71 @@ protected Object checkInstanceLocation(Shell shell, Map applicationArguments) { } } + return promptAndSetWorkspace(shell, instanceLoc, launchData, force); + } + + /** + * Locks the already-set workspace and updates its metadata version. + * + * @return null if the workspace was locked successfully, or an + * exit code if locking failed and the application should terminate + */ + @SuppressWarnings("rawtypes") + private Object lockExistingWorkspace(Shell shell, Location instanceLoc, Map applicationArguments) { + // at this point its valid, so try to lock it and update the + // metadata version information if successful + try { + if (instanceLoc.lock()) { + writeWorkspaceVersion(); + writeWsLockInfo(instanceLoc.getURL()); + return null; + } + + // we failed to create the directory. + // Two possibilities: + // 1. directory is already in use + // 2. directory could not be created + File workspaceDirectory = new File(instanceLoc.getURL().getFile()); + boolean workspaceDirectoryExists = workspaceDirectory.exists(); + + // In dev (PDE) launch mode let the launcher report the locked workspace. + if (workspaceDirectoryExists && isDevLaunchMode(applicationArguments)) { + return EXIT_WORKSPACE_LOCKED; + } + + // If the directory exists and carries lock info, show it; otherwise + // report that the workspace location could not be set. + String lockInfo = workspaceDirectoryExists + ? WorkspaceLock.getWorkspaceLockDetails(instanceLoc.getURL()) + : null; + hideSplash(shell); + if (lockInfo != null) { + WorkspaceLock.showWorkspaceLockedDialog(shell, workspaceDirectory.getAbsolutePath(), lockInfo); + } else { + MessageDialog.openError(shell, + IDEWorkbenchMessages.IDEApplication_workspaceCannotBeSetTitle, + IDEWorkbenchMessages.IDEApplication_workspaceCannotBeSetMessage); + } + } catch (IOException e) { + hideSplash(shell); + IDEWorkbenchPlugin.log("Could not obtain lock for workspace location", e); //$NON-NLS-1$ + MessageDialog.openError( + shell, + IDEWorkbenchMessages.InternalError, + e.getMessage()); + } + return EXIT_OK; + } + + /** + * Repeatedly prompts the user for a workspace location until one is set, the + * user cancels, or a restart is required. + * + * @return null if a workspace was set successfully, or an exit + * code otherwise + */ + private Object promptAndSetWorkspace(Shell shell, Location instanceLoc, ChooseWorkspaceData launchData, + boolean force) { int returnValue = -1; URL workspaceUrl = null; while (true) { @@ -381,31 +405,7 @@ protected Object checkInstanceLocation(Shell shell, Map applicationArguments) { // by this point it has been determined that the workspace is // already in use -- force the user to choose again - - String lockInfo = WorkspaceLock.getWorkspaceLockDetails(workspaceUrl); - - MessageDialog dialog = new MessageDialog(null, IDEWorkbenchMessages.IDEApplication_workspaceInUseTitle, - null, NLS.bind(IDEWorkbenchMessages.IDEApplication_workspaceInUseMessage, workspaceUrl.getFile()), - MessageDialog.ERROR, 2, IDEWorkbenchMessages.IDEApplication_workspaceInUse_Retry, - IDEWorkbenchMessages.IDEApplication_workspaceInUse_Choose, - IDEWorkbenchMessages.IDEApplication_workspaceInUse_Cancel) { - @Override - protected Control createCustomArea(Composite parent) { - if (lockInfo == null || lockInfo.isBlank()) { - return null; - } - - Composite container = new Composite(parent, SWT.NONE); - container.setLayout(new FillLayout()); - - Label multiLineText = new Label(container, SWT.NONE); - multiLineText.setText(NLS.bind(IDEWorkbenchMessages.IDEApplication_Ws_Lock_Owner_Message, lockInfo)); - - return container; - } - }; - // the return value influences the next loop's iteration - returnValue = dialog.open(); + returnValue = openWorkspaceInUseDialog(workspaceUrl); if (returnValue == CANCEL_LAUNCH || returnValue == CANCEL_LAUNCH_DEFAULT) { return EXIT_OK; @@ -416,6 +416,36 @@ protected Control createCustomArea(Composite parent) { } } + /** + * Opens the "workspace in use" dialog and returns the index of the button the + * user selected. + */ + private int openWorkspaceInUseDialog(URL workspaceUrl) { + String lockInfo = WorkspaceLock.getWorkspaceLockDetails(workspaceUrl); + + MessageDialog dialog = new MessageDialog(null, IDEWorkbenchMessages.IDEApplication_workspaceInUseTitle, + null, NLS.bind(IDEWorkbenchMessages.IDEApplication_workspaceInUseMessage, workspaceUrl.getFile()), + MessageDialog.ERROR, 2, IDEWorkbenchMessages.IDEApplication_workspaceInUse_Retry, + IDEWorkbenchMessages.IDEApplication_workspaceInUse_Choose, + IDEWorkbenchMessages.IDEApplication_workspaceInUse_Cancel) { + @Override + protected Control createCustomArea(Composite parent) { + if (lockInfo == null || lockInfo.isBlank()) { + return null; + } + + Composite container = new Composite(parent, SWT.NONE); + container.setLayout(new FillLayout()); + + Label multiLineText = new Label(container, SWT.NONE); + multiLineText.setText(NLS.bind(IDEWorkbenchMessages.IDEApplication_Ws_Lock_Owner_Message, lockInfo)); + + return container; + } + }; + return dialog.open(); + } + /** * Write lock owner details onto workspace lock file. Data includes user, host, * display and current java process id.