KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > commands > KeySequenceBinding


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.commands;
13
14 import org.eclipse.ui.commands.IKeySequenceBinding;
15 import org.eclipse.ui.internal.util.Util;
16 import org.eclipse.ui.keys.KeySequence;
17
18 final class KeySequenceBinding implements IKeySequenceBinding {
19     
20     /**
21      * This is the identifier for the default context. This is used wherever
22      * some default is needed. For example, this is the context that is used
23      * for key bindings that specify no context. This is also used to select a
24      * default context in the keys preference page.
25      */

26     public static final String JavaDoc DEFAULT_CONTEXT_ID = "org.eclipse.ui.contexts.window"; //$NON-NLS-1$
27

28     private final static int HASH_FACTOR = 89;
29     private final static int HASH_INITIAL =
30         KeySequenceBinding.class.getName().hashCode();
31
32     private transient int hashCode;
33     private transient boolean hashCodeComputed;
34
35     private KeySequence keySequence;
36     private int match;
37     private transient String JavaDoc string;
38
39     KeySequenceBinding(KeySequence keySequence, int match) {
40         if (keySequence == null)
41             throw new NullPointerException JavaDoc();
42
43         if (match < 0)
44             throw new IllegalArgumentException JavaDoc();
45
46         this.keySequence = keySequence;
47         this.match = match;
48     }
49
50     public int compareTo(Object JavaDoc object) {
51         KeySequenceBinding castedObject = (KeySequenceBinding) object;
52         int compareTo = Util.compare(match, castedObject.match);
53
54         if (compareTo == 0)
55             compareTo = Util.compare(keySequence, castedObject.keySequence);
56
57         return compareTo;
58     }
59
60     public boolean equals(Object JavaDoc object) {
61         if (!(object instanceof KeySequenceBinding))
62             return false;
63
64         KeySequenceBinding castedObject = (KeySequenceBinding) object;
65         boolean equals = true;
66         equals &= Util.equals(keySequence, castedObject.keySequence);
67         equals &= Util.equals(match, castedObject.match);
68         return equals;
69     }
70
71     public KeySequence getKeySequence() {
72         return keySequence;
73     }
74
75     public int getMatch() {
76         return match;
77     }
78
79     public int hashCode() {
80         if (!hashCodeComputed) {
81             hashCode = HASH_INITIAL;
82             hashCode = hashCode * HASH_FACTOR + Util.hashCode(keySequence);
83             hashCode = hashCode * HASH_FACTOR + Util.hashCode(match);
84             hashCodeComputed = true;
85         }
86
87         return hashCode;
88     }
89
90     public String JavaDoc toString() {
91         if (string == null) {
92             final StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
93             stringBuffer.append('[');
94             stringBuffer.append(keySequence);
95             stringBuffer.append(',');
96             stringBuffer.append(match);
97             stringBuffer.append(']');
98             string = stringBuffer.toString();
99         }
100
101         return string;
102     }
103 }
104
Popular Tags