KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.ui.internal.util.Util;
21 import org.eclipse.ui.keys.KeySequence;
22
23 public final class KeySequenceBindingDefinition
24     implements Comparable JavaDoc {
25
26     private final static int HASH_FACTOR = 89;
27     private final static int HASH_INITIAL =
28         KeySequenceBindingDefinition.class.getName().hashCode();
29
30     static Map JavaDoc keySequenceBindingDefinitionsByCommandId(Collection JavaDoc keySequenceBindingDefinitions) {
31         if (keySequenceBindingDefinitions == null)
32             throw new NullPointerException JavaDoc();
33
34         Map JavaDoc map = new HashMap JavaDoc();
35         Iterator JavaDoc iterator = keySequenceBindingDefinitions.iterator();
36
37         while (iterator.hasNext()) {
38             Object JavaDoc object = iterator.next();
39             Util.assertInstance(object, KeySequenceBindingDefinition.class);
40             KeySequenceBindingDefinition keySequenceBindingDefinition =
41                 (KeySequenceBindingDefinition) object;
42             String JavaDoc commandId = keySequenceBindingDefinition.getCommandId();
43
44             if (commandId != null) {
45                 Collection JavaDoc keySequenceBindingDefinitions2 =
46                     (Collection JavaDoc) map.get(commandId);
47
48                 if (keySequenceBindingDefinitions2 == null) {
49                     keySequenceBindingDefinitions2 = new ArrayList JavaDoc();
50                     map.put(commandId, keySequenceBindingDefinitions2);
51                 }
52
53                 keySequenceBindingDefinitions2.add(
54                     keySequenceBindingDefinition);
55             }
56         }
57
58         return map;
59     }
60
61     private String JavaDoc contextId;
62     private String JavaDoc commandId;
63
64     private transient int hashCode;
65     private transient boolean hashCodeComputed;
66     private String JavaDoc keyConfigurationId;
67     private KeySequence keySequence;
68     private String JavaDoc locale;
69     private String JavaDoc platform;
70     private String JavaDoc sourceId;
71     private transient String JavaDoc string;
72
73     public KeySequenceBindingDefinition(
74         String JavaDoc contextId,
75         String JavaDoc commandId,
76         String JavaDoc keyConfigurationId,
77         KeySequence keySequence,
78         String JavaDoc locale,
79         String JavaDoc platform,
80         String JavaDoc sourceId) {
81         this.contextId = contextId;
82         this.commandId = commandId;
83         this.keyConfigurationId = keyConfigurationId;
84         this.keySequence = keySequence;
85         this.locale = locale;
86         this.platform = platform;
87         this.sourceId = sourceId;
88     }
89
90     public int compareTo(Object JavaDoc object) {
91         KeySequenceBindingDefinition castedObject =
92             (KeySequenceBindingDefinition) object;
93         int compareTo = Util.compare(contextId, castedObject.contextId);
94
95         if (compareTo == 0) {
96             compareTo = Util.compare(commandId, castedObject.commandId);
97
98             if (compareTo == 0) {
99                 compareTo =
100                     Util.compare(
101                         keyConfigurationId,
102                         castedObject.keyConfigurationId);
103
104                 if (compareTo == 0) {
105                     compareTo =
106                         Util.compare(keySequence, castedObject.keySequence);
107
108                     if (compareTo == 0) {
109                         compareTo = Util.compare(locale, castedObject.locale);
110
111                         if (compareTo == 0) {
112                             compareTo =
113                                 Util.compare(platform, castedObject.platform);
114
115                             if (compareTo == 0)
116                                 compareTo =
117                                     Util.compare(
118                                         sourceId,
119                                         castedObject.sourceId);
120                         }
121                     }
122                 }
123             }
124         }
125
126         return compareTo;
127     }
128
129     public boolean equals(Object JavaDoc object) {
130         if (!(object instanceof KeySequenceBindingDefinition))
131             return false;
132
133         KeySequenceBindingDefinition castedObject =
134             (KeySequenceBindingDefinition) object;
135         boolean equals = true;
136         equals &= Util.equals(contextId, castedObject.contextId);
137         equals &= Util.equals(commandId, castedObject.commandId);
138         equals
139             &= Util.equals(keyConfigurationId, castedObject.keyConfigurationId);
140         equals &= Util.equals(keySequence, castedObject.keySequence);
141         equals &= Util.equals(locale, castedObject.locale);
142         equals &= Util.equals(platform, castedObject.platform);
143         equals &= Util.equals(sourceId, castedObject.sourceId);
144         return equals;
145     }
146
147     public String JavaDoc getContextId() {
148         return contextId;
149     }
150
151     public String JavaDoc getCommandId() {
152         return commandId;
153     }
154
155     public String JavaDoc getKeyConfigurationId() {
156         return keyConfigurationId;
157     }
158
159     public KeySequence getKeySequence() {
160         return keySequence;
161     }
162
163     public String JavaDoc getLocale() {
164         return locale;
165     }
166
167     public String JavaDoc getPlatform() {
168         return platform;
169     }
170
171     public String JavaDoc getSourceId() {
172         return sourceId;
173     }
174
175     public int hashCode() {
176         if (!hashCodeComputed) {
177             hashCode = HASH_INITIAL;
178             hashCode = hashCode * HASH_FACTOR + Util.hashCode(contextId);
179             hashCode = hashCode * HASH_FACTOR + Util.hashCode(commandId);
180             hashCode =
181                 hashCode * HASH_FACTOR + Util.hashCode(keyConfigurationId);
182             hashCode = hashCode * HASH_FACTOR + Util.hashCode(keySequence);
183             hashCode = hashCode * HASH_FACTOR + Util.hashCode(locale);
184             hashCode = hashCode * HASH_FACTOR + Util.hashCode(platform);
185             hashCode = hashCode * HASH_FACTOR + Util.hashCode(sourceId);
186             hashCodeComputed = true;
187         }
188
189         return hashCode;
190     }
191
192     public String JavaDoc toString() {
193         if (string == null) {
194             final StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
195             stringBuffer.append('[');
196             stringBuffer.append(contextId);
197             stringBuffer.append(',');
198             stringBuffer.append(commandId);
199             stringBuffer.append(',');
200             stringBuffer.append(keyConfigurationId);
201             stringBuffer.append(',');
202             stringBuffer.append(keySequence);
203             stringBuffer.append(',');
204             stringBuffer.append(locale);
205             stringBuffer.append(',');
206             stringBuffer.append(platform);
207             stringBuffer.append(',');
208             stringBuffer.append(sourceId);
209             stringBuffer.append(']');
210             string = stringBuffer.toString();
211         }
212
213         return string;
214     }
215 }
216
Popular Tags