Search code examples
eclipseeclipse-plugineclipse-rcprcp

How to Handle view parts close tab in RCP


Currently I am working on RCP application with view part. In application, user can able to open multiple instances of same view part. I am able to handle view parts close operation with menu selection. But I am unaware of handling the 'X' i.e tab close operation.

I tried with adding IPartListener object to the view part object,(I refereed this link ),but in its partClosed() method I am facing 2 issues,

  1. I am not able to get the current instance of view part object.
  2. The partClosed() method only gets called after closing the tab, so I am not able to show the confirmation for closing the tab.

What approach should I use to resolve above two questions.

Any help is appreciated.

Best regards,

Mandar


Solution

  • You may consider to implement the interface ISaveablePart2 to your ViewPart. This interface is responsible to prompt the user if the part should be closed or not. Furthermore it decides what to do with the unsaved data.

    You can promt the user if the view should be closed with a custom dialog in the method promptToSaveOnClose().

    @Override
    public boolean isDirty() {
        return true;
    }
    
    @Override
    public int promptToSaveOnClose() {
        boolean close = MessageDialog.openConfirm(
                Display.getCurrent().getActiveShell(), "Close?", "Really?");
    
        if(close)
            return YES;
        return CANCEL;
    }
    

    Eclispe API: ISaveablePart2