PageRenderTime 42ms CodeModel.GetById 20ms app.highlight 5ms RepoModel.GetById 15ms app.codeStats 0ms

/bundles/plugins-trunk/CommonControls/common/swingworker/SwingPropertyChangeSupport.java

#
Java | 108 lines | 34 code | 11 blank | 63 comment | 5 complexity | a85a24f718501c143664e17b673f7feb MD5 | raw file
  1/*
  2 * $Id: SwingPropertyChangeSupport.java,v 1.1 2005/06/18 21:27:14 idk Exp $
  3 *
  4 * Copyright 2005 Sun Microsystems, Inc. All rights
  5 * reserved. Use is subject to license terms.
  6 */
  7
  8//package org.jdesktop.swingworker;
  9package common.swingworker;
 10
 11import java.beans.PropertyChangeSupport;
 12import java.beans.PropertyChangeEvent;
 13
 14import javax.swing.SwingUtilities;
 15
 16/**
 17 * This subclass of {@code java.beans.PropertyChangeSupport} is almost
 18 * identical in functionality. The only difference is if constructed with
 19 * {@code SwingPropertyChangeSupport(sourceBean, true)} it ensures
 20 * listeners are only ever notified on the <i>Event Dispatch Thread</i>.
 21 *
 22 * @author Igor Kushnirskiy
 23 * @version $Revision: 1.1 $ $Date: 2005/06/18 21:27:14 $
 24 */
 25
 26public final class SwingPropertyChangeSupport extends PropertyChangeSupport {
 27
 28    /**
 29     * Constructs a SwingPropertyChangeSupport object.
 30     *
 31     * @param sourceBean  The bean to be given as the source for any
 32     *        events.
 33     * @throws NullPointerException if {@code sourceBean} is
 34     *         {@code null}
 35     */
 36    public SwingPropertyChangeSupport(Object sourceBean) {
 37        this(sourceBean, false);
 38    }
 39
 40    /**
 41     * Constructs a SwingPropertyChangeSupport object.
 42     *
 43     * @param sourceBean the bean to be given as the source for any events
 44     * @param notifyOnEDT whether to notify listeners on the <i>Event
 45     *        Dispatch Thread</i> only
 46     *
 47     * @throws NullPointerException if {@code sourceBean} is
 48     *         {@code null}
 49     * @since 1.6
 50     */
 51    public SwingPropertyChangeSupport(Object sourceBean, boolean notifyOnEDT) {
 52        super(sourceBean);
 53        this.notifyOnEDT = notifyOnEDT;
 54    }
 55
 56    /**
 57     * {@inheritDoc}
 58     *
 59     * <p>
 60     * If {@see #isNotifyOnEDT} is {@code true} and called off the
 61     * <i>Event Dispatch Thread</i> this implementation uses
 62     * {@code SwingUtilities.invokeLater} to send out the notification
 63     * on the <i>Event Dispatch Thread</i>. This ensures  listeners
 64     * are only ever notified on the <i>Event Dispatch Thread</i>.
 65     *
 66     * @throws NullPointerException if {@code evt} is
 67     *         {@code null}
 68     * @since 1.6
 69     */
 70    public void firePropertyChange(final PropertyChangeEvent evt) {
 71        if (evt == null) {
 72            throw new NullPointerException();
 73        }
 74        if (! isNotifyOnEDT()
 75            || SwingUtilities.isEventDispatchThread()) {
 76            super.firePropertyChange(evt);
 77        } else {
 78            SwingUtilities.invokeLater(
 79                new Runnable() {
 80                    public void run() {
 81                        firePropertyChange(evt);
 82                    }
 83                });
 84        }
 85    }
 86
 87    /**
 88     * Returns {@code notifyOnEDT} property.
 89     *
 90     * @return {@code notifyOnEDT} property
 91     * @see #SwingPropertyChangeSupport(Object sourceBean, boolean notifyOnEDT)
 92     * @since 1.6
 93     */
 94    public boolean isNotifyOnEDT() {
 95        return notifyOnEDT;
 96    }
 97
 98    // Serialization version ID
 99    static final long serialVersionUID = 7162625831330845068L;
100
101    /**
102     * whether to notify listeners on EDT
103     *
104     * @serial
105     * @since 1.6
106     */
107    private final boolean notifyOnEDT;
108}