001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.contrib.poibrowser;
019:
020: import java.awt.*;
021: import javax.swing.*;
022: import javax.swing.tree.*;
023: import java.util.*;
024:
025: /**
026: * <p>This is a {@link TreeCellRenderer} implementation which is able
027: * to render arbitrary objects. The {@link ExtendableTreeCellRenderer}
028: * does not do the rendering itself but instead dispatches to
029: * class-specific renderers. A class/renderer pair must be registered
030: * using the {@link #register} method. If a class has no registered
031: * renderer, the renderer of its closest superclass is used. Since the
032: * {@link ExtendableTreeCellRenderer} always has a default renderer
033: * for the {@link Object} class, rendering is always possible. The
034: * default {@link Object} renderer can be replaced by another renderer
035: * but it cannot be unregistered.</p>
036: *
037: * @author Rainer Klute <a
038: * href="mailto:klute@rainer-klute.de"><klute@rainer-klute.de></a>
039: * @version $Id: ExtendableTreeCellRenderer.java 489730 2006-12-22 19:18:16Z bayard $
040: * @since 2002-01-22
041: */
042: public class ExtendableTreeCellRenderer implements TreeCellRenderer {
043:
044: /**
045: * <p>Maps classes to renderers.</p>
046: */
047: protected Map renderers;
048:
049: public ExtendableTreeCellRenderer() {
050: renderers = new HashMap();
051: register(Object.class, new DefaultTreeCellRenderer() {
052: public Component getTreeCellRendererComponent(JTree tree,
053: Object value, boolean selected, boolean expanded,
054: boolean leaf, int row, boolean hasFocus) {
055: final String s = value.toString();
056: final JLabel l = new JLabel(s + " ");
057: if (selected) {
058: Util.invert(l);
059: l.setOpaque(true);
060: }
061: return l;
062: }
063: });
064: }
065:
066: /**
067: * <p>Registers a renderer for a class.</p>
068: **/
069: public void register(final Class c, final TreeCellRenderer renderer) {
070: renderers.put(c, renderer);
071: }
072:
073: /**
074: * <p>Unregisters a renderer for a class. The renderer for the
075: * {@link Object} class cannot be unregistered.</p>
076: */
077: public void unregister(final Class c) {
078: if (c == Object.class)
079: throw new IllegalArgumentException(
080: "Renderer for Object cannot be unregistered.");
081: renderers.put(c, null);
082: }
083:
084: /**
085: * <p>Renders an object in a tree cell depending of the object's
086: * class.</p>
087: *
088: * @see TreeCellRenderer#getTreeCellRendererComponent
089: */
090: public Component getTreeCellRendererComponent(final JTree tree,
091: final Object value, final boolean selected,
092: final boolean expanded, final boolean leaf, final int row,
093: final boolean hasFocus) {
094: final String NULL = "null";
095: TreeCellRenderer r;
096: Object userObject;
097: if (value == null)
098: userObject = NULL;
099: else {
100: userObject = ((DefaultMutableTreeNode) value)
101: .getUserObject();
102: if (userObject == null)
103: userObject = NULL;
104: }
105: r = findRenderer(userObject.getClass());
106: return r.getTreeCellRendererComponent(tree, value, selected,
107: expanded, leaf, row, hasFocus);
108: }
109:
110: /**
111: * <p>Find the renderer for the specified class.</p>
112: */
113: protected TreeCellRenderer findRenderer(final Class c) {
114: final TreeCellRenderer r = (TreeCellRenderer) renderers.get(c);
115: if (r != null)
116: /* The class has a renderer. */
117: return r;
118:
119: /* The class has no renderer, try the superclass, if any. */
120: final Class superclass = c.getSuperclass();
121: if (superclass != null)
122: return findRenderer(superclass);
123: else
124: return null;
125: }
126:
127: }
|