views:

391

answers:

3

I'm writing a custom swing component (something completely new, but think JTree or JList). I'm trying to follow the general design of JTree, JTable, JList etc for consistency (I've also seen various poor 3rd party components abandon the separable model and/or renderer approach).

So, I have a model full of nodes, the component itself and a renderer. At some point the node has to be turned into text and displayed by a renderer. I'm not clear on the best way to do this:

  • Pass the node itself (as Object) to the renderer, and let the renderer decide how to display it.
    • This is how JList does it.
    • Requires a customised renderer just to change the text.
    • Allows great flexibility in how to display the node (doesn't even have to be text).
  • Pass the node itself (as Object) to the renderer, but have a convertValueToText() method in the component class.
    • This is how JTree does it.
    • Renderers can be just as flexibile as before - don't have to use this method.
    • Have to override component to change the text transformation.
  • As above, but delegate convertValueTotext() to the model.
    • This is how JXTable does it.
    • The model is probably the best place for this method - and it's easier to override there.

I don't want to have to customise the renderer just to change the text, but I'd like to be able to customise the renderer to do more than display a model-displayed string (else why bother with renderers). I really don't like the fact that JXTable uses reflection to look for convertValueToText() in the model - this smells of bad magic to me.

Can anyone shed any light on this oft-neglected part of Swing?

SOLUTION

What I ended up doing was this:

  • Add a method to the model which returns a string for the given node. Importantly, this can be null to indicate that the renderer should know what to do or that we simply can't provide anything useful.
  • The component has the same method, and passes the call on to the model. This is important for view-model separation. The renderer calls this method, so it doesn't talk to the model directly.
  • The default renderer calls the above method and if it's not null, it uses it, otherwise it can call toString on the value, or provide a default, or whatever.

This leaves developers a choice when they want to override the displayed value - Override the method with a non-null return value knowing that the default renderer will display this text. - Provide a custom renderer which is passed the actual node object so it can do "clever" things if it needs to.

I'm quite happy with it - it "feels" right, it works, and it's easy to use.

Thanks for your perspectives!

+1  A: 

Good question. This is not specific to Swing, but a philosophical question about the difference between a model and a view.

In general, is converting objects into text the job of the model or the view? My purist head says that actually you want a hierarchy of views - one to convert object model to text, and one to display the text. You might even want more than two - for instance, object-to-text, text-to-document-structure, document-structure-to-HTML, and then CSS to present to the user.

However, pragmatism says this may get too hard to remember and maintain. So in your circumstance I would suggest: think about how likely it is you'll ever want to extract non-text data from the model. If it is not very likely, then put the equivalent of convertValueToText in the model.

Otherwise, allow the component to either use a renderer, if it is given one, or else take the object value and convert it to text internally.

This allows maximum flexibility and probably makes things feel most natural to the users of the API. I believe this is the JTable model though I haven't used Swing for a long time.

Leigh Caldwell
Sorry, the convertvalueToText() in my example applies to JTree, not JTable (oops). JTable's default renderer seems to just call value.toString().
Draemon
I've accepted this since I think it's pretty close to what I ended up doing - see question for update.
Draemon
A: 

AFAIK neither JList nor JTree require the renderer to render text. The renderer gets passed the data object and return a JComponent which gets positioned as a child in Tree/List itself and then rendered.
I would go with this. A renderer for text would simply return a JLabel. If you want to be able to change the way, the text is constructed pass a Formatter to the TextRender, and you are done.

  • Stephan
ZeissS
What do you mean pass a formatter to the TextRenderer? What's a TextRenderer?
Draemon
TextRenderer: A simple implementation of your renderer interface, that returns a JComponent (e.g. JLabel) which displays/renders the data object as a string/textPass a formatter: The render may take a (Format)[http://java.sun.com/j2se/1.5.0/docs/api/java/text/Format.html] as a constructor parameter
ZeissS
The formatter's a good idea, but you would still have to pass in a custom renderer instance (albeit just an instance of the standard renderer with a new formatter). I would rather they only had to customise the model in most cases
Draemon
A: 

If you had to write your own component do it as simple as possible. In a lot of cases if you need a custom renderer then you don't care about interpretation by component or model. Model holds your data. And in this case is also custom written. From my point of view the good choice is based on first option. Provide DefaultRenderer which implements AbstractRenderer and add there all methods like toText(Object o) and so on. Then allow me to decide whether I want to use default functionality or I prefer to wrote my own. Do you really need custom component? To make it works correctly it is a LOT of work. Is this component worth all this?

Rastislav Komara