From 348562960ea2d2a2c93180b7d497d07c9a38bea0 Mon Sep 17 00:00:00 2001 From: Henri Kerola Date: Fri, 2 Jan 2015 19:04:09 -0800 Subject: [PATCH] Moved selectionListeners to its own trait (SelectionNotifier) (part of #59) --- addon/src/main/scala/vaadin/scala/Grid.scala | 12 +++-------- .../vaadin/scala/event/SelectionEvent.scala | 10 +++++++++ .../scala/event/SelectionNotifier.scala | 21 +++++++++++++++++++ .../internal/GridSelectionListener.scala | 13 ++++++------ .../scala/vaadin/scala/tests/GridTests.scala | 5 +++-- 5 files changed, 44 insertions(+), 17 deletions(-) create mode 100644 addon/src/main/scala/vaadin/scala/event/SelectionEvent.scala create mode 100644 addon/src/main/scala/vaadin/scala/event/SelectionNotifier.scala diff --git a/addon/src/main/scala/vaadin/scala/Grid.scala b/addon/src/main/scala/vaadin/scala/Grid.scala index a0481152..67fba822 100644 --- a/addon/src/main/scala/vaadin/scala/Grid.scala +++ b/addon/src/main/scala/vaadin/scala/Grid.scala @@ -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 => } @@ -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) @@ -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) @@ -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)) } diff --git a/addon/src/main/scala/vaadin/scala/event/SelectionEvent.scala b/addon/src/main/scala/vaadin/scala/event/SelectionEvent.scala new file mode 100644 index 00000000..11b2db30 --- /dev/null +++ b/addon/src/main/scala/vaadin/scala/event/SelectionEvent.scala @@ -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) diff --git a/addon/src/main/scala/vaadin/scala/event/SelectionNotifier.scala b/addon/src/main/scala/vaadin/scala/event/SelectionNotifier.scala new file mode 100644 index 00000000..9a8195b4 --- /dev/null +++ b/addon/src/main/scala/vaadin/scala/event/SelectionNotifier.scala @@ -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) + } +} diff --git a/addon/src/main/scala/vaadin/scala/internal/GridSelectionListener.scala b/addon/src/main/scala/vaadin/scala/internal/GridSelectionListener.scala index e0164b83..98a50d95 100644 --- a/addon/src/main/scala/vaadin/scala/internal/GridSelectionListener.scala +++ b/addon/src/main/scala/vaadin/scala/internal/GridSelectionListener.scala @@ -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)) } } diff --git a/addon/src/test/scala/vaadin/scala/tests/GridTests.scala b/addon/src/test/scala/vaadin/scala/tests/GridTests.scala index 66adc01f..03931748 100644 --- a/addon/src/test/scala/vaadin/scala/tests/GridTests.scala +++ b/addon/src/test/scala/vaadin/scala/tests/GridTests.scala @@ -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} @@ -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) }