001    /*
002     * Copyright 2002-2004 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.tree;
017    
018    import java.awt.Component;
019    import java.awt.Image;
020    import java.beans.BeanInfo;
021    import java.beans.IntrospectionException;
022    import java.beans.Introspector;
023    
024    import javax.swing.ImageIcon;
025    import javax.swing.JTree;
026    import javax.swing.tree.DefaultMutableTreeNode;
027    
028    import org.springframework.beans.BeanUtils;
029    import org.springframework.beans.BeanWrapper;
030    import org.springframework.beans.BeanWrapperImpl;
031    import org.springframework.beans.FatalBeanException;
032    import org.springframework.richclient.core.DescribedElement;
033    import org.springframework.richclient.core.VisualizedElement;
034    import org.springframework.util.Assert;
035    
036    /**
037     * 
038     * @author Keith Donald
039     */
040    public class BeanTreeCellRenderer extends FocusableTreeCellRenderer {
041        private BeanInfo beanInfo;
042    
043        private String propertyName;
044    
045        public BeanTreeCellRenderer() {
046    
047        }
048    
049        public BeanTreeCellRenderer(Class beanClass) {
050            this(beanClass, null);
051        }
052    
053        public BeanTreeCellRenderer(Class beanClass, String propertyName) {
054            Assert.notNull(beanClass);
055            try {
056                this.beanInfo = Introspector.getBeanInfo(beanClass);
057            }
058            catch (IntrospectionException e) {
059                throw new RuntimeException(e);
060            }
061            this.propertyName = propertyName;
062        }
063    
064        /**
065         * @see javax.swing.tree.TreeCellRenderer#getTreeCellRendererComponent(javax.swing.JTree,
066         *      java.lang.Object, boolean, boolean, boolean, int, boolean)
067         */
068        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded,
069                boolean leaf, int row, boolean hasFocus) {
070            super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
071            if (value instanceof DefaultMutableTreeNode) {
072                DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
073                Object bean = node.getUserObject();
074                if (bean != null && !BeanUtils.isSimpleProperty(bean.getClass())) {
075                    if (bean instanceof DescribedElement) {
076                        DescribedElement element = (DescribedElement)bean;
077                        setText(element.getDisplayName());
078                        setToolTipText(element.getCaption());
079                    }
080                    else {
081                        BeanWrapper wrapper = new BeanWrapperImpl(bean);
082                        try {
083                            Object text = propertyName != null ? wrapper.getPropertyValue(propertyName) : wrapper
084                                    .getPropertyValue("name");
085                            setText(String.valueOf(text));
086                        }
087                        catch (FatalBeanException e) {
088    
089                        }
090                    }
091    
092                    if (bean instanceof VisualizedElement) {
093                        VisualizedElement element = (VisualizedElement) bean;
094                        setIcon(element.getIcon());
095                    }
096                    else {
097                        if (beanInfo != null) {
098                            Image icon = beanInfo.getIcon(BeanInfo.ICON_COLOR_16x16);
099                            if (icon != null) {
100                                setIcon(new ImageIcon(icon));
101                            }
102                        }
103                    }
104                    
105                }
106            }
107            return this;
108        }
109    }