Skip to content

Commit

Permalink
Resolves #18 KeyShortcut listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
LukaszByczynski committed May 7, 2015
1 parent 8b01b42 commit 074a7ef
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 10 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name := "rinne"
version := "0.5.0-SNAPSHOT"

scalaVersion := "2.11.6"
scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature")
crossScalaVersions := Seq("2.10.5", "2.11.6")

libraryDependencies ++= Dependencies.addon(scalaVersion.value)
Expand Down
31 changes: 31 additions & 0 deletions src/main/scala/org/vaadin/addons/rinne/KeyShortcut.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
package org.vaadin.addons.rinne

import com.vaadin.server.Resource

case class KeyShortcutAction(caption: Option[String], shorthandCaption: Option[String], icon: Option[Resource], key: KeyShortcut, action: (Any, Any) => Unit) {

def this(caption: String, key: KeyShortcut, action: => Unit) {
this(Some(caption), None, None, key, (_, _) => action)
}

def this(caption: String, key: KeyShortcut, action: (Any, Any) => Unit) {
this(Some(caption), None, None, key, action)
}

def this(shorthandCaption: String, action: => Unit, modifiers: KeyModifier*) {
this(None, Some(shorthandCaption), None, KeyShortcut(KeyCode.None, modifiers.toArray: _*), (_, _) => action)
}

def this(shorthandCaption: String, action: (Any, Any) => Unit, modifiers: KeyModifier*) {
this(None, Some(shorthandCaption), None, KeyShortcut(KeyCode.None, modifiers.toArray: _*), action)
}

def this(caption: String, icon: Resource, key: KeyShortcut, action: => Unit) {
this(Some(caption), None, Some(icon), key, (_, _) => action)
}

def this(caption: String, icon: Resource, key: KeyShortcut, action: (Any, Any) => Unit) {
this(Some(caption), None, Some(icon), key, action)
}
}

case class KeyShortcut(keyCode: KeyCode, modifiers: KeyModifier*)

case class KeyCode(value: Int)
Expand All @@ -8,6 +37,8 @@ case class KeyModifier(value: Int)

object KeyCode {

val None = KeyCode(-1)

val Enter = KeyCode(13)

val Escape = KeyCode(27)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,78 @@
package org.vaadin.addons.rinne.mixins

import java.util

import com.vaadin.event.ShortcutListener
import com.vaadin.ui.AbstractComponent
import org.vaadin.addons.rinne.KeyShortcutAction
import org.vaadin.addons.rinne.events.ListenersSet

trait AbstractComponentMixin extends ComponentMixin {
this: AbstractComponent =>

def description: Option[String] = Option(getDescription)
lazy val shortcutListeners = new ShortcutListenersSet()

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

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

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

def immediate: Boolean = isImmediate

def immediate_=(immediate: Boolean): Unit = setImmediate(immediate)

def data: Any = getData

addShortcutListener(
new ShortcutListener("1", 1, Array[Int](): _*) {
override def handleAction(sender: scala.Any, target: scala.Any): Unit = {}
})

def data_=(data: Any): Unit = setData(data)
}

class ShortcutListenersSet extends ListenersSet[Any, ShortcutListener] {
private val _listenersToKeyShortcutActionMap = new util.HashMap[KeyShortcutAction, ShortcutListener]()

def +=(elem: KeyShortcutAction) = {
val listener = elem match {
case KeyShortcutAction(Some(caption), None, None, key, action) =>
new ShortcutListener(caption, key.keyCode.value, key.modifiers.map(_.value).toArray: _*) {
override def handleAction(sender: scala.Any, target: scala.Any): Unit = action(sender, target)
}
case KeyShortcutAction(Some(caption), None, Some(icon), key, action) =>
new ShortcutListener(caption, icon, key.keyCode.value, key.modifiers.map(_.value).toArray: _*) {
override def handleAction(sender: scala.Any, target: scala.Any): Unit = action(sender, target)
}
case KeyShortcutAction(None, Some(shorthandCaption), None, key, action) =>
new ShortcutListener(shorthandCaption, key.modifiers.map(_.value).toArray: _*) {
override def handleAction(sender: scala.Any, target: scala.Any): Unit = action(sender, target)
}
case _ => throw new IllegalArgumentException("Unknow KeyShortcutAction argument configuration")
}
addShortcutListener(listener)
_listenersToKeyShortcutActionMap.put(elem, listener)
this
}


def -=(elem: KeyShortcutAction) = {
Option(_listenersToKeyShortcutActionMap.remove(elem)) match {
case Some(listener) => removeShortcutListener(listener)
case _ =>
}
this
}

override protected def addListener(listener: ListenerLambda): Unit = {
throw new IllegalArgumentException("Use KeyShortcutAction instead ListenerLambda")
}

override protected def removeListener(listener: ShortcutListener): Unit = {
throw new IllegalArgumentException("Use KeyShortcutAction instead ListenerLambda")
}

override protected def listeners: util.Collection[_] = _listenersToKeyShortcutActionMap.values()
}

}
12 changes: 5 additions & 7 deletions src/main/scala/org/vaadin/addons/rinne/mixins/ButtonMixin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ trait ButtonMixin extends AbstractComponentMixin with BlurNotifierMixin with Foc

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

private var _clickKeyShortcut: Option[KeyShortcut] = None

def clickKeyShortcut_=(clickShortcut: Option[KeyShortcut]): Unit = {
Expand All @@ -31,8 +32,6 @@ trait ButtonMixin extends AbstractComponentMixin with BlurNotifierMixin with Foc
}
}

def clickKeyShortcut: Option[KeyShortcut] = _clickKeyShortcut

def disableOnClick: Boolean = isDisableOnClick

def disableOnClick_=(disableOnClick: Boolean): Unit = {
Expand All @@ -41,14 +40,13 @@ trait ButtonMixin extends AbstractComponentMixin with BlurNotifierMixin with Foc

def htmlContentAllowed: Boolean = isHtmlContentAllowed

def clickKeyShortcut_=(clickShortcut: KeyShortcut): Unit =
{
this.clickKeyShortcut = Option(clickShortcut)
}

def htmlContentAllowed_=(htmlContentAllowed: Boolean): Unit = {
setHtmlContentAllowed(htmlContentAllowed)
}

def clickKeyShortcut: Option[KeyShortcut] = _clickKeyShortcut

def clickKeyShortcut_=(clickShortcut: KeyShortcut): Unit = {
clickKeyShortcut = Option(clickShortcut)
}
}

0 comments on commit 074a7ef

Please sign in to comment.