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/>. |
16 | package net.sf.jomic.tools; |
17 | |
18 | import java.awt.event.ActionEvent; |
19 | import java.awt.event.ActionListener; |
20 | import java.util.HashMap; |
21 | import java.util.Iterator; |
22 | import java.util.Map; |
23 | |
24 | import org.apache.commons.logging.Log; |
25 | |
26 | /** |
27 | * Delegate for ActionEvents. |
28 | * |
29 | * @author Thomas Aglassinger |
30 | */ |
31 | public 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 | } |