001    /*
002     * Copyright (c) 2005 Einar Pehrson <einar@pehrson.nu>.
003     *
004     * This file is part of
005     * CleanSheets - a spreadsheet application for the Java platform.
006     *
007     * CleanSheets is free software; you can redistribute it and/or modify
008     * it under the terms of the GNU General Public License as published by
009     * the Free Software Foundation; either version 2 of the License, or
010     * (at your option) any later version.
011     *
012     * CleanSheets 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
015     * 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; if not, write to the Free Software
019     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
020     */
021    package csheets.ext;
022    
023    import java.util.SortedSet;
024    import java.util.TreeSet;
025    
026    import csheets.core.Address;
027    import csheets.core.Cell;
028    import csheets.core.CellListener;
029    import csheets.core.Spreadsheet;
030    import csheets.core.Value;
031    import csheets.core.formula.Formula;
032    import csheets.core.formula.compiler.FormulaCompilationException;
033    
034    /**
035     * A base class for extensions of cells in a spreadsheet that uses delegation
036     * to provide cell data.
037     * @author Einar Pehrson
038     */
039    public abstract class CellExtension implements Cell, CellListener {
040    
041            /** The delegate of the extension */
042            private Cell delegate;
043    
044            /** The name of the extension to which the cell extension belongs */
045            private String name;
046    
047            /**
048             * Creates a new cell extension.
049             * @param delegate the delegate of the extension
050             * @param name the name of the extension to which the cell extension belongs
051             */
052            public CellExtension(Cell delegate, String name) {
053                    this.delegate = delegate;
054                    this.name = name;
055                    delegate.addCellListener(this);
056            }
057    
058            /**
059             * Returns the extension's delegate.
060             * @return the extension's delegate, i.e. the cell to which it belongs
061             */
062            public final Cell getDelegate() {
063                    return delegate;
064            }
065    
066            /**
067             * Returns the name of the extension to which the cell extension belongs.
068             * @return the name of the extension to which the cell extension belongs
069             */
070            public final String getName() {
071                    return name;
072            }
073    
074            public final Spreadsheet getSpreadsheet() {
075                    Spreadsheet spreadsheet = delegate.getSpreadsheet();
076                    Spreadsheet extension = spreadsheet.getExtension(name);
077                    if (extension != null)
078                            return extension;
079                    else
080                            return spreadsheet;
081            }
082    
083            public final Address getAddress() {
084                    return delegate.getAddress();
085            }
086    
087            public final Value getValue() {
088                    return delegate.getValue();
089            }
090    
091            public final String getContent() {
092                    return delegate.getContent();
093            }
094    
095            public final Formula getFormula() {
096                    return delegate.getFormula();
097            }
098    
099            public final void setContent(String content) throws FormulaCompilationException {
100                    // Disallow?
101                    delegate.setContent(content);
102            }
103    
104            public void clear() {
105                    delegate.clear();
106            }
107    
108            public final SortedSet<Cell> getPrecedents() {
109                    SortedSet<Cell> cellExts = new TreeSet<Cell>();
110                    for (Cell cell : delegate.getPrecedents())
111                            cellExts.add(cell.getExtension(getName()));
112                    return cellExts;
113            }
114    
115            public final SortedSet<Cell> getDependents()  {
116                    SortedSet<Cell> cellExts = new TreeSet<Cell>();
117                    for (Cell cell : delegate.getDependents())
118                            cellExts.add(cell.getExtension(getName()));
119                    return cellExts;
120            }
121    
122            public final void copyFrom(Cell source) {
123                    delegate.copyFrom(source);
124            }
125    
126            public final void moveFrom(Cell source) {
127                    delegate.moveFrom(source);
128            }
129    
130            public final void addCellListener(CellListener listener) {
131                    delegate.addCellListener(listener);
132            }
133    
134            public final void removeCellListener(CellListener listener) {
135                    delegate.removeCellListener(listener);
136            }
137    
138            public final CellListener[] getCellListeners() {
139                    return delegate.getCellListeners();
140            }
141    
142            public final Cell getExtension(String name) {
143                    return delegate.getExtension(name);
144            }
145    
146            public final int compareTo(Cell cell) {
147                    return delegate.compareTo(cell);
148            }
149    
150            public final String toString() {
151                    return delegate.toString();
152            }
153    
154            public void valueChanged(Cell cell) {}
155    
156            public void contentChanged(Cell cell) {}
157    
158            public void dependentsChanged(Cell cell) {}
159    
160            public void cellCleared(Cell cell) {}
161    
162            public void cellCopied(Cell cell, Cell source) {}
163    }