001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.keys;
011:
012: import org.eclipse.ui.commands.IKeySequenceBinding;
013: import org.eclipse.ui.internal.util.Util;
014: import org.eclipse.ui.keys.KeySequence;
015:
016: public final class KeySequenceBinding implements IKeySequenceBinding {
017:
018: /**
019: * This is the identifier for the default context. This is used wherever
020: * some default is needed. For example, this is the context that is used
021: * for key bindings that specify no context. This is also used to select a
022: * default context in the keys preference page.
023: */
024: public static final String DEFAULT_CONTEXT_ID = "org.eclipse.ui.contexts.window"; //$NON-NLS-1$
025:
026: private final static int HASH_FACTOR = 89;
027:
028: private final static int HASH_INITIAL = KeySequenceBinding.class
029: .getName().hashCode();
030:
031: private transient int hashCode;
032:
033: private transient boolean hashCodeComputed;
034:
035: private KeySequence keySequence;
036:
037: private int match;
038:
039: private transient String string;
040:
041: public KeySequenceBinding(KeySequence keySequence, int match) {
042: if (keySequence == null) {
043: throw new NullPointerException();
044: }
045:
046: if (match < 0) {
047: throw new IllegalArgumentException();
048: }
049:
050: this .keySequence = keySequence;
051: this .match = match;
052: }
053:
054: public int compareTo(Object object) {
055: KeySequenceBinding castedObject = (KeySequenceBinding) object;
056: int compareTo = Util.compare(match, castedObject.match);
057:
058: if (compareTo == 0) {
059: compareTo = Util.compare(keySequence,
060: castedObject.keySequence);
061: }
062:
063: return compareTo;
064: }
065:
066: public boolean equals(Object object) {
067: if (!(object instanceof KeySequenceBinding)) {
068: return false;
069: }
070:
071: final KeySequenceBinding castedObject = (KeySequenceBinding) object;
072: if (!Util.equals(keySequence, castedObject.keySequence)) {
073: return false;
074: }
075:
076: return Util.equals(match, castedObject.match);
077: }
078:
079: public KeySequence getKeySequence() {
080: return keySequence;
081: }
082:
083: public int getMatch() {
084: return match;
085: }
086:
087: public int hashCode() {
088: if (!hashCodeComputed) {
089: hashCode = HASH_INITIAL;
090: hashCode = hashCode * HASH_FACTOR
091: + Util.hashCode(keySequence);
092: hashCode = hashCode * HASH_FACTOR + Util.hashCode(match);
093: hashCodeComputed = true;
094: }
095:
096: return hashCode;
097: }
098:
099: public String toString() {
100: if (string == null) {
101: final StringBuffer stringBuffer = new StringBuffer();
102: stringBuffer.append('[');
103: stringBuffer.append(keySequence);
104: stringBuffer.append(',');
105: stringBuffer.append(match);
106: stringBuffer.append(']');
107: string = stringBuffer.toString();
108: }
109:
110: return string;
111: }
112: }
|