1 package be
.nikiroo
.fanfix_swing
.gui
.utils
;
3 import java
.awt
.event
.ActionEvent
;
4 import java
.awt
.event
.ActionListener
;
5 import java
.util
.LinkedList
;
6 import java
.util
.Queue
;
8 import javax
.swing
.JPanel
;
10 import be
.nikiroo
.fanfix_swing
.gui
.SearchBar
;
13 * A {@link JPanel} with the default {@link ActionListener} add/remove/fire
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
22 public class ListenerPanel
extends JPanel
{
23 private static final long serialVersionUID
= 1L;
25 /** Waiting queue until at least one listener is here to get the events. */
26 private final Queue
<ActionEvent
> waitingQueue
;
29 * Create a new {@link ListenerPanel}.
31 public ListenerPanel() {
32 waitingQueue
= new LinkedList
<ActionEvent
>();
36 * Check that this {@link ListenerPanel} currently has
37 * {@link ActionListener}s that listen on it.
39 * @return TRUE if it has
41 public synchronized boolean hasListeners() {
42 return listenerList
.getListenerList().length
> 1;
46 * Check how many events are currently waiting for an
47 * {@link ActionListener}.
49 * @return the number of waiting events (can be 0)
51 public synchronized int getWaitingEventCount() {
52 return waitingQueue
.size();
56 * Adds the specified action listener to receive action events from this
60 * the action listener to be added
62 public synchronized void addActionListener(ActionListener listener
) {
63 if (!hasListeners()) {
64 while (!waitingQueue
.isEmpty()) {
65 listener
.actionPerformed(waitingQueue
.remove());
69 listenerList
.add(ActionListener
.class, listener
);
73 * Removes the specified action listener so that it no longer receives
74 * action events from this {@link SearchBar}.
77 * the action listener to be removed
79 public synchronized void removeActionListener(ActionListener listener
) {
80 listenerList
.remove(ActionListener
.class, listener
);
84 * Notify the listeners of an action.
86 * @param listenerCommand
87 * A string that may specify a command (possibly one of several)
88 * associated with the event
90 protected synchronized void fireActionPerformed(String listenerCommand
) {
91 ActionEvent e
= new ActionEvent(this, ActionEvent
.ACTION_PERFORMED
,
94 ActionListener
[] listeners
= getListeners(ActionListener
.class);
95 if (listeners
.length
> 0) {
96 for (ActionListener action
: listeners
) {
97 action
.actionPerformed(e
);