-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b01b42
commit 074a7ef
Showing
4 changed files
with
96 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 59 additions & 3 deletions
62
src/main/scala/org/vaadin/addons/rinne/mixins/AbstractComponentMixin.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters