~ubuntu-branches/ubuntu/trusty/libswingx-java/trusty

« back to all changes in this revision

Viewing changes to src/java/org/jdesktop/swingx/renderer/WrappingProvider.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2008-03-08 16:18:24 UTC
  • Revision ID: james.westby@ubuntu.com-20080308161824-wsahvl9pwzjcea3g
Tags: upstream-0.9.2
ImportĀ upstreamĀ versionĀ 0.9.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Created on 08.01.2007
 
3
 *
 
4
 */
 
5
package org.jdesktop.swingx.renderer;
 
6
 
 
7
import javax.swing.BorderFactory;
 
8
import javax.swing.Icon;
 
9
import javax.swing.tree.DefaultMutableTreeNode;
 
10
 
 
11
import org.jdesktop.swingx.RolloverRenderer;
 
12
import org.jdesktop.swingx.treetable.TreeTableNode;
 
13
 
 
14
 
 
15
/**
 
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.
 
20
 * <p>
 
21
 * 
 
22
 * An example of how to configure a file tree by using the system icons and
 
23
 * display names
 
24
 * 
 
25
 * <pre><code>
 
26
 * StringValue sv = new StringValue() {
 
27
 * 
 
28
 *     public String getString(Object value) {
 
29
 *         if (value instanceof File) {
 
30
 *             return FileSystemView.getFileSystemView().getSystemDisplayName(
 
31
 *                     (File) value);
 
32
 *         }
 
33
 *         return TO_STRING.getString(value);
 
34
 *     }
 
35
 * 
 
36
 * };
 
37
 * IconValue iv = new IconValue() {
 
38
 * 
 
39
 *     public Icon getIcon(Object value) {
 
40
 *         if (value instanceof File) {
 
41
 *             return FileSystemView.getFileSystemView().getSystemIcon(
 
42
 *                     (File) value);
 
43
 *         }
 
44
 *         return null;
 
45
 *     }
 
46
 * };
 
47
 * TreeCellRenderer r = new DefaultTreeRenderer(iv, sv);
 
48
 * tree.setCellRenderer(r);
 
49
 * treeTable.setTreeCellRenderer(r);
 
50
 * </code></pre>
 
51
 * 
 
52
 * PENDING: ui specific focus rect variation (draw rect around icon) missing
 
53
 * <p>
 
54
 */
 
55
public class WrappingProvider extends 
 
56
    ComponentProvider<WrappingIconPanel>  implements RolloverRenderer {
 
57
 
 
58
    protected ComponentProvider wrappee;
 
59
 
 
60
    /**
 
61
     * Instantiates a WrappingProvider with default LabelProvider.
 
62
     * 
 
63
     */
 
64
    public WrappingProvider() {
 
65
        this((ComponentProvider) null);
 
66
    }
 
67
 
 
68
    /**
 
69
     * Instantiates a WrappingProvider with default wrappee. Uses the 
 
70
     * given IconValue to configure the icon. 
 
71
     * 
 
72
     * @param iconValue the IconValue to use for configuring the icon.
 
73
     */
 
74
    public WrappingProvider(IconValue iconValue, StringValue wrappeeStringValue) {
 
75
        this(wrappeeStringValue);
 
76
        setToStringConverter(new MappedValue(null, iconValue));
 
77
    }
 
78
 
 
79
    /**
 
80
     * Instantiates a WrappingProvider with default wrappee. Uses the 
 
81
     * given IconValue to configure the icon. 
 
82
     * 
 
83
     * @param iconValue the IconValue to use for configuring the icon.
 
84
     */
 
85
    public WrappingProvider(IconValue iconValue) {
 
86
        this();
 
87
        setToStringConverter(new MappedValue(null, iconValue));
 
88
    }
 
89
   
 
90
    /**
 
91
     * Instantiates a WrappingProvider with default wrappee configured
 
92
     * with the given StringValue. 
 
93
     * 
 
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 
 
96
     * in the wrappee!
 
97
     * 
 
98
     * @param wrappeeStringValue the StringValue to use in the wrappee.
 
99
     */
 
100
    public WrappingProvider(StringValue wrappeeStringValue) {
 
101
        this(new LabelProvider(wrappeeStringValue));
 
102
    }
 
103
 
 
104
    /**
 
105
     * Instantiates a WrappingProvider with the given delegate
 
106
     * provider for the node content. If null, a default 
 
107
     * LabelProvider will be used. 
 
108
     * 
 
109
     * @param delegate the provider to use as delegate
 
110
     */
 
111
    public WrappingProvider(ComponentProvider delegate) {
 
112
        super();
 
113
        // PENDING JW: this is inherently unsafe - must not call 
 
114
        // non-final methods from constructor
 
115
        setWrappee(delegate);
 
116
        setToStringConverter(StringValue.EMPTY);
 
117
    }
 
118
    
 
119
    /**
 
120
     * Sets the given provider as delegate for the node content. 
 
121
     * If the delegate is null, a default LabelProvider is set.<p>
 
122
     * 
 
123
     *  PENDING: rename to setDelegate?
 
124
     *  
 
125
     * @param delegate the provider to use as delegate. 
 
126
     */
 
127
    public void setWrappee(ComponentProvider delegate) {
 
128
        if (delegate == null) {
 
129
            delegate = new LabelProvider();
 
130
        }
 
131
        this.wrappee = delegate;
 
132
        rendererComponent.setComponent(delegate.rendererComponent);
 
133
    }
 
134
 
 
135
    /**
 
136
     * Returns the delegate provider used to render the node content.
 
137
     * 
 
138
     * @return the provider used for rendering the node content.
 
139
     */
 
140
    public ComponentProvider getWrappee() {
 
141
        return wrappee;
 
142
    }
 
143
    
 
144
    
 
145
    /**
 
146
     * {@inheritDoc} <p>
 
147
     * 
 
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>
 
152
     * 
 
153
     * PENDING JW: factor the unwrapping into one place.
 
154
     * 
 
155
     */
 
156
    @Override
 
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();
 
163
        }
 
