@@ -356,89 +356,94 @@ public IFile createNewFile() {
356356 final IPath containerPath = resourceGroup .getContainerFullPath ();
357357 IPath newFilePath = containerPath .append (resourceGroup .getResource ());
358358 final IFile newFileHandle = createFileHandle (newFilePath );
359- final InputStream initialContents = getInitialContents ();
359+ try ( InputStream initialContents = getInitialContents ()) {
360360
361- createLinkTarget ();
361+ createLinkTarget ();
362362
363- if (linkTargetPath != null ) {
364- URI resolvedPath = newFileHandle .getPathVariableManager ().resolveURI (linkTargetPath );
365- try {
366- if (resolvedPath .getScheme () != null && resolvedPath .getSchemeSpecificPart () != null ) {
367- IFileStore store = EFS .getStore (resolvedPath );
368- if (!store .fetchInfo ().exists ()) {
369- MessageDialog dlg = new MessageDialog (getContainer ().getShell (),
370- IDEWorkbenchMessages .WizardNewFileCreationPage_createLinkLocationTitle , null ,
371- NLS .bind (IDEWorkbenchMessages .WizardNewFileCreationPage_createLinkLocationQuestion ,
372- linkTargetPath ),
373- MessageDialog .QUESTION_WITH_CANCEL , 0 , IDialogConstants .YES_LABEL ,
374- IDialogConstants .NO_LABEL , IDialogConstants .CANCEL_LABEL );
375- int result = dlg .open ();
376- if (result == Window .OK ) {
377- store .getParent ().mkdir (0 , new NullProgressMonitor ());
378- OutputStream stream = store .openOutputStream (0 , new NullProgressMonitor ());
379- stream .close ();
363+ if (linkTargetPath != null ) {
364+ URI resolvedPath = newFileHandle .getPathVariableManager ().resolveURI (linkTargetPath );
365+ try {
366+ if (resolvedPath .getScheme () != null && resolvedPath .getSchemeSpecificPart () != null ) {
367+ IFileStore store = EFS .getStore (resolvedPath );
368+ if (!store .fetchInfo ().exists ()) {
369+ MessageDialog dlg = new MessageDialog (getContainer ().getShell (),
370+ IDEWorkbenchMessages .WizardNewFileCreationPage_createLinkLocationTitle , null ,
371+ NLS .bind (IDEWorkbenchMessages .WizardNewFileCreationPage_createLinkLocationQuestion ,
372+ linkTargetPath ),
373+ MessageDialog .QUESTION_WITH_CANCEL , 0 , IDialogConstants .YES_LABEL ,
374+ IDialogConstants .NO_LABEL , IDialogConstants .CANCEL_LABEL );
375+ int result = dlg .open ();
376+ if (result == Window .OK ) {
377+ store .getParent ().mkdir (0 , new NullProgressMonitor ());
378+ try (OutputStream stream = store .openOutputStream (0 , new NullProgressMonitor ())) {
379+ // only try to open
380+ }
381+ }
382+ if (result == 2 )
383+ return null ;
380384 }
381- if (result == 2 )
382- return null ;
383385 }
386+ } catch (CoreException | IOException e ) {
387+ MessageDialog .open (MessageDialog .ERROR , getContainer ().getShell (),
388+ IDEWorkbenchMessages .WizardNewFileCreationPage_internalErrorTitle ,
389+ NLS .bind (IDEWorkbenchMessages .WizardNewFileCreationPage_internalErrorMessage ,
390+ e .getMessage ()),
391+ SWT .SHEET );
392+
393+ return null ;
384394 }
385- } catch (CoreException | IOException e ) {
395+ }
396+
397+ IRunnableWithProgress op = monitor -> {
398+ CreateFileOperation op1 = new CreateFileOperation (newFileHandle , linkTargetPath , initialContents ,
399+ IDEWorkbenchMessages .WizardNewFileCreationPage_title );
400+ try {
401+ // see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=219901
402+ // directly execute the operation so that the undo state is
403+ // not preserved. Making this undoable resulted in too many
404+ // accidental file deletions.
405+ op1 .execute (monitor , WorkspaceUndoUtil .getUIInfoAdapter (getShell ()));
406+ } catch (final ExecutionException e ) {
407+ getContainer ().getShell ().getDisplay ().syncExec (() -> {
408+ if (e .getCause () instanceof CoreException ) {
409+ ErrorDialog .openError (getContainer ().getShell (), // Was
410+ // Utilities.getFocusShell()
411+ IDEWorkbenchMessages .WizardNewFileCreationPage_errorTitle , null , // no special
412+ // message
413+ ((CoreException ) e .getCause ()).getStatus ());
414+ } else {
415+ IDEWorkbenchPlugin .log (getClass (), "createNewFile()" , e .getCause ()); //$NON-NLS-1$
416+ MessageDialog .openError (getContainer ().getShell (),
417+ IDEWorkbenchMessages .WizardNewFileCreationPage_internalErrorTitle ,
418+ NLS .bind (IDEWorkbenchMessages .WizardNewFileCreationPage_internalErrorMessage ,
419+ e .getCause ().getMessage ()));
420+ }
421+ });
422+ }
423+ };
424+ try {
425+ getContainer ().run (true , true , op );
426+ } catch (InterruptedException e ) {
427+ return null ;
428+ } catch (InvocationTargetException e ) {
429+ // Execution Exceptions are handled above but we may still get
430+ // unexpected runtime errors.
431+ IDEWorkbenchPlugin .log (getClass (), "createNewFile()" , e .getTargetException ()); //$NON-NLS-1$
386432 MessageDialog .open (MessageDialog .ERROR , getContainer ().getShell (),
387433 IDEWorkbenchMessages .WizardNewFileCreationPage_internalErrorTitle ,
388- NLS .bind (IDEWorkbenchMessages .WizardNewFileCreationPage_internalErrorMessage , e .getMessage ()),
434+ NLS .bind (IDEWorkbenchMessages .WizardNewFileCreationPage_internalErrorMessage ,
435+ e .getTargetException ().getMessage ()),
389436 SWT .SHEET );
390437
391438 return null ;
392439 }
393- }
394440
395- IRunnableWithProgress op = monitor -> {
396- CreateFileOperation op1 = new CreateFileOperation (newFileHandle , linkTargetPath , initialContents ,
397- IDEWorkbenchMessages .WizardNewFileCreationPage_title );
398- try {
399- // see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=219901
400- // directly execute the operation so that the undo state is
401- // not preserved. Making this undoable resulted in too many
402- // accidental file deletions.
403- op1 .execute (monitor , WorkspaceUndoUtil .getUIInfoAdapter (getShell ()));
404- } catch (final ExecutionException e ) {
405- getContainer ().getShell ().getDisplay ().syncExec (() -> {
406- if (e .getCause () instanceof CoreException ) {
407- ErrorDialog .openError (getContainer ().getShell (), // Was
408- // Utilities.getFocusShell()
409- IDEWorkbenchMessages .WizardNewFileCreationPage_errorTitle , null , // no special
410- // message
411- ((CoreException ) e .getCause ()).getStatus ());
412- } else {
413- IDEWorkbenchPlugin .log (getClass (), "createNewFile()" , e .getCause ()); //$NON-NLS-1$
414- MessageDialog .openError (getContainer ().getShell (),
415- IDEWorkbenchMessages .WizardNewFileCreationPage_internalErrorTitle ,
416- NLS .bind (IDEWorkbenchMessages .WizardNewFileCreationPage_internalErrorMessage ,
417- e .getCause ().getMessage ()));
418- }
419- });
420- }
421- };
422- try {
423- getContainer ().run (true , true , op );
424- } catch (InterruptedException e ) {
425- return null ;
426- } catch (InvocationTargetException e ) {
427- // Execution Exceptions are handled above but we may still get
428- // unexpected runtime errors.
429- IDEWorkbenchPlugin .log (getClass (), "createNewFile()" , e .getTargetException ()); //$NON-NLS-1$
430- MessageDialog .open (MessageDialog .ERROR , getContainer ().getShell (),
431- IDEWorkbenchMessages .WizardNewFileCreationPage_internalErrorTitle ,
432- NLS .bind (IDEWorkbenchMessages .WizardNewFileCreationPage_internalErrorMessage ,
433- e .getTargetException ().getMessage ()),
434- SWT .SHEET );
435-
436- return null ;
437- }
438-
439- newFile = newFileHandle ;
441+ newFile = newFileHandle ;
440442
441- return newFile ;
443+ return newFile ;
444+ } catch (IOException e ) {
445+ throw new RuntimeException (e );
446+ }
442447 }
443448
444449 /**
0 commit comments