001 /* 002 * Created on Mar 26, 2008 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 * 014 * Copyright @2008-2010 the original author or authors. 015 */ 016 package org.fest.swing.keystroke; 017 018 import static org.fest.util.Objects.HASH_CODE_PRIME; 019 020 import javax.swing.KeyStroke; 021 022 /** 023 * Understands a mapping between a character and a <code>{@link KeyStroke}</code>. 024 * 025 * @author Yvonne Wang 026 */ 027 public class KeyStrokeMapping { 028 029 private final char character; 030 private final KeyStroke keyStroke; 031 032 /** 033 * Creates a new </code>{@link KeyStrokeMapping}</code>. 034 * @param character the character corresponding to the intended <code>KeyStroke</code>. 035 * @param keyCode the numeric key code for the intended <code>KeyStroke</code>. 036 * @param modifiers the set of modifiers for the intended <code>KeyStroke</code>. 037 * @return the created <code>KeyStrokeMapping</code>. 038 */ 039 public static KeyStrokeMapping mapping(char character, int keyCode, int modifiers) { 040 return new KeyStrokeMapping(character, keyCode, modifiers); 041 } 042 043 /** 044 * Creates a new </code>{@link KeyStrokeMapping}</code>. 045 * @param character the character corresponding to the intended <code>KeyStroke</code>. 046 * @param keyCode the numeric key code for the intended <code>KeyStroke</code>. 047 * @param modifiers the set of modifiers for the intended <code>KeyStroke</code>. 048 */ 049 public KeyStrokeMapping(char character, int keyCode, int modifiers) { 050 this(character, KeyStroke.getKeyStroke(keyCode, modifiers)); 051 } 052 053 /** 054 * Creates a new </code>{@link KeyStrokeMapping}</code>. 055 * @param character the character corresponding to the given <code>KeyStroke</code>. 056 * @param keyStroke the <code>KeyStroke</code> corresponding to the given character. 057 */ 058 public KeyStrokeMapping(char character, KeyStroke keyStroke) { 059 this.character = character; 060 this.keyStroke = keyStroke; 061 } 062 063 /** 064 * Returns the character corresponding to this mapping's <code>{@link #keyStroke()}</code>. 065 * @return the character corresponding to this mapping's <code>KeyStroke</code>. 066 */ 067 public char character() { 068 return character; 069 } 070 071 /** 072 * Returns the <code>{@link KeyStroke}</code> corresponding to this mapping's <code>{@link #character()}</code>. 073 * @return the <code>KeyStroke</code> corresponding to this mapping's character. 074 */ 075 public KeyStroke keyStroke() { 076 return keyStroke; 077 } 078 079 /** @see Object#equals(Object) */ 080 @Override public boolean equals(Object o) { 081 if (this == o) return true; 082 if (o == null) return false; 083 if (!(o instanceof KeyStrokeMapping)) return false; 084 KeyStrokeMapping other = (KeyStrokeMapping) o; 085 if (character != other.character) return false; 086 if (keyStroke == null) return other.keyStroke == null; 087 if (other.keyStroke == null) return false; 088 if (keyStroke.getKeyCode() != other.keyStroke.getKeyCode()) return false; 089 return keyStroke.getModifiers() == other.keyStroke.getModifiers(); 090 } 091 092 /** @see Object#hashCode() */ 093 @Override public int hashCode() { 094 int prime = HASH_CODE_PRIME; 095 int result = 1; 096 result = prime * result + character; 097 if (keyStroke == null) return result; 098 result = prime * result + keyStroke.getKeyCode(); 099 result = prime * result + keyStroke.getModifiers(); 100 return result; 101 } 102 103 /** @see Object#toString() */ 104 @Override public String toString() { 105 StringBuilder b = new StringBuilder(); 106 b.append(getClass().getSimpleName()).append("["); 107 b.append("character='").append(character).append("',"); 108 b.append("keyStroke=").append(keyStroke).append("]"); 109 return b.toString(); 110 } 111 }