001: /*
002: * GWT-Ext Widget Library
003: * Copyright(c) 2007-2008, GWT-Ext.
004: * licensing@gwt-ext.com
005: *
006: * http://www.gwt-ext.com/license
007: */
008: package com.gwtext.client.util;
009:
010: import com.google.gwt.core.client.JavaScriptObject;
011: import com.google.gwt.user.client.Element;
012: import com.gwtext.client.core.JsObject;
013:
014: /**
015: * Handles mapping keys to actions for an element. One key map can be used for multiple actions.
016: * If you bind a callback function to a KeyMap, anytime the KeyMap handles an expected key combination it will call the
017: * function with this signature (if the match is a multi-key combination the callback will still be called only once):
018: * (String key, EventObject e) A KeyMap can also handle a string representation of keys.
019: * <p/>
020: * <pre>
021: * // map one key by key code
022: * KeyMap map = new KeyMap("my-element", new KeyMapConfig() {
023: * {
024: * setKey(13); // or EventObject.ENTER
025: * setKeyListener(new KeyListener() {
026: * public void onKey(int key, EventObject e) {
027: * //handle key
028: * }
029: * });
030: * }
031: * });
032: * <p/>
033: * // map multiple keys to one action by string
034: * KeyMap map = new KeyMap("my-element", new KeyMapConfig() {
035: * {
036: * setKey("a\r\n\t");
037: * setKeyListener(new KeyListener() {
038: * public void onKey(int key, EventObject e) {
039: * //handle key
040: * }
041: * });
042: * }
043: * });
044: * <p/>
045: * KeyMap map = new KeyMap("my-element", new KeyMapConfig() {
046: * {
047: * setKey(new int[]{10, 13});
048: * setKeyListener(new KeyListener() {
049: * public void onKey(int key, EventObject e) {
050: * // return was pressed
051: * }
052: * });
053: * }
054: * });
055: * <p/>
056: * KeyMap map = new KeyMap("my-element", new KeyMapConfig() {
057: * {
058: * setKey("abc");
059: * setKeyListener(new KeyListener() {
060: * public void onKey(int key, EventObject e) {
061: * // a, b or c was pressed
062: * }
063: * });
064: * }
065: * });
066: * <p/>
067: * KeyMap map = new KeyMap("my-element", new KeyMapConfig() {
068: * {
069: * setKey("\t");
070: * setCtrl(true);
071: * setShift(true);
072: * setKeyListener(new KeyListener() {
073: * public void onKey(int key, EventObject e) {
074: * // Ctrl + shift + tab was pressed
075: * }
076: * });
077: * }
078: * });
079: * <p/>
080: * </pre>
081: */
082: public class KeyMap extends JsObject {
083:
084: public KeyMap(JavaScriptObject jsObj) {
085: super (jsObj);
086: }
087:
088: /**
089: * Create a new key map for the element.
090: *
091: * @param id the element id
092: * @param config keymap config
093: */
094: public KeyMap(String id, KeyMapConfig config) {
095: this (id, config, null);
096: }
097:
098: /**
099: * Create a new key map for the element.
100: *
101: * @param el the element
102: * @param config keymap config
103: */
104: public KeyMap(Element el, KeyMapConfig config) {
105: this (el, config, null);
106: }
107:
108: /**
109: * Create a new key map for the element.
110: *
111: * @param id the element id
112: * @param config keymap config
113: * @param eventName the event to bind to (defaults to "keydown")
114: */
115: public KeyMap(String id, KeyMapConfig config, String eventName) {
116: jsObj = create(id, config.getJsObj(), eventName);
117: }
118:
119: /**
120: * Create a new key map for the element.
121: *
122: * @param el the element
123: * @param config keymap config
124: * @param eventName the event to bind to (defaults to "keydown")
125: */
126: public KeyMap(Element el, KeyMapConfig config, String eventName) {
127: jsObj = create(el, config.getJsObj(), eventName);
128: }
129:
130: private static native JavaScriptObject create(Element el,
131: JavaScriptObject config, String eventName) /*-{
132: return new $wnd.Ext.KeyMap(el, config, eventName);
133: }-*/;
134:
135: private static native JavaScriptObject create(String id,
136: JavaScriptObject config, String eventName) /*-{
137: return new $wnd.Ext.KeyMap(id, config, eventName);
138: }-*/;
139:
140: private static KeyMap instance(JavaScriptObject jsObj) {
141: return new KeyMap(jsObj);
142: }
143:
144: /**
145: * Add a new binding to this KeyMap.
146: *
147: * @param config new binding config
148: */
149: public native void addBinding(KeyMapConfig config) /*-{
150: var km = this.@com.gwtext.client.core.JsObject::getJsObj()();
151: var configJS = this.@com.gwtext.client.core.JsObject::getJsObj()();
152: km.addBinding(configJS);
153: }-*/;
154:
155: /**
156: * Disable this KeyMap.
157: */
158: public native void disable() /*-{
159: var km = this.@com.gwtext.client.core.JsObject::getJsObj()();
160: km.disable();
161: }-*/;
162:
163: /**
164: * Enable this KeyMap,
165: */
166: public native void enable() /*-{
167: var km = this.@com.gwtext.client.core.JsObject::getJsObj()();
168: km.enable();
169: }-*/;
170:
171: /**
172: * Returns true if this KeyMap is enabled.
173: *
174: * @return true if enabled
175: */
176: public native boolean isEnabled() /*-{
177: var km = this.@com.gwtext.client.core.JsObject::getJsObj()();
178: return km.isEnabled();
179: }-*/;
180:
181: /**
182: * True to stop the event from bubbling and prevent the default browser action if the key was handled by the KeyMap
183: * (defaults to false).
184: *
185: * @param stopEvent true to stop event bubbling
186: */
187: public native void setStopEvent(boolean stopEvent) /*-{
188: var km = this.@com.gwtext.client.core.JsObject::getJsObj()();
189: km.stopEvent = stopEvent;
190: }-*/;
191: }
|