KickJava   Java API By Example, From Geeks To Geeks.

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


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

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