org.homeunix.thecave.moss.model
Class AbstractDocument

java.lang.Object
  extended by org.homeunix.thecave.moss.model.AbstractDocument
All Implemented Interfaces:
StandardDocument
Direct Known Subclasses:
DocumentImpl, LanguageEditorDocument

public abstract class AbstractDocument
extends java.lang.Object
implements StandardDocument

An abstract implementation of StandardDocument, which incldues some default methods. IMPORTANT: We use a weak hash map as backing for the DocumentChangeListener set. This means that you must keep a reference to the listener in each class which calls it. See addDocumentChangeListener() javadocs for more information.

Author:
wyatt

Constructor Summary
AbstractDocument()
           
 
Method Summary
 void addDocumentChangeListener(DocumentChangeListener listener)
          Registers this listener to recieve events when the document changes.
 void finishBatchChange()
          Turn on notification of changes again, and if the model has changed in the interim (and there are no more change events on the stack), fire an event.
protected  void fireDocumentChangeEvent()
           
 java.io.File getFile()
          Returns the file associated with this document
static java.lang.String getGeneratedUid(java.lang.Object object)
          Generate a UID string for a particular object.
 long getMinimumChangeEventPeriod()
          Returns the minimum amount of time (in millis) between fire events.
 boolean isBatchChange()
          Is there a batch change currently going on?
 boolean isChanged()
          Has the document changed since the last save
 void removeDocumentChangeListener(DocumentChangeListener listener)
          Unregisters this listener from recieving events when the document changes.
 void resetChanged()
          The document has not been changed since the last save.
 void setChanged()
          Indicates that the document has changed.
 void setFile(java.io.File file)
          Sets the file associated with this document
 void setMinimumChangeEventPeriod(long minimumChangeEventPeriod)
          Sets the minimum time (in millis) between fire events.
 void startBatchChange()
          Turn off notification of changes to the data model, pending the start of many change operations.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.homeunix.thecave.moss.model.StandardDocument
save, saveAs
 

Constructor Detail

AbstractDocument

public AbstractDocument()
Method Detail

isChanged

public boolean isChanged()
Has the document changed since the last save

Specified by:
isChanged in interface StandardDocument
Returns:

setChanged

public void setChanged()
Indicates that the document has changed.

Specified by:
setChanged in interface StandardDocument

resetChanged

public void resetChanged()
The document has not been changed since the last save.

Specified by:
resetChanged in interface StandardDocument

getFile

public java.io.File getFile()
Returns the file associated with this document

Specified by:
getFile in interface StandardDocument
Returns:

setFile

public void setFile(java.io.File file)
Sets the file associated with this document

Specified by:
setFile in interface StandardDocument
Parameters:
file -

getMinimumChangeEventPeriod

public long getMinimumChangeEventPeriod()
Returns the minimum amount of time (in millis) between fire events. If you try to fire more events than this, we will ignore them.

Returns:

setMinimumChangeEventPeriod

public void setMinimumChangeEventPeriod(long minimumChangeEventPeriod)
Sets the minimum time (in millis) between fire events. Defaults to 1000 (1 second).

Parameters:
minimumChangeEventPeriod -

startBatchChange

public void startBatchChange()
Turn off notification of changes to the data model, pending the start of many change operations. This is implemented as a stack; you must finish each of the change operations before you are able to continue processing events.

Specified by:
startBatchChange in interface StandardDocument

finishBatchChange

public void finishBatchChange()
Turn on notification of changes again, and if the model has changed in the interim (and there are no more change events on the stack), fire an event.

Specified by:
finishBatchChange in interface StandardDocument

isBatchChange

public boolean isBatchChange()
Is there a batch change currently going on?

Returns:

addDocumentChangeListener

public void addDocumentChangeListener(DocumentChangeListener listener)
Registers this listener to recieve events when the document changes. ***IMPORTANT*** This implementation of AbstractDocument uses a WeakHashMap as list backing. This means that when the only reference to the listener object remaining is in the map, it allows the object to be GC'd. If you add a listener to your object, you MUST include a reference to the listener in the object itself. This means you cannot just call abstractDocument.addDocumentChangeListener(new DocumentChangeListener(... . Instead, you must create a private field variable DocumentChangeListener l = new DocumentChangeListener(..., and then call abstractDocument.addDocumentChangeListener(l);. If you don't do this, your reference will be removed as soon as GC is invoked.

Specified by:
addDocumentChangeListener in interface StandardDocument
Parameters:
listener -

removeDocumentChangeListener

public void removeDocumentChangeListener(DocumentChangeListener listener)
Unregisters this listener from recieving events when the document changes.

Specified by:
removeDocumentChangeListener in interface StandardDocument
Parameters:
listener -

fireDocumentChangeEvent

protected void fireDocumentChangeEvent()

getGeneratedUid

public static java.lang.String getGeneratedUid(java.lang.Object object)
Generate a UID string for a particular object. This is guaranteed to be unique for each call to this method, even if the object is the same. It is generated by concatinating the following information, separated by the dash (-): 1) The canonical name of the object (e.g. org.homeunix.thecave.buddi.model3.Account). 2) A hexadecimal representation of the current system time in milliseconds 3) A hexadecimal representation of a 16 bit random number 4) A hexadecimal representation of the 16 least significant bits of this object's hash code (object.hashCode()).

Parameters:
object -
Returns: