98202b2da42108c44beee3119fb32a2b4bf1cec1
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 curently unique) listener will drain
22 public class ListenerPanel
extends JPanel
{
23 private static final long serialVersionUID
= 1L;
25 private final Queue
<ActionEvent
> waitingQueue
;
28 * Create a new {@link ListenerPanel}.
30 public ListenerPanel() {
31 waitingQueue
= new LinkedList
<ActionEvent
>();
35 * Check that this {@link ListenerPanel} currently has {@link ActionListener}s
38 * @return TRUE if it has
40 public synchronized boolean hasListeners() {
41 return listenerList
.getListenerList().length
> 1;
45 * Check how many events are currently waiting for an {@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
57 * @param listener the action listener to be added
59 public synchronized void addActionListener(ActionListener listener
) {
60 if (!hasListeners()) {
61 while (!waitingQueue
.isEmpty()) {
62 listener
.actionPerformed(waitingQueue
.remove());
66 listenerList
.add(ActionListener
.class, listener
);
70 * Removes the specified action listener so that it no longer receives action
71 * events from this {@link SearchBar}.
73 * @param listener the action listener to be removed
75 public synchronized void removeActionListener(ActionListener listener
) {
76 listenerList
.remove(ActionListener
.class, listener
);
80 * Notify the listeners of an action.
82 * @param listenerCommand A string that may specify a command (possibly one of
83 * several) associated with the event
85 protected synchronized void fireActionPerformed(String listenerCommand
) {
86 ActionEvent e
= new ActionEvent(this, ActionEvent
.ACTION_PERFORMED
, listenerCommand
);
88 Object
[] listeners
= listenerList
.getListenerList();
89 for (int i
= listeners
.length
- 2; i
>= 0; i
-= 2) {
90 if (listeners
[i
] == ActionListener
.class) {
91 ((ActionListener
) listeners
[i
+ 1]).actionPerformed(e
);