| package org.eclipse.osbp.vaadin.addons.keyevents.client; |
| |
| import java.util.HashMap; |
| import java.util.Map; |
| |
| import org.eclipse.osbp.vaadin.addons.keyevents.KeyEventExtension; |
| |
| import com.google.gwt.dom.client.NativeEvent; |
| import com.google.gwt.event.shared.HandlerRegistration; |
| import com.google.gwt.user.client.Event.NativePreviewEvent; |
| import com.google.gwt.user.client.Event.NativePreviewHandler; |
| import com.vaadin.client.ServerConnector; |
| import com.vaadin.client.VConsole; |
| import com.vaadin.client.communication.RpcProxy; |
| import com.vaadin.client.extensions.AbstractExtensionConnector; |
| import com.vaadin.event.ShortcutAction.KeyCode; |
| import com.vaadin.event.ShortcutAction.ModifierKey; |
| import com.vaadin.shared.ui.Connect; |
| |
| @SuppressWarnings("serial") |
| @Connect(KeyEventExtension.class) |
| public class KeyEventExtensionConnector extends AbstractExtensionConnector { |
| |
| private KeyEventServerRpc serverRPC = RpcProxy.create(KeyEventServerRpc.class, this); |
| private HandlerRegistration handlerRegistration; |
| protected Map<Integer, String> unkeyMapper = new HashMap<Integer, String>() { |
| private static final long serialVersionUID = 8801421284339992078L; |
| { |
| put(KeyCode.ARROW_DOWN, "ARROW_DOWN"); |
| put(KeyCode.ARROW_LEFT, "ARROW_LEFT"); |
| put(KeyCode.ARROW_RIGHT, "ARROW_RIGHT"); |
| put(KeyCode.ARROW_UP, "ARROW_UP"); |
| put(KeyCode.HOME, "HOME"); |
| put(KeyCode.END, "END"); |
| put(KeyCode.DELETE, "DELETE"); |
| put(KeyCode.BACKSPACE, "BACKSPACE"); |
| put(ModifierKey.ALT, "ALT"); |
| put(ModifierKey.SHIFT, "SHIFT"); |
| put(ModifierKey.CTRL, "CTRL"); |
| put(ModifierKey.META, "META"); |
| } |
| }; |
| |
| @Override |
| protected void init() { |
| super.init(); |
| |
| handlerRegistration = com.google.gwt.user.client.Event.addNativePreviewHandler(new NativePreviewHandler() { |
| @Override |
| public void onPreviewNativeEvent(NativePreviewEvent nativePreviewEvent) { |
| switch (nativePreviewEvent.getTypeInt()) { |
| case com.google.gwt.user.client.Event.ONKEYUP: |
| final Event event = createEvent(nativePreviewEvent.getNativeEvent()); |
| if (!unkeyMapper.containsKey(event.charCode)) { |
| serverRPC.onKeyPress(event); |
| nativePreviewEvent.cancel(); |
| } |
| break; |
| } |
| |
| } |
| }); |
| } |
| |
| public void onUnregister() { |
| super.onUnregister(); |
| |
| if (handlerRegistration != null) { |
| VConsole.log("Unregister handlerRegistratio for keyEvents"); |
| handlerRegistration.removeHandler(); |
| } |
| } |
| |
| @Override |
| protected void extend(final ServerConnector connector) { |
| // NOOP |
| } |
| |
| protected Event createEvent(NativeEvent event) { |
| Event e = new Event(); |
| e.charCode = event.getKeyCode(); |
| e.altKeyDown = event.getAltKey(); |
| e.controlKeyDown = event.getCtrlKey(); |
| e.metaKeyDown = event.getMetaKey(); |
| e.shiftKeyDown = event.getShiftKey(); |
| return e; |
| } |
| |
| } |