EMMA Coverage Report (generated Sat Oct 08 11:41:37 CEST 2011)
[all classes][net.sf.jomic.tools]

COVERAGE SUMMARY FOR SOURCE FILE [ActionDelegate.java]

nameclass, %method, %block, %line, %
ActionDelegate.java100% (1/1)100% (5/5)47%  (60/129)61%  (19/31)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ActionDelegate100% (1/1)100% (5/5)47%  (60/129)61%  (19/31)
removeActionListener (ActionListener): void 100% (1/1)20%  (11/55)30%  (3/10)
finalize (): void 100% (1/1)31%  (11/36)44%  (4/9)
ActionDelegate (Log): void 100% (1/1)100% (11/11)100% (4/4)
actionPerformed (ActionEvent): void 100% (1/1)100% (17/17)100% (6/6)
addActionListener (ActionListener): void 100% (1/1)100% (10/10)100% (2/2)

1// Jomic - a viewer for comic book archives.
2// Copyright (C) 2004-2011 Thomas Aglassinger
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program.  If not, see <http://www.gnu.org/licenses/>.
16package net.sf.jomic.tools;
17 
18import java.awt.event.ActionEvent;
19import java.awt.event.ActionListener;
20import java.util.HashMap;
21import java.util.Iterator;
22import java.util.Map;
23 
24import org.apache.commons.logging.Log;
25 
26/**
27 *  Delegate for ActionEvents.
28 *
29 * @author    Thomas Aglassinger
30 */
31public class ActionDelegate implements ActionListener
32{
33    private Map listeners;
34    private Log logger;
35 
36    /**
37     *  Creates a new delegate with logging messages going to logger.
38     *
39     * @param  newLogger  logger where the delegate logs its messages.
40     */
41    public ActionDelegate(Log newLogger) {
42        logger = newLogger;
43        listeners = new HashMap();
44    }
45 
46    public void actionPerformed(ActionEvent event) {
47        Iterator rider = listeners.keySet().iterator();
48 
49        while (rider.hasNext()) {
50            ActionListener listener = (ActionListener) rider.next();
51 
52            listener.actionPerformed(event);
53        }
54    }
55 
56    public void addActionListener(ActionListener listener) {
57        listeners.put(listener, new Throwable("stack where from where ActionListener was added"));
58    }
59 
60    public void removeActionListener(ActionListener listener) {
61        // TODO: Define exceptional_behavior of removeActionListener().
62        if (!listeners.containsKey(listener)) {
63            logger.error("list of current listeners:");
64 
65            Iterator rider = listeners.entrySet().iterator();
66 
67            while (rider.hasNext()) {
68                Map.Entry entry = (Map.Entry) rider.next();
69 
70                logger.error("- " + entry.getKey(), (Throwable) entry.getValue());
71            }
72            throw new IllegalArgumentException("listener to remove must have been added before: " + listener);
73        }
74        listeners.remove(listener);
75    }
76 
77    /**
78     *  Checks that all listeners have been removed, and warns if not.
79     */
80    protected void finalize()
81        throws Throwable {
82        Iterator rider = listeners.entrySet().iterator();
83 
84        while (rider.hasNext()) {
85            Map.Entry entry = (Map.Entry) rider.next();
86            ActionListener listener = (ActionListener) entry.getKey();
87            Throwable stack = (Throwable) entry.getValue();
88 
89            logger.warn("ActionListener should be removed: " + listener, stack);
90        }
91        super.finalize();
92    }
93}

[all classes][net.sf.jomic.tools]
EMMA 2.0.4217 (C) Vladimir Roubtsov