org.vostok.vaadin.addon.keypress
Class KeyPressAction

java.lang.Object
  extended by com.vaadin.event.Action
      extended by com.vaadin.event.ShortcutAction
          extended by org.vostok.vaadin.addon.keypress.KeyPressAction
All Implemented Interfaces:
com.vaadin.event.Action.Listener, java.io.Serializable

public abstract class KeyPressAction
extends com.vaadin.event.ShortcutAction
implements com.vaadin.event.Action.Listener

KeyPressAction allow you to register a specific listener for a component when using the same shortcut accross several others component.
It acts as a proxy between components and the default vaadin Shortcut listener.

ClientConnector.attach / ClientConnector.detach :
KeyPressAction can detect an ActionHanler from component. If it fail, it will use default, e.i. UI. You can explicitly call addAction / removeAction within ClientConnector.attach / ClientConnector.detach methods instead. Your action will then be linked to your component's proper ActionHanler, and removed when view exit.
Bulk registration :
It's common to need the same shortcut for several components. Instead of registering shortcut ones by ones, you can use addWithFactory / removeWithFactory. Note its still recommanded to override ClientConnector.attach / ClientConnector.detach.

PassThroughHandler
Keep in mind KeyPressAction is some kind of proxy, it works "globaly", at UI level, listens for any shortcut and take care of filtering and dispaching events to correct component. But some component might need to prevent some shortcut, for example, TextArea must prevent <enter> because it's needed internally (else this may block a carriage return). Unfortunately it's not possible to push back key to the component, but you can use PassThroughHandler to "fix" it.
KeyPressAction already takes care of <enter> with TextArea.

RichTextArea issue
Unlike TextArea, RichTextArea lacks caret information. It's not possible to reinject <enter>. See sample at http://vaadin.vostoksystem.eu

Author:
levreau.jerome@vostoksystem.eu
See Also:
Serialized Form

Nested Class Summary
static interface KeyPressAction.KeyPressActionListener
           
static interface KeyPressAction.PassThroughHandler
           
 
Nested classes/interfaces inherited from class com.vaadin.event.ShortcutAction
com.vaadin.event.ShortcutAction.KeyCode, com.vaadin.event.ShortcutAction.ModifierKey
 
Nested classes/interfaces inherited from class com.vaadin.event.Action
com.vaadin.event.Action.Container, com.vaadin.event.Action.Handler, com.vaadin.event.Action.Listener, com.vaadin.event.Action.Notifier, com.vaadin.event.Action.ShortcutNotifier
 
Field Summary
 
Fields inherited from class com.vaadin.event.ShortcutAction
SHORTHAND_CHAR_ALT, SHORTHAND_CHAR_CTRL, SHORTHAND_CHAR_SHIFT
 
Constructor Summary
KeyPressAction(com.vaadin.ui.Component target, int key, int... modifier)
          Create a new action on target for key and modifier.
 
Method Summary
static void addAction(KeyPressAction action)
          Add an action.
static void addAction(KeyPressAction[] action)
          Add a list of actions. target can be different.
static void addPassThrough(java.lang.Class c, KeyPressAction.PassThroughHandler listener)
          Keep in mind KeyPressAction, as build on top of shortcutListener is still global (within an Action.Handler).
static void addWithFactory(com.vaadin.ui.Component[] list, KeyPressAction.KeyPressActionListener listener, int key, int... modifier)
          batch addition.
static KeyPressAction getKeyPressAction(com.vaadin.ui.Component c, int key, int... modifier)
          Return KeyPresAction from internal db
 com.vaadin.ui.Component getTarget()
          return target this KeyPressAction was register for
 void handleAction(java.lang.Object sender, java.lang.Object target)
          DON'T OVERRIDE !!!
abstract  void handleKeyPress(com.vaadin.ui.Component target)
          Your business code here, component is the one - and only the -one - you registered the KeyPressAction for.
static boolean isThisMatch(int keycode_a, int[] modifier_a, int keycode_b, int[] modifier_b)
          convenience method to check if 2 keypress sequence match
static void removeAction(KeyPressAction action)
          Unregister an action.
