Danny Lo
Danny Lo

Reputation: 1583

Eclipse Editor Plugin: isSaveAsAllowed

Working on my plugin for Eclipse containing among others a multi-page editor I faced a problem with the method isSaveEnabled() which is supposed to be overwritten in order to disable or enable the possibility to save the opened file. I forbid to change pages for certain conditions, so I've got the corresponding field and methods to achieve that:

public class ARTEditor
    extends FormEditor
    implements IResourceChangeListener
{
    private boolean pageChangeEnabled;

    @Override
    protected void pageChange(int newPageIndex)
    {
        if (pageChangeEnabled)
        {
            super.pageChange(newPageIndex);
            switch (newPageIndex)
            {
                // handling the change of the active page
            }
        }
    }

    // getter + setter for pageChangeEnabled used by other components
}

Now I would like to disable/enable the save action for the same conditions. I thought it would be sufficient to overwrite the above stated method:

@Override
public boolean isSaveAsAllowed()
{
    return pageChangeEnabled;
}

But this doesn't work, although the method correctly returns true or false. For testing I create the condition where I can't change pages, then press Ctrl+S and see that the file is saved because the asterisk showing the dirty state disappears.

Do I need to do anything else to implement my requirement?

upd:

A possible approach would be:

@Override
public void doSave(IProgressMonitor monitor)
{
    if (pageChangeEnabled)
    {
        // handle save action
    }
}

What I don't like about this solution is that the user can get confused: save action looks active but in fact it isn't.

Upvotes: 0

Views: 161

Answers (1)

greg-449
greg-449

Reputation: 111141

The isSaveAsAllowed method is only used to determine if 'File > Save As...' can be used. It is not used for 'File > Save'.

If you don't want to allow 'File > Save' you could override:

public boolean isDirty()

and prevent the editor for reporting it is dirty. This will also prevent the '*' showing that the editor is dirty being displayed.

You may need to call editorDirtyStateChanged() when you change your pageChangeEnabled flag to get the editor to call isDirty again.

You could also override doSave, in this case you should call setCanceled on the progress monitor. You might also want to show a MessageDialog explaining why you are not doing the save.

Upvotes: 1

Related Questions