0d356cf9679739370d69dd04f6d97ce0bc3cf2ac
[fanfix.git] / src / be / nikiroo / fanfix_swing / gui / utils / ListenerPanel.java
1 package be.nikiroo.fanfix_swing.gui.utils;
2
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5 import java.util.LinkedList;
6 import java.util.Queue;
7
8 import javax.swing.JPanel;
9
10 import be.nikiroo.fanfix_swing.gui.SearchBar;
11
12 /**
13 * A {@link JPanel} with the default {@link ActionListener} add/remove/fire
14 * methods.
15 * <p>
16 * Note that it will queue all events until at least one listener comes (or
17 * comes back!); this first (or at least currently unique) listener will drain
18 * the queue.
19 *
20 * @author niki
21 */
22 public class ListenerPanel extends JPanel {
23 private static final long serialVersionUID = 1L;
24
25 /** Waiting queue until at least one listener is here to get the events. */
26 private final Queue<ActionEvent> waitingQueue;
27
28 /**
29 * Create a new {@link ListenerPanel}.
30 */
31 public ListenerPanel() {
32 waitingQueue = new LinkedList<ActionEvent>();
33 }
34
35 /**
36 * Check that this {@link ListenerPanel} currently has
37 * {@link ActionListener}s that listen on it.
38 *
39 * @return TRUE if it has
40 */
41 public synchronized boolean hasListeners() {
42 return listenerList.getListenerList().length > 1;
43 }
44
45 /**
46 * Check how many events are currently waiting for an
47 * {@link ActionListener}.
48 *
49 * @return the number of waiting events (can be 0)
50 */
51 public synchronized int getWaitingEventCount() {
52 return waitingQueue.size();
53 }
54
55 /**
56 * Adds the specified action listener to receive action events from this
57 * {@link SearchBar}.
58 *
59 * @param listener
60 * the action listener to be added
61 */
62 public synchronized void addActionListener(ActionListener listener) {
63 if (!hasListeners()) {
64 while (!waitingQueue.isEmpty()) {
65 listener.actionPerformed(waitingQueue.remove());
66 }
67 }
68
69 listenerList.add(ActionListener.class, listener);
70 }
71
72 /**
73 * Removes the specified action listener so that it no longer receives
74 * action events from this {@link SearchBar}.
75 *
76 * @param listener
77 * the action listener to be removed
78 */
79 public synchronized void removeActionListener(ActionListener listener) {
80 listenerList.remove(ActionListener.class, listener);
81 }
82
83 /**
84 * Notify the listeners of an action.
85 *
86 * @param listenerCommand
87 * A string that may specify a command (possibly one of several)
88 * associated with the event
89 */
90 protected synchronized void fireActionPerformed(String listenerCommand) {
91 ActionEvent e = new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
92 listenerCommand);
93 if (hasListeners()) {
94 Object[] listeners = listenerList.getListenerList();
95 for (int i = listeners.length - 2; i >= 0; i -= 2) {
96 if (listeners[i] == ActionListener.class) {
97 ((ActionListener) listeners[i + 1]).actionPerformed(e);
98 }
99 }
100 } else {
101 waitingQueue.add(e);
102 }
103 }
104 }