3

I have an editor class which extends FormEditor class. In order to achieve save and save As functionality for this editor, will it be enough to override:

  1. doSave(IProgressMonitor monitor)

  2. doSaveAs()

  3. isSaveAsAllowed()

these methods alone? Or anything else?

Felix Yan
  • 14,841
  • 7
  • 48
  • 61
user1168608
  • 598
  • 2
  • 10
  • 27

2 Answers2

1

There is an excellent post on the Eclipse Community Forums about this: http://www.eclipse.org/forums/index.php/t/140214/

An extract:

You are using an instance of FormEditor as your editor. This editor contains instances of FormPage as pages. I hope that you have specialized SectionParts (or AbstractParts) as sections in the page. In the section you have Text widgets that display some data and have an ModifyListener attached. In this listener call markDirty() for the section and the ManagedForm and FormEditor will handle dirty state for you. And don't forget to add each SectionPart to the ManagedForm by addPart(section)!

So essentially all you need is:

public class MyPage extends FormPage implements ModifyListener {

    private SectionPart secPart;
    private Text myText;

    public MyPage(FormEditor editor, String id, String title) {
        super(editor, id, title);
    }

    @Override
    protected void createFormContent(IManagedForm form) {

        secPart = new SectionPart(formBody, toolkit, Section.DESCRIPTION
                | Section.TITLE_BAR);
        Section section = secPart.getSection();

        //add labels and controls for the three fields
        myText = new Text(composite, SWT.BORDER);
        myText.addModifyListener(this);

        form.addPart(secPart);
    }

    public void modifyText(ModifyEvent arg0) {
        secPart.markDirty();
    }
}

As a side note, editorDirtyStateChanged() from FormEditor will nudge your plugin after you have made a save - to make the pages follow suit use commitPages(true). These methods are to be used AFTER you have saved of course - to let your workbench know everything is clean again.

Rossiar
  • 2,416
  • 2
  • 23
  • 32
1

Yes. Please consider also the following

  • Your editor must be dirty, that means you must have called firePropertyChange(PROP_DIRTY) before
  • If you build your own RCP make sure you have added Save and Save As... Menu-Items in your ActionBarAdvisor
  • consider using the doSave Methods of the integrated FormPages (this makes sometimes more sense)
  • Do not integrate any custom user-interaction (e.g. additional dialogs) in doSave or doSaveAs methods.
  • consider providing a command-stack that the user can undo/redo modifications regarding the editors input
  • make sure to handle the fact that your editor-input can be changed in the meantime (e.g. if another editor manpulates the editors input).
Tom Seidel
  • 9,525
  • 1
  • 26
  • 38