001 /*
002 * Copyright 2006 Mat Gessel <mat.gessel@gmail.com>
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016 package asquare.gwt.tk.client.util;
017
018 import java.util.HashMap;
019
020 import com.google.gwt.user.client.Command;
021
022 /**
023 * A map that facilitates looking up which command is associated to a hot key.
024 * Supports (char, Command) pairs. Similar to the Swing
025 * {@link javax.swing.ActionMap ActionMap}.
026 */
027 public class KeyMap extends HashMap
028 {
029 /**
030 * Create a mapping between a hot key and a command. To ensure case
031 * consistency across keypress and keydown/keyup events you can convert hot
032 * key characters to upper-case before creating the mapping.
033 *
034 * @param keyCode a keyboard shortcut
035 * @param command a Command to associate with the hot key
036 */
037 public void put(char keyCode, Command command)
038 {
039 put(new Character(keyCode), command);
040 }
041
042 /**
043 * Determines if the map contains a mapping for the specified hot key.
044 *
045 * @param keyCode a keyboard shortcut
046 * @return <code>true</code> if a mapping exists for <code>keyCode</code>
047 */
048 public boolean containsKey(char keyCode)
049 {
050 return containsKey(new Character(keyCode));
051 }
052
053 /**
054 * Get the command to which the specified hot key is mapped.
055 *
056 * @param keyCode a keyboard shortcut
057 * @return a Command or <code>null</code>
058 */
059 public Command get(char keyCode)
060 {
061 return (Command) get(new Character(keyCode));
062 }
063
064 /**
065 * Remove a mapping for the specified hot key. Returns the previously mapped
066 * command, if applicable.
067 *
068 * @param keyCode a keyboard shortcut
069 * @return a Command or <code>null</code>
070 */
071 public Command remove(char keyCode)
072 {
073 return (Command) remove(new Character(keyCode));
074 }
075 }