J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1998-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package javax.swing.event; |
| 27 | |
| 28 | import java.beans.PropertyChangeSupport; |
| 29 | import java.beans.PropertyChangeEvent; |
| 30 | |
| 31 | import javax.swing.SwingUtilities; |
| 32 | |
| 33 | /** |
| 34 | * This subclass of {@code java.beans.PropertyChangeSupport} is almost |
| 35 | * identical in functionality. The only difference is if constructed with |
| 36 | * {@code SwingPropertyChangeSupport(sourceBean, true)} it ensures |
| 37 | * listeners are only ever notified on the <i>Event Dispatch Thread</i>. |
| 38 | * |
| 39 | * @author Igor Kushnirskiy |
| 40 | */ |
| 41 | |
| 42 | public final class SwingPropertyChangeSupport extends PropertyChangeSupport { |
| 43 | |
| 44 | /** |
| 45 | * Constructs a SwingPropertyChangeSupport object. |
| 46 | * |
| 47 | * @param sourceBean The bean to be given as the source for any |
| 48 | * events. |
| 49 | * @throws NullPointerException if {@code sourceBean} is |
| 50 | * {@code null} |
| 51 | */ |
| 52 | public SwingPropertyChangeSupport(Object sourceBean) { |
| 53 | this(sourceBean, false); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Constructs a SwingPropertyChangeSupport object. |
| 58 | * |
| 59 | * @param sourceBean the bean to be given as the source for any events |
| 60 | * @param notifyOnEDT whether to notify listeners on the <i>Event |
| 61 | * Dispatch Thread</i> only |
| 62 | * |
| 63 | * @throws NullPointerException if {@code sourceBean} is |
| 64 | * {@code null} |
| 65 | * @since 1.6 |
| 66 | */ |
| 67 | public SwingPropertyChangeSupport(Object sourceBean, boolean notifyOnEDT) { |
| 68 | super(sourceBean); |
| 69 | this.notifyOnEDT = notifyOnEDT; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * {@inheritDoc} |
| 74 | * |
| 75 | * <p> |
| 76 | * If {@link #isNotifyOnEDT} is {@code true} and called off the |
| 77 | * <i>Event Dispatch Thread</i> this implementation uses |
| 78 | * {@code SwingUtilities.invokeLater} to send out the notification |
| 79 | * on the <i>Event Dispatch Thread</i>. This ensures listeners |
| 80 | * are only ever notified on the <i>Event Dispatch Thread</i>. |
| 81 | * |
| 82 | * @throws NullPointerException if {@code evt} is |
| 83 | * {@code null} |
| 84 | * @since 1.6 |
| 85 | */ |
| 86 | public void firePropertyChange(final PropertyChangeEvent evt) { |
| 87 | if (evt == null) { |
| 88 | throw new NullPointerException(); |
| 89 | } |
| 90 | if (! isNotifyOnEDT() |
| 91 | || SwingUtilities.isEventDispatchThread()) { |
| 92 | super.firePropertyChange(evt); |
| 93 | } else { |
| 94 | SwingUtilities.invokeLater( |
| 95 | new Runnable() { |
| 96 | public void run() { |
| 97 | firePropertyChange(evt); |
| 98 | } |
| 99 | }); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Returns {@code notifyOnEDT} property. |
| 105 | * |
| 106 | * @return {@code notifyOnEDT} property |
| 107 | * @see #SwingPropertyChangeSupport(Object sourceBean, boolean notifyOnEDT) |
| 108 | * @since 1.6 |
| 109 | */ |
| 110 | public final boolean isNotifyOnEDT() { |
| 111 | return notifyOnEDT; |
| 112 | } |
| 113 | |
| 114 | // Serialization version ID |
| 115 | static final long serialVersionUID = 7162625831330845068L; |
| 116 | |
| 117 | /** |
| 118 | * whether to notify listeners on EDT |
| 119 | * |
| 120 | * @serial |
| 121 | * @since 1.6 |
| 122 | */ |
| 123 | private final boolean notifyOnEDT; |
| 124 | } |