All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jdesktop.swingx.error.ErrorSupport Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
/*
 * $Id$
 *
 * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package org.jdesktop.swingx.error;

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

import javax.swing.SwingUtilities;

/**
 * ErrorSupport provides support for managing error listeners.
 * @author Joshua Marinacci [email protected]
 * @see ErrorListener
 * @see ErrorEvent
 */
public class ErrorSupport {
    private List listeners;
    private Object source;
    
    /**
     * Creates a new instance of ErrorSupport
     * @param source The object which will fire the ErrorEvents
     */
    public ErrorSupport(Object source) {
        this.source = source;
        listeners = new ArrayList();
    }
    
    /**
     * Add an ErrorListener
     * @param listener the listener to add
     */
    public void addErrorListener(ErrorListener listener) {
        listeners.add(listener);
    }
    
    /**
     * Remove an error listener
     * @param listener the listener to remove
     */
    public void removeErrorListener(ErrorListener listener) {
        listeners.remove(listener);
    }
        
    /**
     * Returns an array of all the listeners which were added to the 
     * ErrorSupport object with addErrorListener().
     * @return all of the ErrorListeners added or an empty array if no listeners have been 
     * added.
     */
    public ErrorListener[] getErrorListeners() {
        return listeners.toArray(null);
    }

    /**
     * Report that an error has occurred
     * @param throwable The {@link Error} or {@link Exception} which occured.
     */
    public void fireErrorEvent(final Throwable throwable) {
        final ErrorEvent evt = new ErrorEvent(throwable, source);
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                for(ErrorListener el : listeners) {
                    el.errorOccured(evt);
                }
            }
        });
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy