{
// Open FileDialog
Shell shell = getSite().getShell();
final IEditorInput input = getEditorInput();
IDocumentProvider provider = getDocumentProvider();
final IEditorInput newInput;
FileDialog dialog = new FileDialog( shell, SWT.SAVE );
String path = dialog.open();
if ( path == null )
{
if ( progressMonitor != null )
{
progressMonitor.setCanceled( true );
}
return;
}
// Check whether file exists and if so, confirm overwrite
final File externalFile = new File( path );
if ( externalFile.exists() )
{
MessageDialog overwriteDialog = new MessageDialog( shell, "Overwrite", null, "Overwrite?",
MessageDialog.WARNING, new String[]
{ IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL }, 1 ); // 'No' is the default
if ( overwriteDialog.open() != Window.OK )
{
if ( progressMonitor != null )
{
progressMonitor.setCanceled( true );
return;
}
}
}
IPath iPath = new Path( path );
newInput = new PathEditorInput( iPath );
boolean success = false;
try
{
provider.aboutToChange( newInput );
provider.saveDocument( progressMonitor, newInput, provider.getDocument( input ), true );
success = true;
}
catch ( CoreException x )
{
final IStatus status = x.getStatus();
if ( status == null || status.getSeverity() != IStatus.CANCEL )
{
String title = "Error in Save As...";
String msg = "Error in Save As... " + x.getMessage();
MessageDialog.openError( shell, title, msg );
}
}
finally
{
provider.changed( newInput );
if ( success )
{
setInput( newInput );
}
}