Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / exceptionHandling / ExceptionHandlingSupport.java @ 40561

History | View | Annotate | Download (3.22 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.utils.exceptionHandling;
25

    
26
import java.util.ArrayList;
27
import java.util.Iterator;
28

    
29
/**
30
 * <p>Stores a group of <code>ExceptionListener</code> that, in whatever moment
31
 * could be notified a <code>Throwable</code> Java error or exception.</p>
32
 * 
33
 * <p><code>ExceptionHandlingSupport</code> is useful to manage a set of listeners
34
 * that need to be executed when a particular error or exception in a object is produced. For
35
 * instance, when user drags the mouse over a geometrical object in a view, it's possible that
36
 * some <code>ExceptionLister</code> of that object and other nearby, must be notified. Using
37
 * <code>ExceptionHandlingSupport</code> the developer can manage easily a set of that kind of
38
 * listeners, that can change according to an external factor. (In the previous sample, for intance,
39
 * can change according the closed objects to the one which this <code>ExceptionHandlingSupport</code>
40
 * refers).</p>
41
 */
42
public class ExceptionHandlingSupport {
43
        /**
44
         * <p>List with a group of <code>ExceptionListener</code>.</p>
45
         */
46
        private ArrayList exceptionListeners = new ArrayList();
47

    
48
    /**
49
     * <p>Adds a new <code>ExceptionListener</code> for adding support to it.</p>
50
     *
51
     * @param o listener adapted to be notified by the <code>ExceptionHandlingSupport</code>
52
     */
53
    public void addExceptionListener(ExceptionListener o) {
54
        exceptionListeners.add(o);
55
    }
56

    
57
    /**
58
     * <p>Removes an <code>ExceptionListener</code> for finishing the support to it.</p>
59
     *
60
     * @param o listener adapted to be notified by the <code>ExceptionHandlingSupport</code>
61
     *
62
     * @return <code>true</code> if the list contained the specified element, <code>false</code> otherwise
63
     */
64
    public boolean removeExceptionListener(ExceptionListener o) {
65
        return exceptionListeners.remove(o);
66
    }
67

    
68
    /**
69
     * <p>Notifies all registered listeners that an error or exception
70
     *  throwable by the Java Virtual Machine has been produced.</p>
71
     *
72
     * @param t an error or exception in the Java language
73
     */
74
    public void throwException(Throwable t) {
75
        for (Iterator iter = exceptionListeners.iterator(); iter.hasNext();) {
76
            ExceptionListener listener = (ExceptionListener) iter.next();
77
            listener.exceptionThrown(t);
78
        }
79
    }
80
}