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     */
022    package csheets.ext.deptree;
023    
024    import java.util.SortedSet;
025    import java.util.TreeSet;
026    
027    import javax.swing.tree.DefaultTreeModel;
028    
029    import csheets.core.Cell;
030    import csheets.core.formula.Reference;
031    import csheets.core.formula.lang.CellReference;
032    import csheets.ui.ctrl.UIController;
033    
034    /**
035     * A tree node for a cell, to which the cell's precedents are added dynamically
036     * when the node is expanded.
037     * @author Einar Pehrson
038     */
039    @SuppressWarnings("serial")
040    public class PrecedentsNode extends CellNode {
041    
042            /** The references of the node's cell. */
043            private SortedSet<Reference> references = new TreeSet<Reference>();
044    
045            /** The user interface controller */
046            private UIController uiController;
047    
048            /**
049             * Creates a new precedents node.
050             * @param cell the cell of the node
051             * @param treeModel the data model to which the node belongs
052             * @param uiController the user interface controller
053             */
054            public PrecedentsNode(Cell cell, DefaultTreeModel treeModel,
055                            UIController uiController) {
056                    super(cell, treeModel);
057                    this.uiController = uiController;
058                    if (cell.getFormula() != null)
059                            references = cell.getFormula().getReferences();
060            }
061    
062            protected void addChildren() {
063                    for (Reference reference : references) {
064                            if (reference instanceof CellReference) {
065                                    Cell cell = ((CellReference)reference).getCell();
066                                    add(new PrecedentsNode(cell, treeModel, uiController));
067                            } else
068                                    add(new ReferenceNode(reference, getCell().getSpreadsheet(),
069                                            treeModel, uiController));
070                    }
071            }
072    
073            /**
074             * Returns whether the cell has references.
075             * @return true if the cell has references
076             */
077            public boolean isLeaf() {
078                    return references.isEmpty();
079            }
080    }