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

package org.biomoby.service.dashboard;

import org.biomoby.shared.MobyNamespace;
import org.biomoby.shared.event.NotificationEvent;
import org.biomoby.shared.event.Notifier;

/**
 * This is a simple graphical widget combining together a tree (a
 * subclass of {@link org.biomoby.service.dashboard.CommonTree}) and a
 * progress bar showing how the tree is being loaded. The progress bar
 * has a cancel button that allows to terminate loading
 * prematurely. The tree represents a part of a Biomoby registry -
 * concretely this class shows Biomoby namespaces. <p>
 *
 * @author <A HREF="mailto:martin.senger@gmail.com">Martin Senger</A>
 * @version $Id: NamespacesBoard.java,v 1.9 2006/04/28 00:13:41 senger Exp $
 */

public class NamespacesBoard
    extends CommonBoard {

    private static org.apache.commons.logging.Log log =
       org.apache.commons.logging.LogFactory.getLog (NamespacesBoard.class);

    /*********************************************************************
     * Constructor. <p>
     *
     * @param model gives access to a Biomoby registry
     * @param console displayed items selected in the tree
     * @param channel is used to fire property-change events
     ********************************************************************/
    public NamespacesBoard (RegistryModel model,
			    CommonConsole console,
			    PropertyChannel channel) {
	this (model, console, channel,
	      new NamespacesTree (model, console));
    }

    /*********************************************************************
     * Constructor. <p>
     *
     * @param model gives access to a Biomoby registry
     * @param console displayed items selected in the tree
     * @param channel is used to fire property-change events
     * @param customTree allows to plug user-defined tree component
     ********************************************************************/
    public NamespacesBoard (RegistryModel model,
			    CommonConsole console,
			    PropertyChannel channel,
			    CommonTree customTree) {
	super (model, channel);
	tree = customTree;
	tree.setPropertyChannel (channel);
	createItself();
	this.model.addNotificationListener (this);
    }

    /*********************************************************************
     *
     * Implementation of a NotificationListener intereface
     *
     ********************************************************************/
    public void notified (NotificationEvent event) {
	Object initiator;
	switch (event.getType()) {
	case Notifier.NAMESPACES_START:
		if (log.isDebugEnabled())
			log.debug (boardId + "-" + event.toString());
	    createProgressBar (Notifier.SIGNAL_CANCEL_NAMESPACES);
	    insertProgressBar();
	    break;
	case Notifier.NAMESPACES_COUNT:
		if (log.isDebugEnabled())
			log.debug (boardId + "-" + event.toString());
	    if (event.getMessage() instanceof Integer)
		calibrateProgressBar ( ((Integer)event.getMessage()).intValue() );
	    break;
 	case Notifier.NAMESPACE_LOADING:
	    channel.fire (this, DP_STATUS_MSG, event.getMessage());
	    break;
 	case Notifier.NAMESPACE_LOADED:
	    addToProgressBar();
	    break;
 	case Notifier.NAMESPACES_END:
 	case Notifier.NAMESPACES_CANCELLED:
 	case Notifier.NAMESPACES_RESET:
		if (log.isDebugEnabled())
			log.debug (boardId + "-" + event.toString());
	    initiator = event.getSource();
	    if (tree == initiator)
		channel.fire (this, DP_STATUS_MSG, "Done");
	    removeProgressBar();
	    break;
 	case Notifier.NAMESPACES_UPDATED:
	    removeProgressBar();   // if it is still there....
	    if (log.isDebugEnabled())
		log.debug (boardId + "-" + event.toString());
	    initiator = event.getSource();
	    if (tree != initiator) {
		// someone else (of this instance) initiated request
		// for updating namespaces - so we need to update our
		// own tree - using for that namespaces included in
		// this event
		tree.update (CommonTree.SORTED_AS_PREVIOUSLY,
			     (MobyNamespace[])event.getDetails());
	    }
	    break;
	}

    }
}
