Search code examples
eclipseeclipse-rcprcp

Eclipse RCP4 close Part on exit / disable persistence of a Part?


i'm working on an Eclipse RCP4 project. I have different perspectives showing a set of Parts to choose informations from. After selecting what i want to see, a new Part opens and displays the objet i want to edit / view attibutes of. I can open many parts of the same type. If i close the application, the eclipse framwork persists the position of all opened Parts. If i restart the application all previously opened Parts are open but without informations.

-How to prevent Eclipseframwork from persisting the state of Parts?

-How to close Parts on exit?

I'm searching for a way to add an "removeOnExit" tag to a Part and than close such a marked Part on exit.

Thanks in advance :)


Solution

  • With this you can close MParts with a specific Tag.

    It seems you have to switch to the Perspective the Part is on, else it's not removed from the context wich will cause nullpointer exceptions.

    @Inject
        @Optional
        public void doEvent(@EventTopic(EventConstants.EventTopics.CLOSE_PARTS_WITH_TAGS) String tagToClose, MApplication app,
    
    
    EModelService eModelService, EPartService ePartService) {
        MUIElement activePart = ePartService.getActivePart();
        EPartService activePeServcie = null;
        MPerspective activePerspective = null;
        if (activePart instanceof MPart) {
            activePeServcie = ((MPart) activePart).getContext().get(EPartService.class);
            activePerspective = eModelService.getPerspectiveFor(activePart);
        }
    
        List<String> tags = new ArrayList<String>();
        tags.add(tagToClose);
        List<MPart> elementsWithTags = eModelService.findElements(app, null, MPart.class, tags);
    
        for (MPart part : elementsWithTags) {
            try {
                logger.info("Closing part " + part.toString());
                EPartService peService = part.getContext().get(EPartService.class);
                peService.switchPerspective(eModelService.getPerspectiveFor(part));
                peService.hidePart(part);
            } catch (Exception e) {
                logger.error(e);
            }
        }
    
        if (activePart instanceof MPart && activePeServcie != null && activePerspective != null) {
            activePeServcie.switchPerspective(activePerspective);
        }
    
    }