// ServicesBoard.java
//
// Created: November 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.MobyService;
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 services. <p>
 *
 * @author <A HREF="mailto:martin.senger@gmail.com">Martin Senger</A>
 * @version $Id: ServicesBoard.java,v 1.10 2006/04/28 00:13:41 senger Exp $
 */

public class ServicesBoard
    extends CommonBoard {

    private static org.apache.commons.logging.Log log =
       org.apache.commons.logging.LogFactory.getLog (ServicesBoard.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 ServicesBoard (RegistryModel model,
			  CommonConsole console,
			  PropertyChannel channel) {
	this (model, console, channel,
	      new ServicesTree (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 ServicesBoard (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.AUTHORITIES_START:
		if (log.isDebugEnabled())
			log.debug (boardId + "-" + event.toString());
	    createProgressBar (Notifier.SIGNAL_CANCEL_SERVICES);
	    insertProgressBar();
	    break;
	case Notifier.AUTHORITIES_COUNT:
		if (log.isDebugEnabled())
			log.debug (boardId + "-" + event.toString());
	    if (event.getMessage() instanceof Integer)
		calibrateProgressBar ( ((Integer)event.getMessage()).intValue() );
	    break;
 	case Notifier.AUTHORITY_LOADING:
	    channel.fire (this, DP_STATUS_MSG, event.getMessage());
	    break;
 	case Notifier.AUTHORITY_LOADED:
	    addToProgressBar();
	    break;
 	case Notifier.AUTHORITIES_END:
 	case Notifier.AUTHORITIES_CANCELLED:
 	case Notifier.AUTHORITIES_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.AUTHORITIES_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 services - so we need to update our
		// own tree - using for that services included in this
		// event
		tree.update (CommonTree.SORTED_AS_PREVIOUSLY,
			     (MobyService[])event.getDetails());
	    }
	    break;
	}

    }
}
