Skip to content

Instantly share code, notes, and snippets.

@rhiguchi
Created February 24, 2012 16:31
Show Gist options
  • Save rhiguchi/1901885 to your computer and use it in GitHub Desktop.
Save rhiguchi/1901885 to your computer and use it in GitHub Desktop.
Scala で PropertyChangeEvent の発行とかを実装するミックスイン用トレイト ref: http://qiita.com/items/2791
import java.beans.{PropertyChangeListener, PropertyChangeSupport, PropertyChangeEvent}
trait PropertyChangePublisher {
private lazy val propertyChangeSupport = new PropertyChangeSupport(this)
def addPropertyChangeListener(listener: PropertyChangeListener) =
propertyChangeSupport.addPropertyChangeListener(listener)
def removePropertyChangeListener(listener: PropertyChangeListener) =
propertyChangeSupport.removePropertyChangeListener(listener)
protected def firePropertyChange(propertyName: String, oldValue: Any, newValue: Any) =
propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue)
protected def firePropertyChange(event: PropertyChangeEvent) =
propertyChangeSupport.firePropertyChange(event)
protected def fireIndexedPropertyChange(
propertyName: String, index: Int, oldValue: Any, newValue: Any) =
propertyChangeSupport.fireIndexedPropertyChange(propertyName, index, oldValue, newValue)
}
class BeanClass extends PropertyChangePublisher {
private var _name = ""
def name = _name
def name_=(newName: String) {
val oldValue = _name
_name = newName
firePropertyChange("name", oldValue, newName)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment