001/*
002 * Copyright (c) 2005 Einar Pehrson, Malin Johansson and Sofia Nilsson
003 *
004 * This file is part of
005 * CleanSheets Extension for Dependency Trees
006 *
007 * CleanSheets Extension for Dependency Trees is free software; you can
008 * redistribute it and/or modify it under the terms of the GNU General Public
009 * License as published by the Free Software Foundation; either version 2 of
010 * the License, or (at your option) any later version.
011 *
012 * CleanSheets Extension for Dependency Trees is distributed in the hope that
013 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
014 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
015 * See the GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU General Public License
018 * along with CleanSheets Extension for Dependency Trees; if not, write to the
019 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020 * Boston, MA  02111-1307  USA
021 */
022package csheets.ext.deptree;
023
024import java.awt.Component;
025
026import javax.swing.Icon;
027import javax.swing.ImageIcon;
028import javax.swing.JTree;
029import javax.swing.tree.DefaultTreeCellRenderer;
030
031import csheets.core.Cell;
032
033/**
034 * The renderer used for nodes in dependency trees.
035 * @author Malin Johansson
036 * @author Sofia Nilsson
037 * @author Einar Pehrson
038 */
039@SuppressWarnings("serial")
040public class DependencyTreeCellRenderer extends DefaultTreeCellRenderer{
041        
042        /** An icon representing a node containing a formula */
043        private Icon formulaIcon = new ImageIcon(
044                DependencyTreeExtension.class.getResource("res/img/formula.gif"));
045        
046        /** An icon representing a cell containing a constant value */
047        private Icon literalIcon = new ImageIcon(
048                DependencyTreeExtension.class.getResource("res/img/literal.gif"));
049        
050        /** An icon representing a cell containing a range */
051        private Icon rangeIcon = new ImageIcon(
052                DependencyTreeExtension.class.getResource("res/img/range.gif"));
053        
054        /**
055         * Creates a new dependency tree cell renderer
056         */
057        public DependencyTreeCellRenderer() {}
058        
059        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, 
060                                                        boolean leaf, int row, boolean hasFocus) {
061
062                if (value instanceof CellNode) {
063                        // Selects the appropriate icon and tool tip to display
064                        Cell cell = ((CellNode)value).getCell();
065                        Icon icon;
066                        if (cell.getFormula() == null) {
067                                icon = literalIcon;
068                                setToolTipText(cell.getValue().toString());
069                        } else {
070                                icon = formulaIcon;
071                                setToolTipText(cell.getValue() + " = " + cell.getFormula());
072                        }
073
074                        // Updates the appropriate icon(s)
075                        if (leaf)
076                                setLeafIcon(icon);
077                        else {
078                                setOpenIcon(icon);
079                                setClosedIcon(icon);
080                        }
081                } else if (value instanceof ReferenceNode) {
082                        // Node is a reference to multiple cells, and therefore not a leaf
083                        setOpenIcon(rangeIcon);
084                        setClosedIcon(rangeIcon);
085                }
086
087                return super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
088        }
089}