KickJava   Java API By Example, From Geeks To Geeks.

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


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.Arrays JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.SortedMap JavaDoc;
22 import java.util.TreeMap JavaDoc;
23
24 import org.eclipse.ui.internal.util.Util;
25 import org.eclipse.ui.keys.KeySequence;
26
27 final class KeySequenceBindingMachine {
28
29     /**
30      * This is a map of the active context identifiers, to their parents. If the
31      * context does not have a parent, then the mapping will be to
32      * <code>null</code>. Both the keys and the values are strings.
33      */

34     private Map JavaDoc activeContextIdMap;
35     private String JavaDoc[] activeKeyConfigurationIds;
36     private String JavaDoc[] activeLocales;
37     private String JavaDoc[] activePlatforms;
38     private List JavaDoc[] keySequenceBindings;
39     private Map JavaDoc keySequenceBindingsByCommandId;
40     private Map JavaDoc matchesByKeySequence;
41     private boolean solved;
42     private SortedMap JavaDoc tree;
43
44     KeySequenceBindingMachine() {
45         activeContextIdMap = new HashMap JavaDoc();
46         activeKeyConfigurationIds = new String JavaDoc[0];
47         activeLocales = new String JavaDoc[0];
48         activePlatforms = new String JavaDoc[0];
49         keySequenceBindings = new List JavaDoc[] { new ArrayList JavaDoc(), new ArrayList JavaDoc()};
50     }
51
52     Map JavaDoc getActiveContextIds() {
53         return activeContextIdMap;
54     }
55
56     String JavaDoc[] getActiveKeyConfigurationIds() {
57         return (String JavaDoc[]) activeKeyConfigurationIds.clone();
58     }
59
60     String JavaDoc[] getActiveLocales() {
61         return (String JavaDoc[]) activeLocales.clone();
62     }
63
64     String JavaDoc[] getActivePlatforms() {
65         return (String JavaDoc[]) activePlatforms.clone();
66     }
67
68     List JavaDoc getKeySequenceBindings0() {
69         return keySequenceBindings[0];
70     }
71
72     List JavaDoc getKeySequenceBindings1() {
73         return keySequenceBindings[1];
74     }
75
76     Map JavaDoc getKeySequenceBindingsByCommandId() {
77         if (keySequenceBindingsByCommandId == null) {
78             validateSolution();
79             keySequenceBindingsByCommandId =
80                 Collections.unmodifiableMap(
81                     KeySequenceBindingNode.getKeySequenceBindingsByCommandId(
82                         getMatchesByKeySequence()));
83         }
84
85         return keySequenceBindingsByCommandId;
86     }
87
88     /**
89      * Gets all of the active key bindings as a map of <code>KeySequence</code>
90      * to command identifiers (<code>String</code>). The map returned is not
91      * a copy, and is not safe to modify. <em>Do not modify the return
92      * value</em>.
93      *
94      * @return The active key bindings; never <code>null,/code>. <em>Do not
95      * modify the return value</em>.
96      */

97     Map JavaDoc getMatchesByKeySequence() {
98         if (matchesByKeySequence == null) {
99             validateSolution();
100             matchesByKeySequence =
101                 KeySequenceBindingNode.getMatchesByKeySequence(
102                     tree,
103                     KeySequence.getInstance());
104         }
105
106         return matchesByKeySequence;
107     }
108
109     private void invalidateSolution() {
110         solved = false;
111         keySequenceBindingsByCommandId = null;
112         matchesByKeySequence = null;
113     }
114
115     private void invalidateTree() {
116         tree = null;
117         invalidateSolution();
118     }
119
120     /**
121      * A mutator for the tree of active contexts. If the tree is different then
122      * the current tree, then the previous key binding map is invalidated.
123      *
124      * @param activeContextTree
125      * The map of child to parent context identifiers that are
126      * currently active. This value must not be <code>null</code>,
127      * but may be empty. All the keys and values in the map should be
128      * strings, and only the values can be <code>null</code>.
129      * @return <code>true</code> if the context tree has changed;
130      * <code>false</code> otherwise.
131      */

132     boolean setActiveContextIds(Map JavaDoc activeContextTree) {
133         if (activeContextTree == null)
134             throw new NullPointerException JavaDoc();
135         
136         if (!activeContextTree.equals(this.activeContextIdMap)) {
137             this.activeContextIdMap = activeContextTree;
138             invalidateSolution();
139             return true;
140         }
141
142         return false;
143     }
144
145     boolean setActiveKeyConfigurationIds(String JavaDoc[] activeKeyConfigurationIds) {
146         if (activeKeyConfigurationIds == null)
147             throw new NullPointerException JavaDoc();
148
149         activeKeyConfigurationIds =
150             (String JavaDoc[]) activeKeyConfigurationIds.clone();
151
152         if (!Arrays
153             .equals(
154                 this.activeKeyConfigurationIds,
155                 activeKeyConfigurationIds)) {
156             this.activeKeyConfigurationIds = activeKeyConfigurationIds;
157             invalidateSolution();
158             return true;
159         }
160
161         return false;
162     }
163
164     boolean setActiveLocales(String JavaDoc[] activeLocales) {
165         if (activeLocales == null)
166             throw new NullPointerException JavaDoc();
167
168         activeLocales = (String JavaDoc[]) activeLocales.clone();
169
170         if (!Arrays.equals(this.activeLocales, activeLocales)) {
171             this.activeLocales = activeLocales;
172             invalidateSolution();
173             return true;
174         }
175
176         return false;
177     }
178
179     boolean setActivePlatforms(String JavaDoc[] activePlatforms) {
180         if (activePlatforms == null)
181             throw new NullPointerException JavaDoc();
182
183         activePlatforms = (String JavaDoc[]) activePlatforms.clone();
184
185         if (!Arrays.equals(this.activePlatforms, activePlatforms)) {
186             this.activePlatforms = activePlatforms;
187             invalidateSolution();
188             return true;
189         }
190
191         return false;
192     }
193
194     boolean setKeySequenceBindings0(List JavaDoc keySequenceBindings0) {
195         keySequenceBindings0 =
196             Util.safeCopy(
197                 keySequenceBindings0,
198                 KeySequenceBindingDefinition.class);
199
200         if (!this.keySequenceBindings[0].equals(keySequenceBindings0)) {
201             this.keySequenceBindings[0] = keySequenceBindings0;
202             invalidateTree();
203             return true;
204         }
205
206         return false;
207     }
208
209     boolean setKeySequenceBindings1(List JavaDoc keySequenceBindings1) {
210         keySequenceBindings1 =
211             Util.safeCopy(
212                 keySequenceBindings1,
213                 KeySequenceBindingDefinition.class);
214
215         if (!this.keySequenceBindings[1].equals(keySequenceBindings1)) {
216             this.keySequenceBindings[1] = keySequenceBindings1;
217             invalidateTree();
218             return true;
219         }
220
221         return false;
222     }
223
224     private void validateSolution() {
225         if (!solved) {
226             validateTree();
227             KeySequenceBindingNode.solve(
228                 tree,
229                 activeContextIdMap,
230                 activeKeyConfigurationIds,
231                 activePlatforms,
232                 activeLocales);
233             solved = true;
234         }
235     }
236
237     private void validateTree() {
238         if (tree == null) {
239             tree = new TreeMap JavaDoc();
240
241             for (int i = 0; i < keySequenceBindings.length; i++) {
242                 Iterator JavaDoc iterator = keySequenceBindings[i].iterator();
243
244                 while (iterator.hasNext()) {
245                     KeySequenceBindingDefinition keySequenceBindingDefinition =
246                         (KeySequenceBindingDefinition) iterator.next();
247                     KeySequenceBindingNode.add(
248                         tree,
249                         keySequenceBindingDefinition.getKeySequence(),
250                         keySequenceBindingDefinition.getContextId(),
251                         keySequenceBindingDefinition.getKeyConfigurationId(),
252                         i,
253                         keySequenceBindingDefinition.getPlatform(),
254                         keySequenceBindingDefinition.getLocale(),
255                         keySequenceBindingDefinition.getCommandId());
256                 }
257             }
258         }
259     }
260 }
261
Popular Tags