Search code examples
eclipsemodeleclipse-plugineclipse-rcprcp

Sharing a model between two views


I am still learning how to use rich client platform and window builder.

what I have at the moment is a simple plugin project with a view, I then added a second view and in the perspetiveextensions extension, I made it so that both of the views are side by side...

But now here is the question, I have this model provider:

public enum ModelProvider {
    INSTANCE;

    List<Object> model;

    private ModelProvider {
        model = new ArayList<Object>();

        //make some objects here
        //add the objects to the list
    }

    public List<Object> getModel() {
        return model;
    }
}

the thing is that I want to share this model with both of the views, because if I go and write this: ModelProvider.INSTANCE.getModel() and assign it to some list within each of the views, then surely they will have their own copy of the model and I would like it to be shared...

how can I initialise my application in such a way that it both of my views can share the same model...

perhaps in Application.java??

sorry this is really new to me.


Solution

  • By the looks of it, you are using the singleton design pattern.

    The reference leading to the List< Object > would firstly need to be private, to prevent any sort of outside access.

    This way, without a setter or any other way to access and change the model variable, if you access ModelProvider.INSTANCE.getModel() you will receive the same ArrayList< Object>. The Eclipse OSGI Equinox framework does not alter the expected behavior of singletons

    • If both views are in the SAME plugin, alongside with the ModelProvider, the plug-in initialization will not be important.

    • If the views are in different plugins, that means that the plug-in that the ModelProvider is, should either be added in MANIFEST.MF file via Require-Bundle directive, or the package where the ModelProvider is, to be added via the Import-Package directive in the plug-ins that contain the views

    I will edit my answer depending on the scenario you're talking about