Thanks to JFace viewers I can easily present anyone of my “model” inside an Eclipse view.
After several usages of this technology I am still sometime wondering how should I use it. Here is my dilemma:
Lets say we want to display a “model” (by model I mean here some Java POJOs representing application data) as a tree. Tree concepts are Children, Parent and Leaf. Of course these concepts must exist somewhere in my data model to present this model as a tree.
From here, depending on the relation between my data model and the tree concept, two solutions come in my mind (I am maybe missing something and that’s why I am blogging on the subject, so let me know about all your ideas, experiences …)
First solution: we perform ALL the “model to tree” mapping in the content provider (Note that this IS the content provider purpose)
This solution works perfectly when the data model IS a tree. I.e children, parent and leaf exists as it in the model. The data model of the first JFace TreeViewer Snippet is a good example of such a model.
Unfortunately, in real situations where we need to present data as trees, we must sometimes “Compute” the tree from the data. In such situations the ContentProvider implementation can quickly increase in terms of complexity and thus in terms of readability and maintainability.
Here’s come the second solution:
In this solution, we first compute a “tree model” of the original data model. Then the ContentProvider implementation becomes really easy, since (as in the previously mentioned snippet) we directly return model’s children, parent and leaf to the viewer. In this case we created an intermediate model: “the view model”
As I said previously, which solution I choose really depends on the context and mainly on the “Cost” and “Complexity” of the computation required to convert the model into a tree.
I will be interested in community feedback regarding this subject. How do you convert your data models to be presented in JFace viewers ??