开发者

Cancel a close event

I have an editor in Eclipse. When the user chooses to close it, a dialog shall appear with several options. One of them is "Cancel" and should cancel the close event. How can I do that?

The partial code I have so far:

    ...
    IEditorPart openEditor = page.openEditor(input, CS_STRINGEDITOR_ID);
    openEditor.getEditorSite().getPage()
        .addPartListener(new IPartListener() {
          public void partOpened(IWorkbenchPart part) {}
          public void partDeactivated(IWorkbenchPart part) {}

          public void partClosed(IWorkbenchPart part) {
            Shell sh = new Shell(cv.getViewSite().getShell());
            // My MessageDialog with the options, one being "cancel"
            CloseDial开发者_StackOverflowog closeDialog = new CloseDialog(sh);
            closeDialog.open();
            int returnCode = closeDialog.getReturnCode();
            switch (returnCode) {
            case CloseDialog.CANCEL_ID:
              // Abort the close event and keep the editor alive
              break;
            }
          }


This thread points out there is no closing event per se.

Eclipse provides mostly post-activite events, i.e. on IPartListener2, partClosed(*) is a post-closing event.

You can provide your own dialog on close, but it will only be shown if the editor is dirty at the time it is closed. See ISaveablePart2.promptToSaveOnClose()

(as presented in the Prevent that a RCP Editor is closed)

However, note that implementing ISaveablePart2 may lead to many dialogs being opened when you close multiple editors.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