1 package be
.nikiroo
.utils
.ui
;
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
;
11 * A {@link JPanel} with the default {@link ActionListener} add/remove/fire
14 * Note that it will queue all events until at least one listener comes (or
15 * comes back!); this first (or at least currently unique) listener will drain
20 public class ListenerPanel
extends JPanel
{
21 private static final long serialVersionUID
= 1L;
23 /** Waiting queue until at least one listener is here to get the events. */
24 private final Queue
<ActionEvent
> waitingQueue
;
27 * Create a new {@link ListenerPanel}.
29 public ListenerPanel() {
30 waitingQueue
= new LinkedList
<ActionEvent
>();
34 * Check that this {@link ListenerPanel} currently has
35 * {@link ActionListener}s that listen on it.
37 * @return TRUE if it has
39 public synchronized boolean hasListeners() {
40 return listenerList
.getListenerList().length
> 1;
44 * Check how many events are currently waiting for an
45 * {@link ActionListener}.
47 * @return the number of waiting events (can be 0)
49 public synchronized int getWaitingEventCount() {
50 return waitingQueue
.size();
54 * Adds the specified action listener to receive action events from this
55 * {@link ListenerPanel}.
58 * the action listener to be added
60 public synchronized void addActionListener(ActionListener listener
) {
61 if (!hasListeners()) {
62 while (!waitingQueue
.isEmpty()) {
63 listener
.actionPerformed(waitingQueue
.remove());
67 listenerList
.add(ActionListener
.class, listener
);
71 * Removes the specified action listener so that it no longer receives
72 * action events from this {@link ListenerPanel}.
75 * the action listener to be removed
77 public synchronized void removeActionListener(ActionListener listener
) {
78 listenerList
.remove(ActionListener
.class, listener
);
82 * Notify the listeners of an action.
84 * @param listenerCommand
85 * A string that may specify a command (possibly one of several)
86 * associated with the event
88 protected synchronized void fireActionPerformed(String listenerCommand
) {
89 ActionEvent e
= new ActionEvent(this, ActionEvent
.ACTION_PERFORMED
,
92 ActionListener
[] listeners
= getListeners(ActionListener
.class);
93 if (listeners
.length
> 0) {
94 for (ActionListener action
: listeners
) {
95 action
.actionPerformed(e
);