PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms 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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. //package org.jdesktop.swingworker;
  8. package common.swingworker;
  9. import java.beans.PropertyChangeSupport;
  10. import java.beans.PropertyChangeEvent;
  11. import javax.swing.SwingUtilities;
  12. /**
  13. * This subclass of {@code java.beans.PropertyChangeSupport} is almost
  14. * identical in functionality. The only difference is if constructed with
  15. * {@code SwingPropertyChangeSupport(sourceBean, true)} it ensures
  16. * listeners are only ever notified on the <i>Event Dispatch Thread</i>.
  17. *
  18. * @author Igor Kushnirskiy
  19. * @version $Revision: 1.1 $ $Date: 2005/06/18 21:27:14 $
  20. */
  21. public final class SwingPropertyChangeSupport extends PropertyChangeSupport {
  22. /**
  23. * Constructs a SwingPropertyChangeSupport object.
  24. *
  25. * @param sourceBean The bean to be given as the source for any
  26. * events.
  27. * @throws NullPointerException if {@code sourceBean} is
  28. * {@code null}
  29. */
  30. public SwingPropertyChangeSupport(Object sourceBean) {
  31. this(sourceBean, false);
  32. }
  33. /**
  34. * Constructs a SwingPropertyChangeSupport object.
  35. *
  36. * @param sourceBean the bean to be given as the source for any events
  37. * @param notifyOnEDT whether to notify listeners on the <i>Event
  38. * Dispatch Thread</i> only
  39. *
  40. * @throws NullPointerException if {@code sourceBean} is
  41. * {@code null}
  42. * @since 1.6
  43. */
  44. public SwingPropertyChangeSupport(Object sourceBean, boolean notifyOnEDT) {
  45. super(sourceBean);
  46. this.notifyOnEDT = notifyOnEDT;
  47. }
  48. /**
  49. * {@inheritDoc}
  50. *
  51. * <p>
  52. * If {@see #isNotifyOnEDT} is {@code true} and called off the
  53. * <i>Event Dispatch Thread</i> this implementation uses
  54. * {@code SwingUtilities.invokeLater} to send out the notification
  55. * on the <i>Event Dispatch Thread</i>. This ensures listeners
  56. * are only ever notified on the <i>Event Dispatch Thread</i>.
  57. *
  58. * @throws NullPointerException if {@code evt} is
  59. * {@code null}
  60. * @since 1.6
  61. */
  62. public void firePropertyChange(final PropertyChangeEvent evt) {
  63. if (evt == null) {
  64. throw new NullPointerException();
  65. }
  66. if (! isNotifyOnEDT()
  67. || SwingUtilities.isEventDispatchThread()) {
  68. super.firePropertyChange(evt);
  69. } else {
  70. SwingUtilities.invokeLater(
  71. new Runnable() {
  72. public void run() {
  73. firePropertyChange(evt);
  74. }
  75. });
  76. }
  77. }
  78. /**
  79. * Returns {@code notifyOnEDT} property.
  80. *
  81. * @return {@code notifyOnEDT} property
  82. * @see #SwingPropertyChangeSupport(Object sourceBean, boolean notifyOnEDT)
  83. * @since 1.6
  84. */
  85. public boolean isNotifyOnEDT() {
  86. return notifyOnEDT;
  87. }
  88. // Serialization version ID
  89. static final long serialVersionUID = 7162625831330845068L;
  90. /**
  91. * whether to notify listeners on EDT
  92. *
  93. * @serial
  94. * @since 1.6
  95. */
  96. private final boolean notifyOnEDT;
  97. }