164
        return wrappee.getString(value);
 
165
    }
 
166
 
 
167
    /**
 
168
     * {@inheritDoc}
 
169
     */
 
170
    @Override
 
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;
 
179
        }
 
180
        return super.getRendererComponent(context);
 
181
    }
 
182
 
 
183
    /**
 
184
     * Restores the context value to the old value.
 
185
     * 
 
186
     * @param context the CellContext to restore.
 
187
     * @param oldValue the value to restore the context to.
 
188
     */
 
189
    protected void restoreContextValue(CellContext context, Object oldValue) {
 
190
        context.value = oldValue;
 
191
    }
 
192
 
 
193
    /**
 
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. 
 
198
     * 
 
199
     * @param context the context to adjust
 
200
     * @return the old context value
 
201
     */
 
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();
 
209
            
 
210
        }
 
211
        return oldValue;
 
212
    }
 
213
 
 
214
    @Override
 
215
    protected void configureState(CellContext context) {
 
216
        rendererComponent.setBorder(BorderFactory.createEmptyBorder());
 
217
    }
 
218
 
 
219
//    /**
 
220
//     * @return
 
221
//     */
 
222
//    private boolean isBorderAroundIcon() {
 
223
//        return Boolean.TRUE.equals(UIManager.get("Tree.drawsFocusBorderAroundIcon"));
 
224
//    }
 
225
 
 
226
    @Override
 
227
    protected WrappingIconPanel createRendererComponent() {
 
228
        return new WrappingIconPanel();
 
229
    }
 
230
 
 
231
    /**
 
232
     * {@inheritDoc} <p>
 
233
     * 
 
234
     * Here: implemented to set the icon.
 
235
     */
 
236
    @Override
 
237
    protected void format(CellContext context) {
 
238
        rendererComponent.setIcon(getValueAsIcon(context));
 
239
    }
 
240
 
 
241
    /**
 
242
     * {@inheritDoc} <p>
 
243
     * 
 
244
     * Overridden to fallback to the default icons supplied by the 
 
245
     * context if super returns null.
 
246
     *   
 
247
     */
 
248
    @Override
 
249
    protected Icon getValueAsIcon(CellContext context) {
 
250
        Icon icon = super.getValueAsIcon(context);
 
251
        if (icon == null) {
 
252
            return context.getIcon();
 
253
        }
 
254
        return IconValue.NULL_ICON == icon ? null : icon;
 
255
    }
 
256
    
 
257
    //----------------- implement RolloverController
 
258
    
 
259
 
 
260
    /**
 
261
     * {@inheritDoc}
 
262
     */
 
263
    public void doClick() {
 
264
        if (isEnabled()) {
 
265
            ((RolloverRenderer) wrappee).doClick(); 
 
266
        }
 
267
    }
 
268
 
 
269
    /**
 
270
     * {@inheritDoc}
 
271
     */
 
272
    public boolean isEnabled() {
 
273
        return (wrappee instanceof RolloverRenderer) && 
 
274
           ((RolloverRenderer) wrappee).isEnabled();
 
275
    }
 
276
 
 
277
 
 
278
    
 
279
}