| 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 | |
| 94 | ActionListener[] listeners = getListeners(ActionListener.class); |
| 95 | if (listeners.length > 0) { |
| 96 | for (ActionListener action : listeners) { |
| 97 | action.actionPerformed(e); |
| 98 | } |
| 99 | } else { |
| 100 | waitingQueue.add(e); |
| 101 | } |
| 102 | } |
| 103 | } |