Search code examples
javaeclipsercpe4

ECLIPSE E4 - Save Workspace status


Hey guys I'd like to deal with persisted data in order to save my Application Status before quitting. Well in order to give it a try I created a dirtyable object in an MPART then added a saving handler linked to a menu entry.

Here it's: MPART:

txtInput.addModifyListener(new ModifyListener() {
                @Override
                public void modifyText(ModifyEvent e) {
                    dirty.setDirty(true);
                }
            });

Handler:

@Execute
public void execute(EPartService partService) {
    partService.saveAll(false);
} 

At the end I also removed the -clearpersistedstate argument from the running configuration but every time I launch my app, it doesn't save changes inside the MPART but only changes at perspective level E.g: If an MPART has been closed in the following execution it will be kept closed.

Any hints?


Solution

  • EPartService.saveAll just calls any method annotated with @Persist in the parts. This will be done automatically when the workspace is shutdown anyway.

    Note: The part must be marked as dirty for the @Persist method to be called.

    So to save any details in your part you need a method:

    @Persist
    void save()
    {
      ... save your data somewhere
    }
    

    when your part is created again you have to load your data from your saved data.

    One place to save data is the MPart persisted state - access this with:

    Map<String, String> persistedState = part.getPersistedState();
    

    You can save string values in this map.

    So:

    @Persist
    void save(MPart part)
    {
       Map<String, String> persistedState = part.getPersistedState();
    
       persistedState.put("key for my value", "my value");
    }
    

    and retrieve it with:

    @PostConstruct
    void createPart(MPart part)
    {
       Map<String, String> persistedState = part.getPersistedState();
    
       String myValue = persistedState.get("key for my value");
    }