001package csheets.ext.comments; 002 003import java.io.IOException; 004import java.util.ArrayList; 005import java.util.List; 006 007import csheets.core.Cell; 008import csheets.ext.CellExtension; 009 010/** 011 * An extension of a cell in a spreadsheet, with support for comments. 012 * @author Alexandre Braganca 013 * @author Einar Pehrson 014 */ 015public class CommentableCell extends CellExtension { 016 017 /** The unique version identifier used for serialization */ 018 private static final long serialVersionUID = 1L; 019 020 /** The cell's user-specified comment */ 021 private String userComment; 022 023 /** The listeners registered to receive events from the comentable cell */ 024 private transient List<CommentableCellListener> listeners 025 = new ArrayList<CommentableCellListener>(); 026 027 /** 028 * Creates a comentable cell extension for the given cell. 029 * @param cell the cell to extend 030 */ 031 CommentableCell(Cell cell) { 032 super(cell, CommentsExtension.NAME); 033 } 034 035 036/* 037 * DATA UPDATES 038 */ 039 040 041// public void contentChanged(Cell cell) { 042// } 043 044 045/* 046 * COMMENT ACCESSORS 047 */ 048 049 /** 050 * Get the cell's user comment. 051 * @return The user supplied comment for the cell or <code>null</code> if no user 052 supplied comment exists. 053 */ 054 public String getUserComment() { 055 return userComment; 056 } 057 058 /** 059 * Returns whether the cell has a comment. 060 * @return true if the cell has a comment 061 */ 062 public boolean hasComment() { 063 return userComment != null; 064 } 065 066/* 067 * COMMENT MODIFIERS 068 */ 069 070 /** 071 * Sets the user-specified comment for the cell. 072 * @param comment the user-specified comment 073 */ 074 public void setUserComment(String comment) { 075 this.userComment = comment; 076 // Notifies listeners 077 fireCommentsChanged(); 078 } 079 080 081/* 082 * EVENT LISTENING SUPPORT 083 */ 084 085 /** 086 * Registers the given listener on the cell. 087 * @param listener the listener to be added 088 */ 089 public void addCommentableCellListener(CommentableCellListener listener) { 090 listeners.add(listener); 091 } 092 093 /** 094 * Removes the given listener from the cell. 095 * @param listener the listener to be removed 096 */ 097 public void removeCommentableCellListener(CommentableCellListener listener) { 098 listeners.remove(listener); 099 } 100 101 /** 102 * Notifies all registered listeners that the cell's comments changed. 103 */ 104 protected void fireCommentsChanged() { 105 for (CommentableCellListener listener : listeners) 106 listener.commentChanged(this); 107 } 108 109 /** 110 * Customizes serialization, by recreating the listener list. 111 * @param stream the object input stream from which the object is to be read 112 * @throws IOException If any of the usual Input/Output related exceptions occur 113 * @throws ClassNotFoundException If the class of a serialized object cannot be found. 114 */ 115 private void readObject(java.io.ObjectInputStream stream) 116 throws java.io.IOException, ClassNotFoundException { 117 stream.defaultReadObject(); 118 listeners = new ArrayList<CommentableCellListener>(); 119 } 120}