Commit | Line | Data |
---|---|---|
2a03ecc0 NR |
1 | package be.nikiroo.fanfix_swing.gui.utils; |
2 | ||
3 | import java.awt.event.ActionEvent; | |
4 | import java.awt.event.ActionListener; | |
d6c8579c NR |
5 | import java.util.LinkedList; |
6 | import java.util.Queue; | |
2a03ecc0 NR |
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. | |
d6c8579c NR |
15 | * <p> |
16 | * Note that it will queue all events until at least one listener comes (or | |
9017ceb4 | 17 | * comes back!); this first (or at least currently unique) listener will drain |
d6c8579c | 18 | * the queue. |
2a03ecc0 NR |
19 | * |
20 | * @author niki | |
21 | */ | |
22 | public class ListenerPanel extends JPanel { | |
23 | private static final long serialVersionUID = 1L; | |
24 | ||
9017ceb4 | 25 | /** Waiting queue until at least one listener is here to get the events. */ |
d6c8579c NR |
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 | /** | |
9017ceb4 NR |
36 | * Check that this {@link ListenerPanel} currently has |
37 | * {@link ActionListener}s that listen on it. | |
d6c8579c NR |
38 | * |
39 | * @return TRUE if it has | |
40 | */ | |
41 | public synchronized boolean hasListeners() { | |
42 | return listenerList.getListenerList().length > 1; | |
43 | } | |
44 | ||
45 | /** | |
9017ceb4 NR |
46 | * Check how many events are currently waiting for an |
47 | * {@link ActionListener}. | |
d6c8579c NR |
48 | * |
49 | * @return the number of waiting events (can be 0) | |
50 | */ | |
51 | public synchronized int getWaitingEventCount() { | |
52 | return waitingQueue.size(); | |
53 | } | |
54 | ||
2a03ecc0 NR |
55 | /** |
56 | * Adds the specified action listener to receive action events from this | |
57 | * {@link SearchBar}. | |
58 | * | |
9017ceb4 NR |
59 | * @param listener |
60 | * the action listener to be added | |
2a03ecc0 NR |
61 | */ |
62 | public synchronized void addActionListener(ActionListener listener) { | |
d6c8579c NR |
63 | if (!hasListeners()) { |
64 | while (!waitingQueue.isEmpty()) { | |
65 | listener.actionPerformed(waitingQueue.remove()); | |
66 | } | |
67 | } | |
68 | ||
2a03ecc0 NR |
69 | listenerList.add(ActionListener.class, listener); |
70 | } | |
71 | ||
72 | /** | |
9017ceb4 NR |
73 | * Removes the specified action listener so that it no longer receives |
74 | * action events from this {@link SearchBar}. | |
2a03ecc0 | 75 | * |
9017ceb4 NR |
76 | * @param listener |
77 | * the action listener to be removed | |
2a03ecc0 NR |
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 | * | |
9017ceb4 NR |
86 | * @param listenerCommand |
87 | * A string that may specify a command (possibly one of several) | |
88 | * associated with the event | |
2a03ecc0 | 89 | */ |
d6c8579c | 90 | protected synchronized void fireActionPerformed(String listenerCommand) { |
9017ceb4 NR |
91 | ActionEvent e = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, |
92 | listenerCommand); | |
af9d85e2 NR |
93 | |
94 | ActionListener[] listeners = getListeners(ActionListener.class); | |
95 | if (listeners.length > 0) { | |
96 | for (ActionListener action : listeners) { | |
97 | action.actionPerformed(e); | |
2a03ecc0 | 98 | } |
d6c8579c NR |
99 | } else { |
100 | waitingQueue.add(e); | |
2a03ecc0 NR |
101 | } |
102 | } | |
103 | } |