org.jdesktop.swingx.renderer
Class ComponentProvider<T extends JComponent>

java.lang.Object
  extended by org.jdesktop.swingx.renderer.ComponentProvider<T>
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
ButtonProvider, HyperlinkProvider, LabelProvider, WrappingProvider

public abstract class ComponentProvider<T extends JComponent>
extends Object
implements Serializable

Abstract base class of a provider for a cell rendering component. Configures the component's content and default visuals depending on the renderee's state as captured in a CellContext. It's basically re-usable across all types of renderees (JTable, JList, JTree).


 StringValue stringValue = new StringValue() {
 
     public String getString(Object value) {
         if (!(value instanceof Contributor))
             return TO_STRING.getString(value);
         Contributor contributor = (Contributor) value;
         return contributor.lastName + ", " + contributor.firstName;
     }
 
 };
 table.setDefaultRenderer(Contributor.class, new DefaultTableRenderer(
         stringValue));
 list.setCellRenderer(new DefaultListRenderer(stringValue));
 tree.setCellRenderer(new DefaultTreeRenderer(stringValue));
 
 

Guarantees to completely configure the visual properties listed below. As a consequence, client code (f.i. in Highlighters) can safely change them without long-lasting visual artefacts.

As this internally delegates default visual configuration to a DefaultVisuals (which handles the first six items) subclasses have to guarantee the alignment only.

See Also:
DefaultVisuals, CellContext, DefaultTableRenderer, DefaultListRenderer, DefaultTreeRenderer, Serialized Form

Field Summary
protected  int alignment
          horizontal (text) alignment of component.
protected  DefaultVisuals<T> defaultVisuals
          configurator of default visuals.
protected  StringValue formatter
          the converter to use for string representation.
protected  T rendererComponent
          component to render with.
 
Constructor Summary
ComponentProvider()
          Instantiates a default component provider with LEADING horizontal alignment and default to-String converter.
 
Method Summary
protected  void configureContent(CellContext context)
          Configures the renderering component's content and state from the given cell context.
protected abstract  void configureState(CellContext context)
          Configures the renderering component's state from the given cell context.
protected  void configureVisuals(CellContext context)
          Configures the rendering component's default visuals frome the given cell context.
protected abstract  T createRendererComponent()
          Factory method to create and return the component to use for rendering.
protected  DefaultVisuals<T> createRendererController()
          Factory method to create and return the DefaultVisuals used by this to configure the default visuals.
protected abstract  void format(CellContext context)
          Formats the renderering component's content from the given cell context.
 int getHorizontalAlignment()
          Returns the horizontal alignment.
 T getRendererComponent(CellContext context)
          Configures and returns an appropriate component to render a cell in the given context.
protected  DefaultVisuals<T> getRendererController()
          Intermediate exposure during refactoring...
 String getStringValue(CellContext context)
          Returns a string representation of the content.
 StringValue getToStringConverter()
          Returns the converter to use for obtaining the String representation.
 void setHorizontalAlignment(int alignment)
          Sets the horizontal alignment property to configure the component with.
 void setToStringConverter(StringValue formatter)
          Sets the StringValue to use.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

rendererComponent

protected T extends JComponent rendererComponent
component to render with.


defaultVisuals

protected DefaultVisuals<T extends JComponent> defaultVisuals
configurator of default visuals.


alignment

protected int alignment
horizontal (text) alignment of component. PENDING: useful only for labels, buttons?


formatter

protected StringValue formatter
the converter to use for string representation.

Constructor Detail

ComponentProvider

public ComponentProvider()
Instantiates a default component provider with LEADING horizontal alignment and default to-String converter.

Method Detail

getRendererComponent

public T getRendererComponent(CellContext context)
Configures and returns an appropriate component to render a cell in the given context. If the context is null, returns the component in its current state.

Parameters:
context - the cell context to configure from
Returns:
a component to render a cell in the given context.

setHorizontalAlignment

public void setHorizontalAlignment(int alignment)
Sets the horizontal alignment property to configure the component with. Allowed values are those accepted by corresponding JLabel setter. The default value is JLabel.LEADING. This controller guarantees to apply the alignment on each request for a configured rendering component, if possible. Note that not all components have a horizontal alignment property.

Parameters:
alignment - the horizontal alignment to use when configuring the rendering component.

getHorizontalAlignment

public int getHorizontalAlignment()
Returns the horizontal alignment.

Returns:
the horizontal alignment of the rendering component.
See Also:
setHorizontalAlignment(int)

setToStringConverter

public void setToStringConverter(StringValue formatter)
Sets the StringValue to use. If the given converter is null, uses the default to_string.

Parameters:
formatter - the format to use.

getToStringConverter

public StringValue getToStringConverter()
Returns the converter to use for obtaining the String representation.

Returns:
the StringValue used by this controller, guaranteed to be not null.

getStringValue

public String getStringValue(CellContext context)
Returns a string representation of the content.

PENDING: This is a first attempt - we need a consistent string representation across all (new and old) theme: rendering, (pattern) filtering/highlighting, searching, auto-complete, what else??

Parameters:
context - the cell context.
Returns:
a appropriate string representation of the cell's content.

configureVisuals

protected void configureVisuals(CellContext context)
Configures the rendering component's default visuals frome the given cell context. Here: delegates to the renderer controller.

Parameters:
context - the cell context to configure from, must not be null.
See Also:
DefaultVisuals

configureContent

protected void configureContent(CellContext context)
Configures the renderering component's content and state from the given cell context.

Parameters:
context - the cell context to configure from, must not be null.
See Also:
configureState(CellContext), format(CellContext)

format

protected abstract void format(CellContext context)
Formats the renderering component's content from the given cell context.

Parameters:
context - the cell context to configure from, must not be null.

configureState

protected abstract void configureState(CellContext context)
Configures the renderering component's state from the given cell context.

Parameters:
context - the cell context to configure from, must not be null.

createRendererComponent

protected abstract T createRendererComponent()
Factory method to create and return the component to use for rendering.

Returns:
the component to use for rendering.

createRendererController

protected DefaultVisuals<T> createRendererController()
Factory method to create and return the DefaultVisuals used by this to configure the default visuals. Here: creates the default controller parameterized to the same type as this.

Returns:
the controller used to configure the default visuals of the rendering component.

getRendererController

protected DefaultVisuals<T> getRendererController()
Intermediate exposure during refactoring...

Returns:
the default visual configurator used by this.