static void removeAction(KeyPressAction[] action)
          Unregister a list of actions. target can be different.
static void removeWithFactory(com.vaadin.ui.Component[] list, int key, int... modifier)
           
 
Methods inherited from class com.vaadin.event.ShortcutAction
getKeyCode, getModifiers
 
Methods inherited from class com.vaadin.event.Action
getCaption, getIcon, setCaption, setIcon
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

KeyPressAction

public KeyPressAction(com.vaadin.ui.Component target,
                      int key,
                      int... modifier)
Create a new action on target for key and modifier.
You must add action, either by using one of static method ( recommended) of into an ActionHandler (panel, window,etc...)

Parameters:
target - target this action will respond to
key - shortcut, use ShortcutAction.KeyCode
modifier - mdifier, use ShortcutAction.ModifierKey, can be null
See Also:
ShortcutAction
Method Detail

handleAction

public void handleAction(java.lang.Object sender,
                         java.lang.Object target)
DON'T OVERRIDE !!! INTERNAL USE !!! ydbu must use handleKeyPress instead

Specified by:
handleAction in interface com.vaadin.event.Action.Listener
Parameters:
sender -
target -

handleKeyPress

public abstract void handleKeyPress(com.vaadin.ui.Component target)
Your business code here, component is the one - and only the -one - you registered the KeyPressAction for.
If you registered a shortcut accross several component, only proper one will receive callback.
You can get keypress / modifier with call to this.getKeyCode() / this.getModifiers()

Parameters:
target -

getTarget

public com.vaadin.ui.Component getTarget()
return target this KeyPressAction was register for

Returns:

addAction

public static void addAction(KeyPressAction action)
Add an action.
Will try to locate an Action.Handler from target an its parents. If none, will use UI (like default shortcut).
You should insure to call this method after attaching your component to a porper actionHandler( panel, window, etc...) or within a ClientConnector.attach.

Parameters:
action - can be null

addAction

public static void addAction(KeyPressAction[] action)
Add a list of actions. target can be different.

Parameters:
action - can be null

removeAction

public static void removeAction(KeyPressAction action)
Unregister an action.

Parameters:
action - can be null

removeAction

public static void removeAction(KeyPressAction[] action)
Unregister a list of actions. target can be different.

Parameters:
action - array, can be null

addWithFactory

public static void addWithFactory(com.vaadin.ui.Component[] list,
                                  KeyPressAction.KeyPressActionListener listener,
                                  int key,
                                  int... modifier)
batch addition. Useful when several component need the same shortcut

Parameters:
list -
listener -
key -
modifier -

removeWithFactory

public static void removeWithFactory(com.vaadin.ui.Component[] list,
                                     int key,
                                     int... modifier)
Parameters:
list -
key -
modifier -

getKeyPressAction

public static KeyPressAction getKeyPressAction(com.vaadin.ui.Component c,
                                               int key,
                                               int... modifier)
Return KeyPresAction from internal db

Parameters:
c - component KeyPressAction was registered on
key - ShortcutAction.KeyCode KeyPressAction was registered with
modifier - ShortcutAction.KeyCode KeyPressAction was registered with
Returns:

addPassThrough

public static void addPassThrough(java.lang.Class c,
                                  KeyPressAction.PassThroughHandler listener)
Keep in mind KeyPressAction, as build on top of shortcutListener is still global (within an Action.Handler). Therefore, 'A' shortcut will prevent ALL components from directly use the key 'A'. Though 'A' example is a little nut, you migh need to catch some. This is the purpose of PassThroughHandler. for example KeyPressAction already provide a pass Though for 'enter' key press on TextArea component. Any attempt for 'enter' shortcut will be denied and a new line will be send to the TextArea anyway.

Parameters:
c -
listener -

isThisMatch

public static boolean isThisMatch(int keycode_a,
                                  int[] modifier_a,
                                  int keycode_b,
                                  int[] modifier_b)
convenience method to check if 2 keypress sequence match

Parameters:
keycode_a -
modifier_a -
keycode_b -
modifier_b -
Returns: