Bug 140293 - [api] Need more flexibility to override behavior of TextEditor.performSaveAs
Summary: [api] Need more flexibility to override behavior of TextEditor.performSaveAs
Status: RESOLVED INVALID
Alias: None
Product: Platform
Classification: Eclipse Project
Component: Text (show other bugs)
Version: 3.1.1   Edit
Hardware: PC Windows XP
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: Platform-Text-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords: needinfo
Depends on:
Blocks:
 
Reported: 2006-05-05 00:48 EDT by Brian Payton CLA
Modified: 2007-06-22 10:04 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Brian Payton CLA 2006-05-05 00:48:46 EDT
I found it necessary to copy and override the behavior of the performSaveAs method in org.eclipse.ui.editors.test.TextEditor.  I had to do this when the current input is a StorageEditorInput (non-file) but the user wants to save to a file.  Here's the code with comments where I had to do my tweaks.  This is in org.eclipse.wst.rdb.server.extensions.internal.editorlaunch.SQLEditorForServerExplorer.

    protected void performSaveAs(IProgressMonitor progressMonitor) {
        IEditorInput input = getEditorInput();
        String inputName = input.getName();
        
        /* Begin code copied from TextEditor.performSaveAs */
        Shell shell= getSite().getShell();
        
        SaveAsDialog dialog= new SaveAsDialog(shell);
        
        /* Begin local modifications */
        /* When file input, behave as usual. */
        if (input instanceof IFileEditorInput) {
            IFile original= (input instanceof IFileEditorInput) ? ((IFileEditorInput) input).getFile() : null;
            if (original != null) {
                dialog.setOriginalFile(original);
            }
        }
        /* When we have a SQLEditorStorageEditorInput, initialize the SaveAsDialog
         * with the input name, since that's all we have
         */
        else if (input instanceof SQLEditorStorageEditorInput){
            if (inputName != null && inputName.length() > 0) {
                if (!inputName.endsWith(".sql") && !inputName.endsWith(".ddl")) { //$NON-NLS-1$ //$NON-NLS-2$
                    inputName = inputName + ".sql"; //$NON-NLS-1$
                }
                dialog.setOriginalName( inputName );
            }
        }
        /* End local modifications */
        
        dialog.create();
        
        IDocumentProvider provider= getDocumentProvider();
        if (provider == null) {
            // editor has programmatically been  closed while the dialog was open
            return;
        }
        
        
        if (provider.isDeleted(input)) {
            /* Begin local modifications */
            /* We don't have access to the TextEditorMessages class, since it's
             * "package private" to the org.eclipse.ui.editors.text package. So
             * we have copied the messages into our own resources file. 
             */
            // String message= MessageFormat.format(TextEditorMessages.getString("Editor.warning.save.delete"), new Object[] { inputName }); //$NON-NLS-1$
            String message= MessageFormat.format(SQLEditorResources.getString("SQLEditor.warning.save.delete"), new Object[] { inputName }); //$NON-NLS-1$
            /* End local modifications */
            dialog.setErrorMessage(null);
            dialog.setMessage(message, IMessageProvider.WARNING);
        }
        
        if (dialog.open() == Window.CANCEL) {
            if (progressMonitor != null)
                progressMonitor.setCanceled(true);
            return;
        }
        
        IPath filePath= dialog.getResult();
        if (filePath == null) {
            if (progressMonitor != null)
                progressMonitor.setCanceled(true);
            return;
        }
        
        IWorkspace workspace= ResourcesPlugin.getWorkspace();
        IFile file= workspace.getRoot().getFile(filePath);
        /* Begin local modifications */
        /* We need to preserve the connection and other information that we got
         * from the original SQL editor input.  So we need to create a new 
         * SQL Editor input and copy the information to it. 
         */
        // final IEditorInput newInput= new FileEditorInput(file);
        IEditorInput newInput = null;
        if (input instanceof ISQLEditorInput) {
            ISQLEditorInput sqlEditorInput = (ISQLEditorInput) input;
            newInput = new SQLEditorFileEditorInput( file );
            SQLEditorFileEditorInput newSQLEditorInput = (SQLEditorFileEditorInput) newInput;
            newSQLEditorInput.setConnectionInfo( sqlEditorInput.getConnectionInfo() );
            newSQLEditorInput.setDatabase( sqlEditorInput.getDatabase() );
            newSQLEditorInput.setDefaultSchemaName( sqlEditorInput.getDefaultSchemaName() );
        }
        else {
            newInput = new FileEditorInput( file );
        }
        /* End local modifications */
        
        boolean success= false;
        try {
            
            provider.aboutToChange(newInput);
            provider.saveDocument(progressMonitor, newInput, provider.getDocument(input), true);            
            success= true;
            
        } catch (CoreException x) {
            IStatus status= x.getStatus();
            if (status == null || status.getSeverity() != IStatus.CANCEL) {
                /* Begin local modifications */
                // String title= TextEditorMessages.getString("Editor.error.save.title"); //$NON-NLS-1$
                // String msg= MessageFormat.format(TextEditorMessages.getString("Editor.error.save.message"), new Object[] { x.getMessage() }); //$NON-NLS-1$
                String title= SQLEditorResources.getString("SQLEditor.error.save.title"); //$NON-NLS-1$
                String msg= MessageFormat.format(SQLEditorResources.getString("SQLEditor.error.save.message"), new Object[] { x.getMessage() }); //$NON-NLS-1$
                /* End local modifications */
                
                if (status != null) {
                    switch (status.getSeverity()) {
                        case IStatus.INFO:
                            MessageDialog.openInformation(shell, title, msg);
                        break;
                        case IStatus.WARNING:
                            MessageDialog.openWarning(shell, title, msg);
                        break;
                        default:
                            MessageDialog.openError(shell, title, msg);
                    }
                } else {
                    MessageDialog.openError(shell, title, msg);
                }
            }
        } finally {
            provider.changed(newInput);
            if (success)
                setInput(newInput);
        }
        
        if (progressMonitor != null)
            progressMonitor.setCanceled(!success);
        /* End code copied from TextEditor.performSaveAs */
    }

