Skip to content

Commit

Permalink
Moved selectionListeners to its own trait (SelectionNotifier) (part of
Browse files Browse the repository at this point in the history
  • Loading branch information
Henri Kerola committed Jan 3, 2015
1 parent e3297cf commit 3485629
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 17 deletions.
12 changes: 3 additions & 9 deletions addon/src/main/scala/vaadin/scala/Grid.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.vaadin.ui.{Grid => VaadinGrid}
import vaadin.scala.Grid._
import collection.JavaConverters._
import scala.reflect.ClassTag
import vaadin.scala.event.SelectionNotifier

package mixins {
trait GridMixin extends AbstractComponentMixin { self: com.vaadin.ui.Grid => }
Expand Down Expand Up @@ -84,7 +85,7 @@ object Grid {
def value: Option[Any] = property.value
}

case class SelectionEvent(grid: Grid, added: Seq[Any], removed: Seq[Any]) extends ComponentEvent(grid)
//case class SelectionEvent(grid: Grid, added: Seq[Any], removed: Seq[Any]) extends ComponentEvent(grid)

case class SortEvent(grid: Grid, sortOrder: Seq[(Any, SortDirection.Value)], userOriginated: Boolean) extends ComponentEvent(grid)

Expand Down Expand Up @@ -137,7 +138,7 @@ object Grid {
* @author Henri Kerola / Vaadin
*/
class Grid(override val p: VaadinGrid with GridMixin)
extends AbstractComponent(p) {
extends AbstractComponent(p) with SelectionNotifier {

def this() {
this(new VaadinGrid(new IndexedContainer().p) with GridMixin)
Expand Down Expand Up @@ -230,13 +231,6 @@ class Grid(override val p: VaadinGrid with GridMixin)

def deselect(itemId: Any): Boolean = p.deselect(itemId)

lazy val selectionListeners: ListenersSet[Grid.SelectionEvent => Unit] =
new ListenersTrait[Grid.SelectionEvent, GridSelectionListener] {
override def listeners = p.getListeners(classOf[com.vaadin.event.SelectionEvent])
override def addListener(elem: Grid.SelectionEvent => Unit) = p.addSelectionListener(new GridSelectionListener(elem))
override def removeListener(elem: GridSelectionListener) = p.removeSelectionListener(elem)
}

def sortOrder: Seq[(Any, SortDirection.Value)] =
p.getSortOrder.asScala map { so => (so.getPropertyId, SortDirection(so.getDirection.ordinal)) }

Expand Down
10 changes: 10 additions & 0 deletions addon/src/main/scala/vaadin/scala/event/SelectionEvent.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package vaadin.scala.event

import vaadin.scala.Component

/**
*
* @author Henri Kerola / Vaadin
*/
case class SelectionEvent(component: Component, added: Seq[Any], removed: Seq[Any])
extends ComponentEvent(component)
21 changes: 21 additions & 0 deletions addon/src/main/scala/vaadin/scala/event/SelectionNotifier.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package vaadin.scala.event

import com.vaadin.ui.{ AbstractComponent => VaadinAbstractComponent }
import com.vaadin.event.SelectionEvent.{SelectionNotifier => VaadinSelectionNotifier}
import vaadin.scala.ListenersSet
import vaadin.scala.internal.{ListenersTrait, GridSelectionListener}

/**
*
* @author Henri Kerola / Vaadin
*/
trait SelectionNotifier {
self: { def p: VaadinAbstractComponent with VaadinSelectionNotifier } =>

lazy val selectionListeners: ListenersSet[SelectionEvent => Unit] =
new ListenersTrait[SelectionEvent, GridSelectionListener] {
override def listeners = p.getListeners(classOf[com.vaadin.event.SelectionEvent])
override def addListener(elem: SelectionEvent => Unit) = p.addSelectionListener(new GridSelectionListener(elem))
override def removeListener(elem: GridSelectionListener) = p.removeSelectionListener(elem)
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package vaadin.scala.internal

import com.vaadin.event.SelectionEvent
import com.vaadin.event.{SelectionEvent => VaadinSelectionEvent}
import com.vaadin.event.SelectionEvent.SelectionListener
import vaadin.scala.Grid
import vaadin.scala.Component
import vaadin.scala.event.SelectionEvent

/**
*
* @author Henri Kerola / Vaadin
*/
class GridSelectionListener(val action: Grid.SelectionEvent => Unit)
class GridSelectionListener(val action: SelectionEvent => Unit)
extends SelectionListener with Listener {
override def select(event: SelectionEvent): Unit = {
val grid = wrapperFor[Grid](event.getSource).get
override def select(event: VaadinSelectionEvent): Unit = {
val component = wrapperFor[Component](event.getSource).get
val added = event.getAdded.toArray
val removed = event.getRemoved.toArray
action(Grid.SelectionEvent(grid, added, removed))
action(SelectionEvent(component, added, removed))
}
}
5 changes: 3 additions & 2 deletions addon/src/test/scala/vaadin/scala/tests/GridTests.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package vaadin.scala.tests

import com.vaadin.data.sort.SortOrder
import vaadin.scala.event.SelectionEvent
import com.vaadin.shared.data.sort.SortDirection
import com.vaadin.ui.Grid.{RowReference, Column}
import org.mockito.{ArgumentCaptor, Mockito}
Expand Down Expand Up @@ -208,9 +209,9 @@ class GridTests extends ScaladinTestSuite {
val itemId = grid.addRow("value")
var cnt = 0

val selectionListener = { e: Grid.SelectionEvent =>
val selectionListener = { e: SelectionEvent =>
cnt = cnt + 1
assert(grid == e.grid)
assert(grid == e.component)
assert(Seq(itemId) == e.added)
assert(e.removed.isEmpty)
}
Expand Down

0 comments on commit 3485629

Please sign in to comment.