// NotificationEvent.java
//
// Created: October 2005
//
// This file is a component of the BioMoby project.
// Copyright Martin Senger (martin.senger@gmail.com).
//

package org.biomoby.shared.event;

import java.util.EventObject;

/**
 * An event fired by various Moby components to inform that something
 * has happened. Typical usage is for monitoring access to a Biomoby
 * registry (how many entities to read, and which one was just
 * read). <p>
 *
 * Each notification event points to its source - this is inherited
 * from a usual Java EventObject. Sometimes, however, the semantics of
 * the source may change to indicate not the event producer but the
 * event initiator (an instance that caused that something had
 * happened that triggered later sending an event). <p>
 *
 * Additionally, it has its type (an integer value, preferably taken
 * from a list defined in interface {@link
 * org.biomoby.shared.event.Notifier}), a message (whose format may be
 * determined by the notification type), and optionally an object with
 * more details (again, its presence or absence may depend on the
 * notification type). <p>
 *
 * @author <A HREF="mailto:martin.senger@gmail.com">Martin Senger</A>
 * @version $Id: NotificationEvent.java,v 1.6 2005/11/06 16:47:11 senger Exp $
 */

public class NotificationEvent
    extends EventObject {

    private int type;
    private Object message;
    private Object details;

    /*********************************************************************
     * Constructor that does not do much.
     ********************************************************************/
    public NotificationEvent (Object source) {
	super (source);
    }

    /*********************************************************************
     * A usual constructor, setting notification type and a message.
     ********************************************************************/
    public NotificationEvent (Object source,
			      int type, Object message) {
	this (source);
	this.type = type;
	this.message = (message == null ? "" : message);
    }

    /*********************************************************************
     * A full constructor, setting notification type, message, and
     * details.
     ********************************************************************/
    public NotificationEvent (Object source,
			      int type, Object message, Object details) {
	this (source, type, message);
	this.details = details;
    }

    /*********************************************************************
     * Return a type of this notification event.
     ********************************************************************/
    public int getType() {
	return type;
    }

    /*********************************************************************
     * Return a message associated with this notification event.
     ********************************************************************/
    public Object getMessage() {
	return message;
    }

    /*********************************************************************
     * Return an object with more details about this notification
     * event.
     ********************************************************************/
    public Object getDetails() {
	return details;
    }

    /*********************************************************************
     *
     ********************************************************************/
    public String toString() {
	String typeStr = null;
	try {
	    typeStr = typeTexts [type];
	} catch (Exception e) {
	    typeStr = "" + type;
	}
	StringBuffer buf = new StringBuffer (100);
	buf.append ("[");
	buf.append (typeStr);
	if (message != null &&
	    ( message instanceof String || message instanceof Integer || message instanceof Boolean ))
	    buf.append (" " + message);
	if (details != null)
	    buf.append (", Details=" + details.getClass().getName());
	buf.append ("]");
	return new String (buf);
    }

    static final String[] typeTexts = new String[] {
	"data-type-start",      // [ 0]
	"data-type-count",      // [ 1]
	"data-type-loading",    // [ 2]
	"data-type-loaded",     // [ 3]
	"data-type-end",        // [ 4]
	"service-type-start",   // [ 5]
	"service-type-count",   // [ 6]
	"service-type-loading", // [ 7]
	"service-type-loaded",  // [ 8]
	"service-type-end",     // [ 9]
	"namespace-start",      // [10]
	"namespace-count",      // [11]
	"namespace-loading",    // [12]
	"namespace-loaded",     // [13]
	"namespace-end",        // [14]
	"authority-start",      // [15]
	"authority-count",      // [16]
	"authority-loading",    // [17]
	"authority-loaded",     // [18]
	"authority-end",        // [19]
	"data-type-reset",      // [20]
	"service-type-reset",   // [21]
	"namespace-reset",      // [22]
	"authority-reset",      // [23]
	"data-type-cancel",     // [24]
	"service-type-cancel",  // [25]
	"namespace-cancel",     // [26]
	"authority-cancel",     // [27]
        "data-type-updated",    // [28]
        "service-type-updated", // [29]
        "namespace-updated",    // [30]
        "authority-updated"     // [31]
    };

}
