001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Pavel Dolgov
019: * @version $Revision$
020: */package java.awt;
021:
022: import java.awt.event.InputEvent;
023: import java.awt.event.KeyEvent;
024: import java.io.Serializable;
025:
026: import org.apache.harmony.misc.HashCode;
027:
028: public class MenuShortcut implements Serializable {
029:
030: private static final long serialVersionUID = 143448358473180225L;
031:
032: private final int keyCode;
033: private final boolean shiftModifier;
034:
035: public MenuShortcut(int key) {
036: this (key, false);
037: }
038:
039: public MenuShortcut(int key, boolean useShiftModifier) {
040: keyCode = key;
041: shiftModifier = useShiftModifier;
042: }
043:
044: @Override
045: public int hashCode() {
046: int hashCode = HashCode.EMPTY_HASH_CODE;
047: hashCode = HashCode.combine(hashCode, keyCode);
048: hashCode = HashCode.combine(hashCode, shiftModifier);
049: return hashCode;
050: }
051:
052: public boolean equals(MenuShortcut s) {
053: return (s != null) && (s.keyCode == keyCode)
054: && (s.shiftModifier == shiftModifier);
055: }
056:
057: @Override
058: public boolean equals(Object obj) {
059: if (obj instanceof MenuShortcut) {
060: MenuShortcut s = (MenuShortcut) obj;
061: return (s.keyCode == keyCode)
062: && (s.shiftModifier == shiftModifier);
063: }
064: return false;
065: }
066:
067: @Override
068: public String toString() {
069: int modifiers = InputEvent.CTRL_DOWN_MASK
070: | (shiftModifier ? InputEvent.SHIFT_DOWN_MASK : 0);
071:
072: return KeyEvent.getKeyModifiersText(modifiers)
073: + "+" + KeyEvent.getKeyText(keyCode); //$NON-NLS-1$
074: }
075:
076: public int getKey() {
077: return keyCode;
078: }
079:
080: protected String paramString() {
081: /* The format of paramString is based on 1.5 release behavior
082: * which can be revealed using the following code:
083: *
084: * MenuShortcut obj = new MenuShortcut(KeyEvent.VK_A, true);
085: * System.out.println(obj.toString());
086: */
087:
088: String str = "key=" + keyCode; //$NON-NLS-1$
089: if (shiftModifier) {
090: str += ",usesShiftModifier"; //$NON-NLS-1$
091: }
092: return str;
093: }
094:
095: public boolean usesShiftModifier() {
096: return shiftModifier;
097: }
098:
099: static MenuShortcut lookup(KeyEvent ke) {
100: if (ke.isControlDown()) {
101: return new MenuShortcut(ke.getKeyCode(), ke.isShiftDown());
102: }
103: return null;
104: }
105:
106: static boolean isShortcut(KeyEvent ke) {
107: return ke.isControlDown();
108: }
109:
110: }
|