package org.apache.maven.j2ee;

/* ====================================================================
 *   Copyright 2001-2004 The Apache Software Foundation.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * ====================================================================
 */

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @version $Id$
 * @author  dion
 */
public class ValidationBroadcaster
{
    
    /** {@link ValidationListeners} handled by this broadcaster */
    private List listeners = new ArrayList();

    /** Creates a new instance of ValidationBroadcaster */
    public ValidationBroadcaster() 
    {
    }
    
    /**
     * fire a {@link ValidationListener#validationStarted(ValidationEvent) 
     * started} event.
     * @param event the event to broadcast
     */
    public void fireStartedEvent(ValidationEvent event)
    {
        for (Iterator listeners = getListeners().iterator(); 
            listeners.hasNext();)
        {
            ((ValidationListener) listeners.next()).validationStarted(event);
        }
    }

    /**
     * fire an {@link ValidationListener#validationEnded(ValidationEvent) 
     * ended} event.
     * @param event the event to broadcast
     */
    public void fireEndedEvent(ValidationEvent event)
    {
        for (Iterator listeners = getListeners().iterator(); 
            listeners.hasNext();)
        {
            ((ValidationListener) listeners.next()).validationEnded(event);
        }
    }

    /**
     * fire an {@link ValidationListener#validationError(ValidationEvent) 
     * error} event.
     * @param event the event to broadcast
     */
    public void fireErrorEvent(ValidationEvent event)
    {
        for (Iterator listeners = getListeners().iterator(); 
            listeners.hasNext();)
        {
            ((ValidationListener) listeners.next()).validationError(event);
        }
    }

    /**
     * fire a {@link ValidationListener#validationWarning(ValidationEvent) 
     * warning} event.
     * @param event the event to broadcast
     */
    public void fireWarningEvent(ValidationEvent event)
    {
        for (Iterator listeners = getListeners().iterator(); 
            listeners.hasNext();)
        {
            ((ValidationListener) listeners.next()).validationWarning(event);
        }
    }

    /**
     * fire a {@link ValidationListener#validationInformation(ValidationEvent) 
     * information} event.
     * @param event the event to broadcast
     */
    public void fireInformationEvent(ValidationEvent event)
    {
        for (Iterator listeners = getListeners().iterator(); 
            listeners.hasNext();)
        {
            ((ValidationListener) listeners.next()).validationInformation(
                event);
        }
    }
    
    /** Getter for property listeners.
     * @return Value of property listeners.
     */
    private List getListeners()
    {
        return listeners;
    }
    
    /**
     * add a listener to the list to be notified
     * @param listener a {@link ValidationListener}
     */
    public void addValidationListener(ValidationListener listener)
    {
        getListeners().add(listener);
    }
    
    /**
     * remove a listener from the list to be notified
     * @param listener a {@link ValidationListener}
     */
    public void removeValidationListener(ValidationListener listener)
    {
        getListeners().remove(listener);
    }

}
