@@ -263,51 +263,10 @@ protected Object checkInstanceLocation(Shell shell, Map applicationArguments) {
263263 return EXIT_OK ;
264264 }
265265 if (result == ReturnCode .VALID ) {
266- // at this point its valid, so try to lock it and update the
267- // metadata version information if successful
268- try {
269- if (instanceLoc .lock ()) {
270- writeWorkspaceVersion ();
271- writeWsLockInfo (instanceLoc .getURL ());
272- return null ;
273- }
274-
275- // we failed to create the directory.
276- // Two possibilities:
277- // 1. directory is already in use
278- // 2. directory could not be created
279- File workspaceDirectory = new File (instanceLoc .getURL ().getFile ());
280- boolean workspaceDirectoryExists = workspaceDirectory .exists ();
281-
282- // In dev (PDE) launch mode let the launcher report the locked workspace.
283- if (workspaceDirectoryExists && isDevLaunchMode (applicationArguments )) {
284- return EXIT_WORKSPACE_LOCKED ;
285- }
286-
287- // If the directory exists and carries lock info, show it; otherwise
288- // report that the workspace location could not be set.
289- String lockInfo = workspaceDirectoryExists
290- ? WorkspaceLock .getWorkspaceLockDetails (instanceLoc .getURL ())
291- : null ;
292- hideSplash (shell );
293- if (lockInfo != null ) {
294- WorkspaceLock .showWorkspaceLockedDialog (shell , workspaceDirectory .getAbsolutePath (), lockInfo );
295- } else {
296- MessageDialog .openError (shell ,
297- IDEWorkbenchMessages .IDEApplication_workspaceCannotBeSetTitle ,
298- IDEWorkbenchMessages .IDEApplication_workspaceCannotBeSetMessage );
299- }
300- } catch (IOException e ) {
301- hideSplash (shell );
302- IDEWorkbenchPlugin .log ("Could not obtain lock for workspace location" , e ); //$NON-NLS-1$
303- MessageDialog .openError (
304- shell ,
305- IDEWorkbenchMessages .InternalError ,
306- e .getMessage ());
307- }
308- return EXIT_OK ;
266+ return lockExistingWorkspace (shell , instanceLoc , applicationArguments );
309267 }
310268 if (result == ReturnCode .INVALID ) {
269+ // prompt the user for a different location
311270 force = true ;
312271 }
313272 }
@@ -324,6 +283,71 @@ protected Object checkInstanceLocation(Shell shell, Map applicationArguments) {
324283 }
325284 }
326285
286+ return promptAndSetWorkspace (shell , instanceLoc , launchData , force );
287+ }
288+
289+ /**
290+ * Locks the already-set workspace and updates its metadata version.
291+ *
292+ * @return <code>null</code> if the workspace was locked successfully, or an
293+ * exit code if locking failed and the application should terminate
294+ */
295+ @ SuppressWarnings ("rawtypes" )
296+ private Object lockExistingWorkspace (Shell shell , Location instanceLoc , Map applicationArguments ) {
297+ // at this point its valid, so try to lock it and update the
298+ // metadata version information if successful
299+ try {
300+ if (instanceLoc .lock ()) {
301+ writeWorkspaceVersion ();
302+ writeWsLockInfo (instanceLoc .getURL ());
303+ return null ;
304+ }
305+
306+ // we failed to create the directory.
307+ // Two possibilities:
308+ // 1. directory is already in use
309+ // 2. directory could not be created
310+ File workspaceDirectory = new File (instanceLoc .getURL ().getFile ());
311+ boolean workspaceDirectoryExists = workspaceDirectory .exists ();
312+
313+ // In dev (PDE) launch mode let the launcher report the locked workspace.
314+ if (workspaceDirectoryExists && isDevLaunchMode (applicationArguments )) {
315+ return EXIT_WORKSPACE_LOCKED ;
316+ }
317+
318+ // If the directory exists and carries lock info, show it; otherwise
319+ // report that the workspace location could not be set.
320+ String lockInfo = workspaceDirectoryExists
321+ ? WorkspaceLock .getWorkspaceLockDetails (instanceLoc .getURL ())
322+ : null ;
323+ hideSplash (shell );
324+ if (lockInfo != null ) {
325+ WorkspaceLock .showWorkspaceLockedDialog (shell , workspaceDirectory .getAbsolutePath (), lockInfo );
326+ } else {
327+ MessageDialog .openError (shell ,
328+ IDEWorkbenchMessages .IDEApplication_workspaceCannotBeSetTitle ,
329+ IDEWorkbenchMessages .IDEApplication_workspaceCannotBeSetMessage );
330+ }
331+ } catch (IOException e ) {
332+ hideSplash (shell );
333+ IDEWorkbenchPlugin .log ("Could not obtain lock for workspace location" , e ); //$NON-NLS-1$
334+ MessageDialog .openError (
335+ shell ,
336+ IDEWorkbenchMessages .InternalError ,
337+ e .getMessage ());
338+ }
339+ return EXIT_OK ;
340+ }
341+
342+ /**
343+ * Repeatedly prompts the user for a workspace location until one is set, the
344+ * user cancels, or a restart is required.
345+ *
346+ * @return <code>null</code> if a workspace was set successfully, or an exit
347+ * code otherwise
348+ */
349+ private Object promptAndSetWorkspace (Shell shell , Location instanceLoc , ChooseWorkspaceData launchData ,
350+ boolean force ) {
327351 int returnValue = -1 ;
328352 URL workspaceUrl = null ;
329353 while (true ) {
@@ -381,31 +405,7 @@ protected Object checkInstanceLocation(Shell shell, Map applicationArguments) {
381405
382406 // by this point it has been determined that the workspace is
383407 // already in use -- force the user to choose again
384-
385- String lockInfo = WorkspaceLock .getWorkspaceLockDetails (workspaceUrl );
386-
387- MessageDialog dialog = new MessageDialog (null , IDEWorkbenchMessages .IDEApplication_workspaceInUseTitle ,
388- null , NLS .bind (IDEWorkbenchMessages .IDEApplication_workspaceInUseMessage , workspaceUrl .getFile ()),
389- MessageDialog .ERROR , 2 , IDEWorkbenchMessages .IDEApplication_workspaceInUse_Retry ,
390- IDEWorkbenchMessages .IDEApplication_workspaceInUse_Choose ,
391- IDEWorkbenchMessages .IDEApplication_workspaceInUse_Cancel ) {
392- @ Override
393- protected Control createCustomArea (Composite parent ) {
394- if (lockInfo == null || lockInfo .isBlank ()) {
395- return null ;
396- }
397-
398- Composite container = new Composite (parent , SWT .NONE );
399- container .setLayout (new FillLayout ());
400-
401- Label multiLineText = new Label (container , SWT .NONE );
402- multiLineText .setText (NLS .bind (IDEWorkbenchMessages .IDEApplication_Ws_Lock_Owner_Message , lockInfo ));
403-
404- return container ;
405- }
406- };
407- // the return value influences the next loop's iteration
408- returnValue = dialog .open ();
408+ returnValue = openWorkspaceInUseDialog (workspaceUrl );
409409
410410 if (returnValue == CANCEL_LAUNCH || returnValue == CANCEL_LAUNCH_DEFAULT ) {
411411 return EXIT_OK ;
@@ -416,6 +416,36 @@ protected Control createCustomArea(Composite parent) {
416416 }
417417 }
418418
419+ /**
420+ * Opens the "workspace in use" dialog and returns the index of the button the
421+ * user selected.
422+ */
423+ private int openWorkspaceInUseDialog (URL workspaceUrl ) {
424+ String lockInfo = WorkspaceLock .getWorkspaceLockDetails (workspaceUrl );
425+
426+ MessageDialog dialog = new MessageDialog (null , IDEWorkbenchMessages .IDEApplication_workspaceInUseTitle ,
427+ null , NLS .bind (IDEWorkbenchMessages .IDEApplication_workspaceInUseMessage , workspaceUrl .getFile ()),
428+ MessageDialog .ERROR , 2 , IDEWorkbenchMessages .IDEApplication_workspaceInUse_Retry ,
429+ IDEWorkbenchMessages .IDEApplication_workspaceInUse_Choose ,
430+ IDEWorkbenchMessages .IDEApplication_workspaceInUse_Cancel ) {
431+ @ Override
432+ protected Control createCustomArea (Composite parent ) {
433+ if (lockInfo == null || lockInfo .isBlank ()) {
434+ return null ;
435+ }
436+
437+ Composite container = new Composite (parent , SWT .NONE );
438+ container .setLayout (new FillLayout ());
439+
440+ Label multiLineText = new Label (container , SWT .NONE );
441+ multiLineText .setText (NLS .bind (IDEWorkbenchMessages .IDEApplication_Ws_Lock_Owner_Message , lockInfo ));
442+
443+ return container ;
444+ }
445+ };
446+ return dialog .open ();
447+ }
448+
419449 /**
420450 * Write lock owner details onto workspace lock file. Data includes user, host,
421451 * display and current java process id.
0 commit comments