1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
2 | // |
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | // you may not use this file except in compliance with the License. |
---|
5 | // You may obtain a copy of the License at |
---|
6 | // |
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | // |
---|
9 | // Unless required by applicable law or agreed to in writing, software |
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | // See the License for the specific language governing permissions and |
---|
13 | // limitations under the License. |
---|
14 | |
---|
15 | package de.ugoe.cs.autoquest.keyboardmaps; |
---|
16 | |
---|
17 | import java.util.ArrayList; |
---|
18 | import java.util.HashMap; |
---|
19 | import java.util.List; |
---|
20 | import java.util.Map; |
---|
21 | import java.util.logging.Level; |
---|
22 | |
---|
23 | import de.ugoe.cs.util.console.Console; |
---|
24 | |
---|
25 | /** |
---|
26 | * <p> |
---|
27 | * Helper class to handle synonymous {@link VirtualKey}s. |
---|
28 | * </p> |
---|
29 | * |
---|
30 | * @version 1.0 |
---|
31 | * @author Patrick Harms |
---|
32 | */ |
---|
33 | class VirtualKeySynonyms { |
---|
34 | |
---|
35 | /** |
---|
36 | * <p> |
---|
37 | * Map of synonymous keys. |
---|
38 | * </p> |
---|
39 | */ |
---|
40 | private Map<Integer, List<VirtualKey>> synonyms = new HashMap<Integer, List<VirtualKey>>(); |
---|
41 | |
---|
42 | /** |
---|
43 | * <p> |
---|
44 | * Mapping of {@link VirtualKey}s to integer Ids. |
---|
45 | * </p> |
---|
46 | */ |
---|
47 | private Map<VirtualKey, Integer> keyIds = new HashMap<VirtualKey, Integer>(); |
---|
48 | |
---|
49 | /** |
---|
50 | * <p> |
---|
51 | * Adds a new synonymous key. |
---|
52 | * </p> |
---|
53 | * |
---|
54 | * @param keyId |
---|
55 | * id of the synonym |
---|
56 | * @param virtualKey |
---|
57 | * the synonym |
---|
58 | */ |
---|
59 | public void add(int keyId, VirtualKey virtualKey) { |
---|
60 | List<VirtualKey> synonymList = synonyms.get(keyId); |
---|
61 | |
---|
62 | if (synonymList == null) { |
---|
63 | synonymList = new ArrayList<VirtualKey>(); |
---|
64 | synonyms.put(keyId, synonymList); |
---|
65 | } |
---|
66 | |
---|
67 | if (!synonymList.contains(virtualKey)) { |
---|
68 | // ensure that the latest determined virtual keys are considered first |
---|
69 | synonymList.add(0, virtualKey); |
---|
70 | } |
---|
71 | |
---|
72 | Integer existingKeyId = keyIds.get(virtualKey); |
---|
73 | |
---|
74 | if ((existingKeyId != null) && (existingKeyId != keyId)) { |
---|
75 | Console.traceln(Level.FINEST, "virtual key " + virtualKey + " is mapped to more " + |
---|
76 | "than one key id (current is " + existingKeyId + ", new is " + keyId + |
---|
77 | "). New key id will be used (" + keyId + ")."); |
---|
78 | } |
---|
79 | |
---|
80 | keyIds.put(virtualKey, keyId); |
---|
81 | } |
---|
82 | |
---|
83 | /** |
---|
84 | * <p> |
---|
85 | * Returns whether a key is contained in the set of synonyms. |
---|
86 | * </p> |
---|
87 | * |
---|
88 | * @param keyId |
---|
89 | * id of the key |
---|
90 | * @return true, if the key is contained; false otherwise |
---|
91 | */ |
---|
92 | public boolean containsKey(int keyId) { |
---|
93 | return synonyms.containsKey(keyId); |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * <p> |
---|
98 | * Returns all synonyms known for a given key. |
---|
99 | * </p> |
---|
100 | * |
---|
101 | * @param keyId |
---|
102 | * the id of the key |
---|
103 | * @return the synonyms |
---|
104 | */ |
---|
105 | public VirtualKey[] getVirtualKeySynonyms(int keyId) { |
---|
106 | List<VirtualKey> virtualKeys = synonyms.get(keyId); |
---|
107 | if (virtualKeys != null) { |
---|
108 | return virtualKeys.toArray(new VirtualKey[virtualKeys.size()]); |
---|
109 | } |
---|
110 | else { |
---|
111 | Console.traceln(Level.WARNING, "no virtual key define for key id " + keyId); |
---|
112 | return null; |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | } |
---|