Skip to content

Commit

Permalink
Closes #19 - Add Attach/Detach Listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
LukaszByczynski committed Jun 20, 2015
1 parent 4c00a3b commit e0f5d2e
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import java.util
import java.util.Locale

import com.vaadin.event.ShortcutListener
import com.vaadin.server.ClientConnector.{AttachEvent, AttachListener, DetachEvent, DetachListener}
import com.vaadin.ui.AbstractComponent
import org.vaadin.addons.rinne.KeyShortcutAction
import org.vaadin.addons.rinne.events.ListenersSet
Expand All @@ -13,6 +14,32 @@ trait AbstractComponentMixin extends ComponentMixin {

lazy val shortcutListeners = new ShortcutListenersSet()

lazy val attachListeners = new ListenersSet[AttachEvent, AttachListener] {

override protected def addListener(listener: ListenerLambda): Unit = addAttachListener(
new Listener(listener) with AttachListener {
override def attach(attachEvent: AttachEvent): Unit = listener.apply(attachEvent)
}
)

override protected def removeListener(listener: AttachListener): Unit = removeAttachListener(listener)

override protected def listeners: util.Collection[_] = getListeners(classOf[AttachEvent])
}

lazy val detachListeners = new ListenersSet[DetachEvent, DetachListener] {

override protected def addListener(listener: ListenerLambda): Unit = addDetachListener(
new Listener(listener) with DetachListener {
override def detach(detachEvent: DetachEvent): Unit = listener.apply(detachEvent)
}
)

override protected def removeListener(listener: DetachListener): Unit = removeDetachListener(listener)

override protected def listeners: util.Collection[_] = getListeners(classOf[DetachEvent])
}

def locale_=(locale: Option[Locale]): Unit = setLocale(locale.orNull)

def locale_=(locale: Locale): Unit = setLocale(locale)
Expand All @@ -23,10 +50,10 @@ trait AbstractComponentMixin extends ComponentMixin {

def description: Option[String] = Option(getDescription)

def description_=(description: String): Unit = setDescription(description)

def description_=(description: Option[String]): Unit = setDescription(description.orNull)

def description_=(description: String): Unit = setDescription(description)

def immediate: Boolean = isImmediate

def immediate_=(immediate: Boolean): Unit = setImmediate(immediate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package org.vaadin.addons.rinne.mixins

import java.util.Locale

import com.vaadin.server.ClientConnector.{AttachEvent, AttachListener, DetachEvent, DetachListener}
import org.mockito.Mockito
import org.scalatest.FunSpec
import org.scalatest.mock.MockitoSugar
import org.vaadin.addons.rinne.VLabel
import org.vaadin.addons.rinne.{VLabel, VTextField}

class AbstractComponentMixinSpec extends FunSpec with MockitoSugar {

Expand Down Expand Up @@ -40,5 +41,129 @@ class AbstractComponentMixinSpec extends FunSpec with MockitoSugar {
abstractComponent.locale = Locale.US
Mockito.verify(abstractComponent).setLocale(Locale.US)
}

describe("attachListeners should") {

val listener1 = (e: AttachEvent) => println("1")
val listener2 = (e: AttachEvent) => println(e)

it("attachListeners.add a listener to textField") {
val textField = new VTextField

textField.attachListeners += (println(_))

assert(textField.attachListeners.size === 1)
}

it("attachListeners.remove a listener from textField") {
val textField = new VTextField

textField.attachListeners += listener1
textField.attachListeners -= listener1

assert(textField.attachListeners.size === 0)
}

it("attachListeners.iterator returns added listeners") {
val textField = new VTextField

textField.attachListeners += listener1
textField.attachListeners += listener2

val iter = textField.attachListeners.iterator
assert(iter.next() === listener1)
assert(iter.next() === listener2)
assert(!iter.hasNext)
}

it("attachListeners.contains returns true for added listener") {
val textField = new VTextField

textField.attachListeners += listener1
assert(textField.attachListeners.contains(listener1))
}

it("attachListeners.contains returns false for non-added listener") {
val textField = new VTextField

assert(!textField.attachListeners.contains(listener2))
}

it("execute listeners") {
var executed = false
val textField = new VTextField {
attachListeners += {
executed = true
}
}

import scala.collection.JavaConverters._
textField.getListeners(classOf[AttachEvent]).asScala
.foreach { case e: AttachListener => e.attach(null) }
assert(executed)
}
}

describe("detachListeners should") {

val listener1 = (e: DetachEvent) => println("1")
val listener2 = (e: DetachEvent) => println(e)

it("detachListeners.add a listener to textField") {
val textField = new VTextField

textField.detachListeners += (println(_))

assert(textField.detachListeners.size === 1)
}

it("detachListeners.remove a listener from textField") {
val textField = new VTextField

textField.detachListeners += listener1
textField.detachListeners -= listener1

assert(textField.detachListeners.size === 0)
}

it("detachListeners.iterator returns added listeners") {
val textField = new VTextField

textField.detachListeners += listener1
textField.detachListeners += listener2

val iter = textField.detachListeners.iterator
assert(iter.next() === listener1)
assert(iter.next() === listener2)
assert(!iter.hasNext)
}

it("detachListeners.contains returns true for added listener") {
val textField = new VTextField

textField.detachListeners += listener1
assert(textField.detachListeners.contains(listener1))
}

it("detachListeners.contains returns false for non-added listener") {
val textField = new VTextField

assert(!textField.detachListeners.contains(listener2))
}

it("execute listeners") {
var executed = false
val textField = new VTextField {
detachListeners += {
executed = true
}
}

import scala.collection.JavaConverters._
textField.getListeners(classOf[DetachEvent]).asScala
.foreach { case e: DetachListener => e.detach(null) }
assert(executed)
}
}
}
}

0 comments on commit e0f5d2e

Please sign in to comment.