001 /* 002 * $Id: DefaultTreeRenderer.java 3286 2009-03-10 12:13:43Z kleopatra $ 003 * 004 * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle, 005 * Santa Clara, California 95054, U.S.A. All rights reserved. 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * You should have received a copy of the GNU Lesser General Public 018 * License along with this library; if not, write to the Free Software 019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 020 */ 021 package org.jdesktop.swingx.renderer; 022 023 024 import java.awt.Component; 025 026 import javax.swing.JTree; 027 import javax.swing.tree.TreeCellRenderer; 028 029 030 /** 031 * Adapter to glue SwingX renderer support to core api. 032 * <p> 033 * 034 * 035 * @author Jeanette Winzenburg 036 * 037 * 038 */ 039 public class DefaultTreeRenderer extends AbstractRenderer 040 implements TreeCellRenderer { 041 042 private TreeCellContext cellContext; 043 044 /** 045 * Instantiates a default tree renderer with the default component 046 * provider. 047 * 048 */ 049 public DefaultTreeRenderer() { 050 this((ComponentProvider)null); 051 } 052 053 054 /** 055 * Instantiates a default tree renderer with the given component provider. 056 * If the controller is null, creates and uses a default. The default 057 * controller is of type <code>WrappingProvider</code>. 058 * 059 * @param componentProvider the provider of the configured component to 060 * use for cell rendering 061 */ 062 public DefaultTreeRenderer(ComponentProvider componentProvider) { 063 super(componentProvider); 064 this.cellContext = new TreeCellContext(); 065 } 066 067 /** 068 * Instantiates a default tree renderer with the default 069 * wrapping provider, using the given IconValue for 070 * customizing the icons. 071 * 072 * @param iv the IconValue to use for mapping a custom icon 073 * for a given value 074 * 075 */ 076 public DefaultTreeRenderer(IconValue iv) { 077 this(new WrappingProvider(iv)); 078 } 079 080 /** 081 * Instantiates a default tree renderer with a default component 082 * provider using the given converter. 083 * 084 * @param sv the converter to use for mapping the 085 * content value to a String representation. 086 * 087 */ 088 public DefaultTreeRenderer(StringValue sv) { 089 this(new WrappingProvider(sv)); 090 } 091 092 093 /** 094 * Instantiates a default tree renderer with the default 095 * wrapping provider, using the given IconValue for 096 * customizing the icons and the given StringValue for 097 * node content. 098 * 099 * @param iv the IconValue to use for mapping a custom icon 100 * for a given value 101 * @param sv the converter to use for mapping the 102 * content value to a String representation. 103 * 104 */ 105 public DefaultTreeRenderer(IconValue iv, StringValue sv) { 106 this(new WrappingProvider(iv, sv)); 107 } 108 109 /** 110 * Instantiates a default tree renderer with the default 111 * wrapping provider, using the given IconValue for 112 * customizing the icons and the given StringValue for 113 * node content. 114 * 115 * @param iv the IconValue to use for mapping a custom icon 116 * for a given value 117 * @param sv the converter to use for mapping the 118 * content value to a String representation. 119 * @param unwrapUserObject a flag indicating whether this provider 120 * should auto-unwrap the userObject from the context value. 121 * 122 */ 123 public DefaultTreeRenderer(IconValue iv, StringValue sv, boolean unwrapUserObject) { 124 this(new WrappingProvider(iv, sv, unwrapUserObject)); 125 } 126 127 // -------------- implements javax.swing.table.TableCellRenderer 128 /** 129 * 130 * Returns a configured component, appropriate to render the given tree 131 * cell. 132 * 133 * @param tree the <code>JTree</code> 134 * @param value the value to assign to the cell 135 * @param selected true if cell is selected 136 * @param expanded true if the cell is expanded 137 * @param leaf true if the cell is a leaf 138 * @param hasFocus true if cell has focus 139 * @param row the row of the cell to render 140 * @return a component to render the given list cell. 141 */ 142 public Component getTreeCellRendererComponent(JTree tree, Object value, 143 boolean selected, boolean expanded, boolean leaf, int row, 144 boolean hasFocus) { 145 cellContext.installContext(tree, value, row, 0, selected, hasFocus, 146 expanded, leaf); 147 Component comp = componentController.getRendererComponent(cellContext); 148 // fix issue #1040-swingx: memory leak if value not released 149 cellContext.replaceValue(null); 150 return comp; 151 } 152 153 154 /** 155 * {@inheritDoc} 156 */ 157 @Override 158 protected ComponentProvider createDefaultComponentProvider() { 159 return new WrappingProvider(); 160 } 161 162 163 } 164 165