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 javax.swing.tree.DefaultMutableTreeNode;
025    import javax.swing.tree.DefaultTreeModel;
026    
027    import csheets.core.Cell;
028    
029    /**
030     * A tree node for a cell, to which child nodes are added dynamically
031     * when the node is expanded.
032     * @author Einar Pehrson
033     */
034    public abstract class CellNode extends DefaultMutableTreeNode {
035    
036            /** The cell of the node. */
037            private Cell cell;
038    
039            /** If child nodes for the node's references have been added. */
040            private boolean populated = false;
041    
042            /** The data model to which the node belongs */
043            protected DefaultTreeModel treeModel;
044    
045            /**
046             * Creates a new cell node.
047             * @param cell the cell of the node
048             * @param treeModel the data model to which the node belongs
049             */
050            public CellNode(Cell cell, DefaultTreeModel treeModel) {
051                    super(cell);
052    
053                    // Stores members
054                    this.cell = cell;
055                    this.treeModel = treeModel;
056            }
057    
058            /**
059             * Returns the cell of the node.
060             * @return the cell of the node.
061             */
062            public Cell getCell() {
063                    return cell;
064            }
065    
066            /**
067             * Populates the cell node by adding child nodes for all its references.
068             */
069            public void populate() {
070                    if (!populated) {
071                            populated = true;
072                            addChildren();
073                            treeModel.nodeStructureChanged(this);
074                    }
075            }
076    
077            /**
078             * Adds children to the node. Invoked once, when the node is populated.
079             */
080            protected abstract void addChildren();
081    }