]> source.dussan.org Git - vaadin-framework.git/commitdiff
refactored keyboard shortcuts (now using gwt's interface)
authorMatti Tahvonen <matti.tahvonen@itmill.com>
Thu, 30 Aug 2007 07:18:29 +0000 (07:18 +0000)
committerMatti Tahvonen <matti.tahvonen@itmill.com>
Thu, 30 Aug 2007 07:18:29 +0000 (07:18 +0000)
svn changeset:2153/svn branch:trunk

src/com/itmill/toolkit/terminal/gwt/client/ui/IShortcutAction.java [deleted file]
src/com/itmill/toolkit/terminal/gwt/client/ui/IShortcutActionHandler.java [new file with mode: 0644]
src/com/itmill/toolkit/terminal/gwt/client/ui/IView.java
src/com/itmill/toolkit/terminal/gwt/client/ui/ShortcutKeyCombination.java [deleted file]

diff --git a/src/com/itmill/toolkit/terminal/gwt/client/ui/IShortcutAction.java b/src/com/itmill/toolkit/terminal/gwt/client/ui/IShortcutAction.java
deleted file mode 100644 (file)
index 73a1e7c..0000000
+++ /dev/null
@@ -1,27 +0,0 @@
-package com.itmill.toolkit.terminal.gwt.client.ui;
-
-public class IShortcutAction {
-
-       private ShortcutKeyCombination sc;
-       private String caption;
-       private String key;
-
-       public IShortcutAction(String key, ShortcutKeyCombination sc, String caption) {
-               this.sc = sc;
-               this.key = key;
-               this.caption = caption;
-       }
-       
-       public ShortcutKeyCombination getShortcutCombination() {
-               return sc;
-       }
-       
-       public String getCaption() {
-               return caption;
-       }
-       
-       public String getKey() {
-               return key;
-       }
-
-}
diff --git a/src/com/itmill/toolkit/terminal/gwt/client/ui/IShortcutActionHandler.java b/src/com/itmill/toolkit/terminal/gwt/client/ui/IShortcutActionHandler.java
new file mode 100644 (file)
index 0000000..eb4845d
--- /dev/null
@@ -0,0 +1,158 @@
+package com.itmill.toolkit.terminal.gwt.client.ui;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+
+import com.google.gwt.user.client.ui.KeyboardListener;
+import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
+import com.itmill.toolkit.terminal.gwt.client.UIDL;
+
+/**
+ * A helper class to implement keyboard shorcut handling. Keeps
+ * a list of owners actions and fires actions to server. User class
+ * needs to delegate keyboard events to handleKeyboardEvents function.
+ * 
+ * @author IT Mill ltd
+ */
+public class IShortcutActionHandler {
+       private ArrayList actions = new ArrayList();
+       private ApplicationConnection client;
+       private String paintableId;
+       
+       private IShortcutActionHandler() {}
+       /**
+        * 
+        * @param pid Paintable id
+        * @param c reference to application connections
+        */
+       public IShortcutActionHandler(String pid, ApplicationConnection c) {
+               paintableId = pid;
+               client = c;
+       }
+
+       /**
+        * Updates list of actions this handler listens to.
+        * 
+        * @param c UIDL snippet containing actions
+        */
+       public void updateActionMap(UIDL c) {
+               actions.clear();
+               Iterator it = c.getChildIterator();
+               while(it.hasNext()) {
+                       UIDL action = (UIDL) it.next();
+                       
+                       int[] modifiers = null;
+                       if(action.hasAttribute("mk"))
+                               modifiers = action.getIntArrayAttribute("mk");
+                       
+                       ShortcutKeyCombination kc = new ShortcutKeyCombination(
+                                       action.getIntAttribute("kc"),
+                                       modifiers);
+                       String key = action.getStringAttribute("key");
+                       String caption = action.getStringAttribute("caption");
+                       actions.add(new IShortcutAction(key,kc, caption));
+               }
+       }
+
+       /**
+        * This method compares given key code and modifier keys to
+        * internal list of actions. If matching action is found it 
+        * is fired.
+        * 
+        * @param keyCode character typed
+        * @param modifiers modifier keys (bitmask like in {@link KeyboardListener})
+        */
+       public void handleKeyboardEvent(char keyCode, int modifiers) {
+               client.console.log("keyDownEvent");
+               
+               ShortcutKeyCombination kc = 
+                       new ShortcutKeyCombination(keyCode, modifiers);
+               Iterator it = actions.iterator();
+               while(it.hasNext()) {
+                       IShortcutAction a = (IShortcutAction) it.next();
+                       if(a.getShortcutCombination().equals(kc)) {
+                               client.updateVariable(paintableId, "action", a.getKey(), true);
+                               break;
+                       }
+               }
+       }
+       
+}
+
+class ShortcutKeyCombination {
+       
+       public static final int SHIFT = 16;
+       public static final int CTRL = 17;
+       public static final int ALT = 18;
+       
+       
+       
+       char keyCode = 0;
+       private int modifiersMask;
+       
+       public ShortcutKeyCombination() {
+       }
+       
+       ShortcutKeyCombination(char kc, int modifierMask) {
+               keyCode = kc;
+               this.modifiersMask = modifierMask;
+       }
+       
+       ShortcutKeyCombination(int kc, int[] modifiers) {
+               keyCode = (char) kc;
+               keyCode = Character.toUpperCase(keyCode);
+               
+               this.modifiersMask = 0;
+               if(modifiers != null) {
+                       for (int i = 0; i < modifiers.length; i++) {
+                               switch (modifiers[i]) {
+                               case ALT:
+                                       modifiersMask = modifiersMask | KeyboardListener.MODIFIER_ALT;
+                                       break;
+                               case CTRL:
+                                       modifiersMask = modifiersMask | KeyboardListener.MODIFIER_CTRL;
+                                       break;
+                               case SHIFT:
+                                       modifiersMask = modifiersMask | KeyboardListener.MODIFIER_SHIFT;
+                                       break;
+                               default:
+                                       break;
+                               }
+                       }
+               }
+       }
+       
+       public boolean equals(ShortcutKeyCombination other) {
+               if( this.keyCode == other.keyCode &&
+                               this.modifiersMask == other.modifiersMask)
+                       return true;
+               return false;
+       }
+}
+
+class IShortcutAction {
+
+       private ShortcutKeyCombination sc;
+       private String caption;
+       private String key;
+
+       public IShortcutAction(String key, ShortcutKeyCombination sc, String caption) {
+               this.sc = sc;
+               this.key = key;
+               this.caption = caption;
+       }
+       
+       public ShortcutKeyCombination getShortcutCombination() {
+               return sc;
+       }
+       
+       public String getCaption() {
+               return caption;
+       }
+       
+       public String getKey() {
+               return key;
+       }
+
+}
+
index 445eb4b9670c21c26d9d75fb2d3beed2ccb79b86..f657dba41739461b7baacaa2493f40c75046a66d 100644 (file)
@@ -1,20 +1,18 @@
 package com.itmill.toolkit.terminal.gwt.client.ui;
 
-import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.Iterator;
 
-import com.google.gwt.user.client.DOM;
-import com.google.gwt.user.client.Event;
 import com.google.gwt.user.client.Window;
+import com.google.gwt.user.client.ui.FocusPanel;
+import com.google.gwt.user.client.ui.KeyboardListener;
 import com.google.gwt.user.client.ui.RootPanel;
-import com.google.gwt.user.client.ui.SimplePanel;
 import com.google.gwt.user.client.ui.Widget;
 import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
 import com.itmill.toolkit.terminal.gwt.client.Paintable;
 import com.itmill.toolkit.terminal.gwt.client.UIDL;
 
-public class IView extends SimplePanel implements Paintable {
+public class IView extends FocusPanel implements Paintable, KeyboardListener {
        
        private String theme;
        
@@ -22,15 +20,13 @@ public class IView extends SimplePanel implements Paintable {
        
        private HashSet subWindows = new HashSet();
 
-       private ArrayList actions = new ArrayList();
-
-       private ApplicationConnection client;
-
        private String id;
+
+       private IShortcutActionHandler actionHandler;
        
        public IView() {
                super();
-               sinkEvents(Event.KEYEVENTS);
+               addKeyboardListener(this);
        }
        
        public String getTheme() {
@@ -38,7 +34,6 @@ public class IView extends SimplePanel implements Paintable {
        }
        
        public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
-               this.client = client;
                
                this.id = uidl.getId();
                
@@ -92,7 +87,10 @@ public class IView extends SimplePanel implements Paintable {
                                }
                                ((Paintable)w).updateFromUIDL(childUidl, client);
                        } else if ("actions".equals(childUidl.getTag())) {
-                               updateActionMap(childUidl);
+                               if(actionHandler == null) {
+                                       actionHandler = new IShortcutActionHandler(id, client);
+                               }
+                               actionHandler.updateActionMap(childUidl);
                        }
                }
                
@@ -105,49 +103,18 @@ public class IView extends SimplePanel implements Paintable {
                }
        }
 
-       private void updateActionMap(UIDL c) {
-               actions.clear();
-               Iterator it = c.getChildIterator();
-               while(it.hasNext()) {
-                       UIDL action = (UIDL) it.next();
-                       
-                       int[] modifiers = null;
-                       if(action.hasAttribute("mk"))
-                               modifiers = action.getIntArrayAttribute("mk");
-                       
-                       ShortcutKeyCombination kc = new ShortcutKeyCombination(
-                                       action.getIntAttribute("kc"),
-                                       modifiers);
-                       String key = action.getStringAttribute("key");
-                       String caption = action.getStringAttribute("caption");
-                       actions.add(new IShortcutAction(key,kc, caption));
-               }
+       public void onKeyDown(Widget sender, char keyCode, int modifiers) {
+               if(actionHandler != null)
+                       actionHandler.handleKeyboardEvent(keyCode, modifiers);
        }
 
-       public void onBrowserEvent(Event event) {
-               if(DOM.eventGetType(event) == Event.ONKEYDOWN) {
-                       handleKeyEvent(event);
-               }
-               super.onBrowserEvent(event);
+       public void onKeyPress(Widget sender, char keyCode, int modifiers) {
+               
        }
 
-       private void handleKeyEvent(Event event) {
-               client.console.log("keyEvent");
+       public void onKeyUp(Widget sender, char keyCode, int modifiers) {
                
-               ShortcutKeyCombination kc = new ShortcutKeyCombination();
-               kc.altKey = DOM.eventGetAltKey(event);
-               kc.ctrlKey = DOM.eventGetCtrlKey(event);
-               kc.shiftKey = DOM.eventGetShiftKey(event);
-               kc.keyCode = DOM.eventGetKeyCode(event);
-               Iterator it = actions.iterator();
-               while(it.hasNext()) {
-                       IShortcutAction a = (IShortcutAction) it.next();
-                       if(a.getShortcutCombination().equals(kc)) {
-                               client.updateVariable(id, "action", a.getKey(), true);
-                       }
-               }
        }
        
 }
 
-
diff --git a/src/com/itmill/toolkit/terminal/gwt/client/ui/ShortcutKeyCombination.java b/src/com/itmill/toolkit/terminal/gwt/client/ui/ShortcutKeyCombination.java
deleted file mode 100644 (file)
index 2732859..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-package com.itmill.toolkit.terminal.gwt.client.ui;
-
-public class ShortcutKeyCombination {
-       
-       public static final int SHIFT = 16;
-       public static final int CTRL = 17;
-       public static final int ALT = 18;
-       
-       
-       
-       int keyCode = 0;
-       boolean altKey = false;
-       boolean ctrlKey = false;
-       boolean shiftKey = false;
-       boolean metaKey = false;
-       
-       public ShortcutKeyCombination() {
-       }
-       
-       ShortcutKeyCombination(int kc, int[] modifiers) {
-               keyCode = kc;
-               if(modifiers != null) {
-                       for (int i = 0; i < modifiers.length; i++) {
-                               switch (modifiers[i]) {
-                               case ALT:
-                                       altKey = true;
-                                       break;
-                               case CTRL:
-                                       ctrlKey = true;
-                                       break;
-                               case SHIFT:
-                                       shiftKey = true;
-                                       break;
-                               default:
-                                       break;
-                               }
-                       }
-               }
-       }
-       
-       public boolean equals(ShortcutKeyCombination other) {
-               if( this.keyCode == other.keyCode &&
-                               this.altKey == other.altKey &&
-                               this.ctrlKey == other.ctrlKey &&
-                               this.shiftKey == other.shiftKey)
-                       return true;
-               return false;
-       }
-}
\ No newline at end of file