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.core;
022:
023: import java.io.IOException;
024: import java.io.ObjectInputStream;
025: import java.io.Serializable;
026: import java.util.ArrayList;
027: import java.util.Iterator;
028: import java.util.List;
029:
030: /**
031: * A workbook which can contain several spreadsheets.
032: * @author Einar Pehrson
033: */
034: public class Workbook implements Iterable<Spreadsheet>, Serializable {
035:
036: /** The unique version identifier used for serialization */
037: private static final long serialVersionUID = -6324252462576447242L;
038:
039: /** The spreadsheets of which the workbook consists */
040: private List<Spreadsheet> spreadsheets = new ArrayList<Spreadsheet>();
041:
042: /** The cell listeners that have been registered on the cell */
043: private transient List<WorkbookListener> listeners = new ArrayList<WorkbookListener>();
044:
045: /** The number of spreadsheets that have been created in the workbook */
046: private int createdSpreadsheets;
047:
048: /**
049: * Creates a new empty workbook.
050: */
051: public Workbook() {
052: }
053:
054: /**
055: * Creates a new workbook, which initially contains the given number
056: * of blank spreadsheets.
057: * @param sheets the number of sheets to create initially
058: */
059: public Workbook(int sheets) {
060: for (int i = 0; i < sheets; i++)
061: spreadsheets.add(new SpreadsheetImpl(this ,
062: getNextSpreadsheetTitle()));
063: }
064:
065: /**
066: * Creates a new workbook, using the given content matrix to create
067: * spreadsheets initially.
068: * @param contents the content matrices to use when creating spreadsheets
069: */
070: public Workbook(String[][]... contents) {
071: for (String[][] content : contents)
072: spreadsheets.add(new SpreadsheetImpl(this ,
073: getNextSpreadsheetTitle(), content));
074: }
075:
076: /**
077: * Adds a blank spreadsheet to the end of the workbook.
078: */
079: public void addSpreadsheet() {
080: Spreadsheet spreadsheet = new SpreadsheetImpl(this ,
081: getNextSpreadsheetTitle());
082: spreadsheets.add(spreadsheet);
083: fireSpreadsheetInserted(spreadsheet, spreadsheets.size() - 1);
084: }
085:
086: /**
087: * Adds a new spreadsheet to the workbook, in which cells are initialized
088: * with data from the given content matrix.
089: * @param content the contents of the cells in the spreadsheet
090: */
091: public void addSpreadsheet(String[][] content) {
092: Spreadsheet spreadsheet = new SpreadsheetImpl(this ,
093: getNextSpreadsheetTitle(), content);
094: spreadsheets.add(spreadsheet);
095: fireSpreadsheetInserted(spreadsheet, spreadsheets.size() - 1);
096: }
097:
098: /**
099: * Returns the title to be used for the next spreadsheet added.
100: * @return the title to be used for the next spreadsheet added
101: */
102: private String getNextSpreadsheetTitle() {
103: return SpreadsheetImpl.BASE_TITLE + " "
104: + (createdSpreadsheets++ + 1);
105: }
106:
107: /**
108: * Adds a new blank spreadsheet to the workbook.
109: */
110: public void removeSpreadsheet(Spreadsheet spreadsheet) {
111: spreadsheets.remove(spreadsheet);
112: // Remove references to the spreadsheet in remaining spreadsheets!
113: fireSpreadsheetRemoved(spreadsheet);
114: }
115:
116: /**
117: * Returns the spreadsheet at the given index.
118: * @param index the index of the spreadsheet in the workbook
119: * @return the spreadsheet at the given index
120: * @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index >= |spreadsheets|)
121: */
122: public Spreadsheet getSpreadsheet(int index)
123: throws IndexOutOfBoundsException {
124: return spreadsheets.get(index);
125: }
126:
127: /**
128: * Returns the number of spreadsheets in the the workbook.
129: * @return the number of spreadsheets in the the workbook
130: */
131: public int getSpreadsheetCount() {
132: return spreadsheets.size();
133: }
134:
135: /**
136: * Returns an iterator over the spreadsheets in the workbook.
137: * @return an iterator over the spreadsheets in the workbook
138: */
139: public Iterator<Spreadsheet> iterator() {
140: return spreadsheets.iterator();
141: }
142:
143: /*
144: * EVENT HANDLING
145: */
146:
147: /**
148: * Registers the given listener on the workbook.
149: * @param listener the listener to be added
150: */
151: public void addWorkbookListener(WorkbookListener listener) {
152: listeners.add(listener);
153: }
154:
155: /**
156: * Removes the given listener from the workbook.
157: * @param listener the listener to be removed
158: */
159: public void removeWorkbookListener(WorkbookListener listener) {
160: listeners.remove(listener);
161: }
162:
163: /**
164: * Returns the listeners that have been registered on the workbook.
165: * @return the listeners that have been registered on the workbook
166: */
167: public WorkbookListener[] getWorkbookListeners() {
168: return listeners
169: .toArray(new WorkbookListener[listeners.size()]);
170: }
171:
172: /**
173: * Notifies all registered listeners that a spreadsheet has been inserted.
174: * @param spreadsheet the spreadsheet that was inserted
175: * @param index the index at which the spreadsheet was inserted
176: */
177: private void fireSpreadsheetInserted(Spreadsheet spreadsheet,
178: int index) {
179: for (WorkbookListener listener : listeners)
180: listener.spreadsheetInserted(spreadsheet, index);
181: }
182:
183: /**
184: * Notifies all registered listeners that a spreadsheet has been removed.
185: * @param spreadsheet the spreadsheet that was removed
186: */
187: private void fireSpreadsheetRemoved(Spreadsheet spreadsheet) {
188: for (WorkbookListener listener : listeners)
189: listener.spreadsheetRemoved(spreadsheet);
190: }
191:
192: /**
193: * Notifies all registered listeners that a spreadsheet has been renamed.
194: * @param spreadsheet the spreadsheet that was renamed
195: */
196: @SuppressWarnings("unused")
197: private void fireSpreadsheetRenamed(Spreadsheet spreadsheet) {
198: for (WorkbookListener listener : listeners)
199: listener.spreadsheetRenamed(spreadsheet);
200: }
201:
202: /*
203: * GENERAL
204: */
205:
206: /**
207: * Customizes deserialization by recreating the listener list.
208: * @param stream the object input stream from which the object is to be read
209: * @throws IOException If any of the usual Input/Output related exceptions occur
210: * @throws ClassNotFoundException If the class of a serialized object cannot be found.
211: */
212: private void readObject(ObjectInputStream stream)
213: throws IOException, ClassNotFoundException {
214: stream.defaultReadObject();
215: listeners = new ArrayList<WorkbookListener>();
216: }
217: }
|