2
* Created on 08.01.2007
5
package org.jdesktop.swingx.renderer;
7
import javax.swing.BorderFactory;
8
import javax.swing.Icon;
9
import javax.swing.tree.DefaultMutableTreeNode;
11
import org.jdesktop.swingx.RolloverRenderer;
12
import org.jdesktop.swingx.treetable.TreeTableNode;
16
* Wrapping ComponentProvider for usage in tree rendering. Handles the icon
17
* itself, delegates the node content to the wrappee. Value-based icon and
18
* content mapping can be configured by custom <code>IconValue</code>s and
19
* <b>StringValue</b>, respectively.
22
* An example of how to configure a file tree by using the system icons and
26
* StringValue sv = new StringValue() {
28
* public String getString(Object value) {
29
* if (value instanceof File) {
30
* return FileSystemView.getFileSystemView().getSystemDisplayName(
33
* return TO_STRING.getString(value);
37
* IconValue iv = new IconValue() {
39
* public Icon getIcon(Object value) {
40
* if (value instanceof File) {
41
* return FileSystemView.getFileSystemView().getSystemIcon(
47
* TreeCellRenderer r = new DefaultTreeRenderer(iv, sv);
48
* tree.setCellRenderer(r);
49
* treeTable.setTreeCellRenderer(r);
52
* PENDING: ui specific focus rect variation (draw rect around icon) missing
55
public class WrappingProvider extends
56
ComponentProvider<WrappingIconPanel> implements RolloverRenderer {
58
protected ComponentProvider wrappee;
61
* Instantiates a WrappingProvider with default LabelProvider.
64
public WrappingProvider() {
65
this((ComponentProvider) null);
69
* Instantiates a WrappingProvider with default wrappee. Uses the
70
* given IconValue to configure the icon.
72
* @param iconValue the IconValue to use for configuring the icon.
74
public WrappingProvider(IconValue iconValue, StringValue wrappeeStringValue) {
75
this(wrappeeStringValue);
76
setToStringConverter(new MappedValue(null, iconValue));
80
* Instantiates a WrappingProvider with default wrappee. Uses the
81
* given IconValue to configure the icon.
83
* @param iconValue the IconValue to use for configuring the icon.
85
public WrappingProvider(IconValue iconValue) {
87
setToStringConverter(new MappedValue(null, iconValue));
91
* Instantiates a WrappingProvider with default wrappee configured
92
* with the given StringValue.
94
* PENDING: we have a slight semantic glitch compared to super because
95
* the given StringValue is <b>not</b> for use in this provider but for use
98
* @param wrappeeStringValue the StringValue to use in the wrappee.
100
public WrappingProvider(StringValue wrappeeStringValue) {
101
this(new LabelProvider(wrappeeStringValue));
105
* Instantiates a WrappingProvider with the given delegate
106
* provider for the node content. If null, a default
107
* LabelProvider will be used.
109
* @param delegate the provider to use as delegate
111
public WrappingProvider(ComponentProvider delegate) {
113
// PENDING JW: this is inherently unsafe - must not call
114
// non-final methods from constructor
115
setWrappee(delegate);
116
setToStringConverter(StringValue.EMPTY);
120
* Sets the given provider as delegate for the node content.
121
* If the delegate is null, a default LabelProvider is set.<p>
123
* PENDING: rename to setDelegate?
125
* @param delegate the provider to use as delegate.
127
public void setWrappee(ComponentProvider delegate) {
128
if (delegate == null) {
129
delegate = new LabelProvider();
131
this.wrappee = delegate;
132
rendererComponent.setComponent(delegate.rendererComponent);
136
* Returns the delegate provider used to render the node content.
138
* @return the provider used for rendering the node content.
140
public ComponentProvider getWrappee() {
148
* Overridden to comply to contract: returns the string representation as
149
* provided by the wrappee (as this level has no string rep). Must do the
150
* same unwrapping magic as in configuring the rendering component. Here:
151
* unwraps userObject of DefaultMutableTreeNode and TreeTableNode.<p>
153
* PENDING JW: factor the unwrapping into one place.
157
public String getString(Object value) {
158
if (value instanceof DefaultMutableTreeNode) {
159
value = ((DefaultMutableTreeNode) value).getUserObject();
160
} else if (value instanceof TreeTableNode) {
161
TreeTableNode node = (TreeTableNode) value;
162
value = node.getUserObject();
164
return wrappee.getString(value);
171
public WrappingIconPanel getRendererComponent(CellContext context) {
172
if (context != null) {
173
rendererComponent.setComponent(wrappee.rendererComponent);
174
Object oldValue = adjustContextValue(context);
175
super.getRendererComponent(context);
176
wrappee.getRendererComponent(context);
177
restoreContextValue(context, oldValue);
178
return rendererComponent;
180
return super.getRendererComponent(context);
184
* Restores the context value to the old value.
186
* @param context the CellContext to restore.
187
* @param oldValue the value to restore the context to.
189
protected void restoreContextValue(CellContext context, Object oldValue) {
190
context.value = oldValue;
194
* Replace the context's value with the userobject
195
* if it's a treenode. <p>
196
* Subclasses may override but must guarantee to return the original
197
* value for restoring.
199
* @param context the context to adjust
200
* @return the old context value
202
protected Object adjustContextValue(CellContext context) {
203
Object oldValue = context.getValue();
204
if (oldValue instanceof DefaultMutableTreeNode) {
205
context.value = ((DefaultMutableTreeNode) oldValue).getUserObject();
206
} else if (oldValue instanceof TreeTableNode) {
207
TreeTableNode node = (TreeTableNode) oldValue;
208
context.value = node.getUserObject();
215
protected void configureState(CellContext context) {
216
rendererComponent.setBorder(BorderFactory.createEmptyBorder());
222
// private boolean isBorderAroundIcon() {
223
// return Boolean.TRUE.equals(UIManager.get("Tree.drawsFocusBorderAroundIcon"));
227
protected WrappingIconPanel createRendererComponent() {
228
return new WrappingIconPanel();
234
* Here: implemented to set the icon.
237
protected void format(CellContext context) {
238
rendererComponent.setIcon(getValueAsIcon(context));
244
* Overridden to fallback to the default icons supplied by the
245
* context if super returns null.
249
protected Icon getValueAsIcon(CellContext context) {
250
Icon icon = super.getValueAsIcon(context);
252
return context.getIcon();
254
return IconValue.NULL_ICON == icon ? null : icon;
257
//----------------- implement RolloverController
263
public void doClick() {
265
((RolloverRenderer) wrappee).doClick();
272
public boolean isEnabled() {
273
return (wrappee instanceof RolloverRenderer) &&
274
((RolloverRenderer) wrappee).isEnabled();