001 /*
002 * $Id: ErrorSupport.java,v 1.1 2006/04/28 18:28:19 joshy Exp $
003 *
004 * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle,
005 * Santa Clara, California 95054, U.S.A. All rights reserved.
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this library; if not, write to the Free Software
019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
020 */
021
022 package org.jdesktop.swingx.error;
023
024 import java.util.ArrayList;
025 import java.util.List;
026 import javax.swing.SwingUtilities;
027 import org.jdesktop.swingx.*;
028
029 /**
030 * ErrorSupport provides support for managing error listeners.
031 * @author Joshua Marinacci joshua.marinacci@sun.com
032 * @see ErrorListener
033 * @see ErrorEvent
034 */
035 public class ErrorSupport {
036 private List<ErrorListener> listeners;
037 private Object source;
038
039 /**
040 * Creates a new instance of <CODE>ErrorSupport</CODE>
041 * @param source The object which will fire the <CODE>ErrorEvent</CODE>s
042 */
043 public ErrorSupport(Object source) {
044 this.source = source;
045 listeners = new ArrayList<ErrorListener>();
046 }
047
048 /**
049 * Add an ErrorListener
050 * @param listener the listener to add
051 */
052 public void addErrorListener(ErrorListener listener) {
053 listeners.add(listener);
054 }
055
056 /**
057 * Remove an error listener
058 * @param listener the listener to remove
059 */
060 public void removeErrorListener(ErrorListener listener) {
061 listeners.remove(listener);
062 }
063
064 /**
065 * Returns an array of all the listeners which were added to the
066 * <CODE>ErrorSupport</CODE> object with <CODE>addErrorListener()</CODE>.
067 * @return all of the <CODE>ErrorListener</CODE>s added or an empty array if no listeners have been
068 * added.
069 */
070 public ErrorListener[] getErrorListeners() {
071 return listeners.toArray(null);
072 }
073
074 /**
075 * Report that an error has occured
076 * @param throwable The <CODE>{@link Error}</CODE> or <CODE>{@link Exception}</CODE> which occured.
077 */
078 public void fireErrorEvent(final Throwable throwable) {
079 final ErrorEvent evt = new ErrorEvent(throwable, source);
080 SwingUtilities.invokeLater(new Runnable() {
081 public void run() {
082 for(ErrorListener el : listeners) {
083 el.errorOccured(evt);
084 }
085 }
086 });
087 }
088
089 }