开发者

How do I stop an Eclipse Editor from closing in an RCP

开发者 https://www.devze.com 2022-12-18 14:20 出处:网络
I am working on an Eclipse based RCP.We have a need to prevent one of the opened editors from being closed by the user.

I am working on an Eclipse based RCP. We have a need to prevent one of the opened editors from being closed by the user.

The desired behavior is:

  1. the user clicks the X in the editor window or "CTRL+W"
  2. a dialog pops up saying: "If you close this editor, your activity will stop. Do you want to?"
  3. if they click yes, it closes, if n开发者_运维知识库o, it stays open.

Oh yeah, and is this even possible?

Thanks, gk


You could use a org.eclipse.ui.ISaveablePart2, more specifically the method promptToSaveOnClose().

However, as said in this thread,

it will only be shown if the editor is dirty at the time it is closed.

See an example in this SaveableHelper.java source file.

See also the article Prevent that a RCP Editor is closed, which explains how this method works:

How do I stop an Eclipse Editor from closing in an RCP


You can also cancel the saving in

@Override
public void doSave(IProgressMonitor monitor) {

by calling

monitor.setCanceled(true);

In the EditorPart implementation


Not directly related but I was looking for a way to prevent an Editor to be closed and found this little hack, hope it could help.

page.addPartListener(new IPartListener2() {
    // [...]
    @Override
    public void partClosed(IWorkbenchPartReference partRef) {
        try {
            page.openEditor(input, id);
        } catch (PartInitException e) {
            e.printStackTrace();
        }
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号