Override Save Action In Basic Text Eclipse Editor - updated for extra step

Posted on 2008-11-18
Last Modified: 2013-12-15
I have created a class that extends IFileEditorInput that opens a stored file anywhere on my machine and displays the file in the org.eclipse.ui.DefaultTextEditor.  This file is not in the current project, just displayed in an editor window.  

What I would like to do is make it so that if a person wants to edit it and save it back to the temporary location *and* also write that file in a different location, like a database. What class would I need to extend to get that functionality?  Basically if someone right mouse clicks on the file and selects "save" that eclipse will call my method for saving the information in both places instead of the default save action.  Any examples of doing this that you know about?  Thanks in advance
Question by:M5Driver
    LVL 12

    Expert Comment

    You didn't say what version of Eclipse you're using.  The older way to handle this would be with a Command.  The new way (since 3.3) is an Action.  I'll assume you can use an Action...

    Create an ActionSet, associate an Action with that set.  The Action associates the action being taken with an ActionDelegate you define.  The delegate implements IWorkbenchWindowActionDelegate.  You can read more details here:

    Author Comment

    I am using Eclipse 3.4.  

    The only way that I was able to get something close to working was to create a whole new editor by extending the default text editor.  There is a doSave method in the editor class that I can override and is being called when I click on the save dialog on the editor.  So the question now is:

    public void doSave(IProgressMonitor monitor){
      String editorcontents = ???

    How do I get the contents of the editor window so that I can do something with the file info?  I  looked all through the methods available off the editor and can't find anything like getEditorContents or whatever.  Any idea?  Thanks.

    LVL 12

    Accepted Solution

    To get the contents in doSave(), try this:

    IDocument  doc  = this.getDocumentProvider().getDocument(getEditorInput());

    The resulting document has lots of methods to access the text of the editor.

    Expert Comment

    Unfortunately I have the same problem but I don't want to solve it with my own editor solution. I want to be able to do it the way marklorenz suggested it. Is there anyone that can give more information about a solution with the
    Create an ActionSet, associate an Action with that set.  The Action associates the action being taken with an ActionDelegate you define.  The delegate implements IWorkbenchWindowActionDelegate.

    on existing editors?

    thanks a lot.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
    Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
    THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
    The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now