This project is read-only.

DotNetLib.DataEventArgs and DotNetLib.DataCancelEventArgs

Provides and easy way to attach a piece of data to normal and cancellable events.
enum DocumentType { Legal, Personal }

class Document
{
  private event DataCancelEventHandler<DocumentType> savingEvent;
  private event DataEventHandler<DocumentType> savedEvent;
  private DocumentType type;

  public Document(DocumentType type)
  {
    this.type = type;
  }

  public event DataCancelEventHandler<DocumentType> Saving
  {
    add { this.savingEvent += value; }
    remove { this.savingEvent -= value; }
  }

  public event DataEventHandler<DocumentType> Saved
  {
    add { this.savedEvent += value; }
    remove { this.savedEvent -= value; }
  }

  protected virtual void OnSaving(DataCancelEventArgs<DocumentType> e)
  {
    if(this.savingEvent != null)
      this.savingEvent(this, e);
  }

  protected virtual void OnSaved(DataEventArgs<DocumentType> e)
  {
    if(this.savedEvent != null)
      this.savedEvent(this, e);
  }

  public void Save()
  {
    DataCancelEventArgs<DocumentType> e = new DataCancelEventArgs<DocumentType>(type);
    this.OnSaving(e);
    if(!e.Cancel)
      this.OnSaved(new DataEventArgs<DocumentType>(e.Data));
  }
}

class FileChecker
{
  private List<Document> documents = new List<Document>();

  public void WatchDocument(Document document)
  {
    documents.Add(document);
    document.Saving += new DataCancelEventHandler<DocumentType>(document_Saving);
    document.Saved += new DataEventHandler<DocumentType>(document_Saved);
  }

  private void document_Saving(object sender, DataCancelEventArgs<DocumentType> e)
  {
    // cannot save personal documents
    if (e.Data == DocumentType.Personal)
      e.Cancel = true;
  }

  private void document_Saved(object sender, DataEventArgs<DocumentType> e)
  {
    // document was saved, must be legal
  }
}
Home

Last edited Nov 26, 2008 at 4:20 AM by joshucar, version 1

Comments

No comments yet.