Is there a way to reinitialize the model? I.e. forget the previous state and load the new state in one generic operation. For example, I have empty model initialized and some time later I want to reinitialize it from other model or xml string. Can I do that?
The question is related to launch configuration life cycle. Launch configuration tab has createControl and to initialize sapphire control I need to pass the model to it. However initialization of the model will happen in initializeFrom method in with the model state will be set from launch configuration using condig.getAttribute calls.
My take on this would be, rather than storing each property in LC separately, store the whole model in launch configuration as single XML string.
However it is not clear how to reinitialize the same model object from different persisted stores.
The problem with this example is that createControl happens earlier than initializeFrom. This means that I need to keep the model handle between two calls and initialize the model state during second call. To be generic it would be nice to reinitialize the whole model from XML string. But it also possible to create some temporary model instance from xml string and set the state using get/set methods. Any advice?
So a Sapphire model is layered like so in common XML scenarios...
ModelElement -> XmlResource -> XmlResourceStore -> FileResourceStore or ByteArrayResourceStore
If you've instantiated the model, you are also in control of the resource store. I'd be inclined to take a look at sub-classing XmlResourceStore so that you can control the DOM tree that it is responsible for maintaining. Let's say you replace DOM in XmlResourceStore, you can then invoke refresh( true, true ) on the model root to force the model to re-read everything from the resource.
An alternative for solving the launch configuration initialization problem is to create an empty composite in createControl method. Then in initializeFrom, create the model and SapphireControl as a child of the composite created in createControl.
If I parse the content using separate model instance and then setting new DOM document to XMLResourceStore, this also does nothing. In this case I also have to extend XMLResourceStore b/c setDomDocument is protected.
So it looks like your last proposition is the only alternative.
XmlResourceStore does not listen on ByteArrayResourceStore, so it make sense that setContents() is not noticed. We can fix that if that's an interesting usecase.
It also looks like RootXmlResource caches DOM document during resource construction, so yeah, swapping out DOM isn't going to work now either.
If you are interested in pursuing this usecase, please go ahead and open a bug. I think RootXmlResource/XmlResourceStore/ByteArrayResourceStore should be connected by listeners. It should be sufficient to call setContents on ByteArrayResourceStore and have the model update. Even refresh call should not be necessary as RootXmlResource can take care of that once it is notified that the DOM tree has been replaced.