Here's another example where I needed to do something similar.  This is from
com.ibm.datatools.sqlbuilder.SQLBuilderForServerExplorer:

    protected void convertEditorInputToFileEditorInput() {
        IProgressMonitor progressMonitor = null;
        IEditorInput input = getEditorInput();
        
        /* The input must be storage-based (non-file) input, so bail out if it's not. */
        if (!(input instanceof SQLEditorStorageEditorInput)) {
            return;
        }
        SQLEditorStorageEditorInput storageInput = (SQLEditorStorageEditorInput) input;
        String inputName = storageInput.getName();
        
        Shell shell = getSite().getShell();
        SaveAsDialog dialog = new SaveAsDialog( shell );
        
        /* Initialize the SaveAsDialog with the input name, since that's all we have
         */
        if (inputName != null && inputName.length() > 0) {
            if (!inputName.endsWith(".sql") && !inputName.endsWith(".ddl")) { //$NON-NLS-1$ //$NON-NLS-2$
                inputName = inputName + ".sql"; //$NON-NLS-1$
            }
            dialog.setOriginalName( inputName );
        }
        
        dialog.create();
        if (dialog.open() == Window.CANCEL) {
            if (progressMonitor != null)
                progressMonitor.setCanceled(true);
            return;
        }
        
        IPath filePath= dialog.getResult();
        if (filePath == null) {
            if (progressMonitor != null)
                progressMonitor.setCanceled(true);
            return;
        }
        
        IWorkspace workspace = ResourcesPlugin.getWorkspace();
        IFile fileResource = workspace.getRoot().getFile( filePath );
        
        /* Load the current SQL into the file we just created by getting the
         * domain model to save into it. */
        SQLDomainModel domainModel = getDomainModel();
        domainModel.setIFile( fileResource );
        domainModel.save();
        
        /* We need to preserve the connection and other information that we got
         * from the original SQL editor input.  So we need to create a new 
         * SQLEditorFileEditorInput and copy the information to it. 
         */
        SQLEditorFileEditorInput fileInput = new SQLEditorFileEditorInput( fileResource );
        fileInput.setConnectionInfo( storageInput.getConnectionInfo() );
        fileInput.setDatabase( storageInput.getDatabase() );
        fileInput.setDefaultSchemaName( storageInput.getDefaultSchemaName() );
        
        /* Reset everything using the new editor input. */
        if (getSite() instanceof IEditorSite) {
            IEditorSite site = (IEditorSite) getSite();
            try {
                init( site, fileInput );
            }
            catch( PartInitException e ) {
                String title= SQLEditorResources.getString("SQLEditor.error.save.title"); //$NON-NLS-1$
                String msg= MessageFormat.format(SQLEditorResources.getString("SQLEditor.error.save.message"), new Object[] { e.getMessage() }); //$NON-NLS-1$
                MessageDialog.openError(shell, title, msg);
            }
        }
        
        if (progressMonitor != null) {
            progressMonitor.setCanceled(false);
        }
    }
Comment 1 Dani Megert CLA 2006-05-05 03:24:36 EDT
Please provide more details what exactly you miss. NOTE: as usual business inside the Eclipse SDK we won't provide the messages as API.
Comment 2 Dani Megert CLA 2007-06-22 09:59:48 EDT
Get rid of deprecated state.
Comment 3 Dani Megert CLA 2007-06-22 10:04:57 EDT
.