| 1 | package be.nikiroo.fanfix_swing.gui.utils; |
| 2 | |
| 3 | import java.awt.event.ActionEvent; |
| 4 | import java.awt.event.ActionListener; |
| 5 | |
| 6 | import javax.swing.JPanel; |
| 7 | |
| 8 | import be.nikiroo.fanfix_swing.gui.SearchBar; |
| 9 | |
| 10 | /** |
| 11 | * A {@link JPanel} with the default {@link ActionListener} add/remove/fire |
| 12 | * methods. |
| 13 | * |
| 14 | * @author niki |
| 15 | */ |
| 16 | public class ListenerPanel extends JPanel { |
| 17 | private static final long serialVersionUID = 1L; |
| 18 | |
| 19 | /** |
| 20 | * Adds the specified action listener to receive action events from this |
| 21 | * {@link SearchBar}. |
| 22 | * |
| 23 | * @param listener the action listener to be added |
| 24 | */ |
| 25 | public synchronized void addActionListener(ActionListener listener) { |
| 26 | listenerList.add(ActionListener.class, listener); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Removes the specified action listener so that it no longer receives action |
| 31 | * events from this {@link SearchBar}. |
| 32 | * |
| 33 | * @param listener the action listener to be removed |
| 34 | */ |
| 35 | public synchronized void removeActionListener(ActionListener listener) { |
| 36 | listenerList.remove(ActionListener.class, listener); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Notify the listeners of an action. |
| 41 | * |
| 42 | * @param listenerCommand A string that may specify a command (possibly one of |
| 43 | * several) associated with the event |
| 44 | */ |
| 45 | protected void fireActionPerformed(String listenerCommand) { |
| 46 | ActionEvent e = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, listenerCommand); |
| 47 | Object[] listeners = listenerList.getListenerList(); |
| 48 | for (int i = listeners.length - 2; i >= 0; i -= 2) { |
| 49 | if (listeners[i] == ActionListener.class) { |
| 50 | ((ActionListener) listeners[i + 1]).actionPerformed(e); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | } |