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.7.2
Show newest version
/*
 * $Id: ErrorSupport.java 3840 2010-10-09 03:25:17Z kschaefe $
 *
 * 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 javax.swing.SwingUtilities;
import java.util.ArrayList;
import java.util.List;

/**
 * ErrorSupport provides support for managing error listeners.
 *
 * @author Joshua Marinacci [email protected]
 * @see ErrorListener
 * @see ErrorEvent
 */
public class ErrorSupport {

    private final List listeners;
    private final 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(new ErrorListener[0]);
    }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy