Class ComponentProvider<T extends JComponent>
- All Implemented Interfaces:
Serializable
,UIDependent
- Direct Known Subclasses:
CheckBoxProvider
,HyperlinkProvider
,LabelProvider
,WrappingProvider
CellContext
. It's basically re-usable across
all types of renderees (JTable, JList, JTree).
Content
A provider guarantees to configure the "content" properties completely for any given object. The most frequent mappings are to text and/or icon properties of the rendering components. The former is controlled by a StringValue (see below), the latter by an IconValue. Subclasses which hand out component of type IconAware guarantee to reset its icon property always.
To ease content configuration, it supports a pluggable
StringValue
which purpose is to create and return a string
representation of a given object. Implemenations of a ComponentProvider can
use it to configure their rendering component as appropriate.
F.i. to show a Contributor cell object as "Busywoman, Herta" implement a custom StringValue and use it in a text rendering provider. (Note that SwingX default implementations of Table/List/TreeCellRenderer have convenience constructors to take the converter and create a default LabelProvider which uses it).
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));
To ease handling of formatted localizable content, there's a
FormatStringValue
which is pluggable with a
Format
. F.i. to show a Date's time in the default Locale's SHORT version and right align the cell
StringValue stringValue = new FormatStringValue(
DateFormat.getTimeInstance(DateFormat.SHORT));
table.getColumnExt("timeID").setCellRenderer(
new DefaultTableRenderer(stringValue, JLabel.RIGHT);
Default Visuals
Guarantees to completely configure the visual properties listed below. As a consequence, client code (f.i. inHighlighter
s) can safely
change them without long-lasting visual artefacts.
- foreground and background, depending on selected and focused state
- border
- font
- Painter (if applicable)
- enabled
- componentOrientation
- tooltipText
- minimum-, maximum-, preferredSize
- horizontal alignment (if applicable)
DefaultVisuals
(which handles the first eight items)
subclasses have to guarantee the alignment only.-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected int
horizontal (text) alignment of component.protected DefaultVisuals<T>
configurator of default visuals.protected IconValue
protected T
component to render with.protected StringValue
the converter to use for string representation. -
Constructor Summary
ConstructorsConstructorDescriptionInstantiates a component provider with LEADING horizontal alignment and default to-String converter.ComponentProvider
(StringValue converter) Instantiates a component provider with LEADING horizontal alignment and the given converter.ComponentProvider
(StringValue converter, int alignment) Instantiates a LabelProvider with given to-String converter and given horizontal alignment. -
Method Summary
Modifier and TypeMethodDescriptionprotected 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 rendering 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 DefaultVisuals<T>
Factory method to create and return the DefaultVisuals used by this to configure the default visuals.protected abstract T
Factory method to create and return the component to use for rendering.protected abstract void
format
(CellContext context) Formats the renderering component's content from the given cell context.protected DefaultVisuals<T>
Intermediate exposure during refactoring...int
Returns the horizontal alignment.getRendererComponent
(CellContext context) Configures and returns an appropriate component to render a cell in the given context.Returns a string representation of the content.Returns the StringValue to use for obtaining the String representation.protected Icon
getValueAsIcon
(CellContext context) Returns a Icon representation of the content.protected String
getValueAsString
(CellContext context) Returns a String representation of the content.void
setHorizontalAlignment
(int alignment) Sets the horizontal alignment property to configure the component with.void
void
setStringValue
(StringValue formatter) Sets the StringValue to use.void
updateUI()
-
Field Details
-
rendererComponent
component to render with. -
defaultVisuals
configurator of default visuals. -
alignment
protected int alignmenthorizontal (text) alignment of component. PENDING: useful only for labels, buttons? -
stringValue
the converter to use for string representation. -
iconValue
-
-
Constructor Details
-
ComponentProvider
public ComponentProvider()Instantiates a component provider with LEADING horizontal alignment and default to-String converter. -
ComponentProvider
Instantiates a component provider with LEADING horizontal alignment and the given converter.- Parameters:
converter
- the converter to use for mapping the cell value to a String representation.
-
ComponentProvider
Instantiates a LabelProvider with given to-String converter and given horizontal alignment. If the converter is null, the default TO_STRING is used.- Parameters:
converter
- the converter to use for mapping the cell value to a String representation.alignment
- the horizontal alignment.
-
-
Method Details
-
getRendererComponent
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:
-
setStringValue
Sets the StringValue to use. If the given StringValue is null, defaults toStringValue.TO_STRING
.- Parameters:
formatter
- the format to use.
-
setIconValue
-
getStringValue
Returns the StringValue to use for obtaining the String representation.- Returns:
- the StringValue used by this provider, guaranteed to be not null.
-
getString
Returns a string representation of the content.This method guarantees to return the same string representation as would appear in the renderer, given that the corresponding cellContext has the same value as the parameter passed-in here. That is (assuming that the rendering component has a getText())
This implementation simply delegates to its StringValue. Subclasses might need to override to comply.if (equals(value, context.getValue()) { assertEquals(provider.getString(value), provider.getRenderingComponent(context).getText()); }
This is a second attempt - the driving force is the need for a consistent string representation across all (new and old) themes: rendering, (pattern) filtering/highlighting, searching, auto-complete ...
- Parameters:
value
- the object to represent as string.- Returns:
- a appropriate string representation of the cell's content.
-
getValueAsString
Returns a String representation of the content.This method messages the
StringValue
to get the String rep. Meant as a convenience for subclasses.- Parameters:
context
- the cell context, must not be null.- Returns:
- a appropriate string representation of the cell's content.
-
getValueAsIcon
Returns a Icon representation of the content.This method messages the
IconValue
to get the Icon rep. Meant as a convenience for subclasses.- Parameters:
context
- the cell context, must not be null.- Returns:
- a appropriate icon representation of the cell's content, or null if non if available.
-
configureVisuals
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:
-
configureContent
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:
-
format
Formats the renderering component's content from the given cell context.- Parameters:
context
- the cell context to configure from, must not be null.
-
configureState
Configures the rendering component's state from the given cell context.- Parameters:
context
- the cell context to configure from, must not be null.
-
createRendererComponent
Factory method to create and return the component to use for rendering.- Returns:
- the component to use for rendering.
-
createDefaultVisuals
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.
-
getDefaultVisuals
Intermediate exposure during refactoring...- Returns:
- the default visual configurator used by this.
-
updateUI
public void updateUI()- Specified by:
updateUI
in interfaceUIDependent
-