|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 package com.nokia.carbide.cpp.internal.pi.button.ui; |
|
19 |
|
20 import java.util.Set; |
|
21 import java.util.TreeMap; |
|
22 import java.util.Map.Entry; |
|
23 |
|
24 import org.eclipse.emf.common.util.EList; |
|
25 |
|
26 import com.nokia.carbide.cpp.internal.pi.util.config.gen.PIConfig.ButtonEventProfileType; |
|
27 import com.nokia.carbide.cpp.internal.pi.util.config.gen.PIConfig.MappingType; |
|
28 import com.nokia.carbide.cpp.internal.pi.util.config.gen.PIConfig.PIConfigFactory; |
|
29 import com.nokia.carbide.cpp.pi.button.IBupEventMap; |
|
30 import com.nokia.carbide.cpp.pi.button.IBupEventMapEditable; |
|
31 import com.nokia.carbide.cpp.pi.button.IBupEventMapProfile; |
|
32 |
|
33 /** |
|
34 * |
|
35 * A class that cache IBupEventMap, handles modifications and commits update |
|
36 * |
|
37 */ |
|
38 public class ModifyCachedBupEventMap implements IBupEventMap, IBupEventMapEditable { |
|
39 |
|
40 private IBupEventMap srcMap; |
|
41 private boolean modified; |
|
42 class TableEntry { |
|
43 String enumString; |
|
44 String label; |
|
45 boolean modified = false; |
|
46 }; |
|
47 private TreeMap<Integer, TableEntry> cachedMap = new TreeMap<Integer, TableEntry>(); |
|
48 |
|
49 public ModifyCachedBupEventMap(IBupEventMap map) { |
|
50 srcMap = map; |
|
51 initializeFromMap(srcMap); |
|
52 } |
|
53 |
|
54 public void initializeFromMap(IBupEventMap map) { |
|
55 modified = !map.equals(srcMap); |
|
56 cachedMap.clear(); |
|
57 Set<Integer> keyCodeSet = map.getKeyCodeSet(); |
|
58 for (Integer keyCode : keyCodeSet) { |
|
59 TableEntry entry = new TableEntry(); |
|
60 entry.enumString = map.getEnum(keyCode); |
|
61 entry.label = map.getLabel(keyCode); |
|
62 cachedMap.put(keyCode, entry); |
|
63 } |
|
64 } |
|
65 |
|
66 public void commitChanges() { |
|
67 if (haveUncommitedChanges()) { |
|
68 // reset the map to blank |
|
69 Integer [] keyCodeSet = srcMap.getKeyCodeSet().toArray(new Integer[srcMap.getKeyCodeSet().size()]); |
|
70 for (Integer keyCode : keyCodeSet) { |
|
71 srcMap.removeMapping(keyCode); |
|
72 } |
|
73 // replace with all new mapping |
|
74 Set<Entry<Integer, TableEntry>> entrySet = cachedMap.entrySet(); |
|
75 for (Entry<Integer, TableEntry> mapEntry : entrySet) { |
|
76 srcMap.addMapping(mapEntry.getKey(), mapEntry.getValue().enumString, mapEntry.getValue().label); |
|
77 } |
|
78 } |
|
79 initializeFromMap(srcMap); // now we are done, reload as if freshly instantiated |
|
80 } |
|
81 |
|
82 public Set<Integer> getKeyCodeSet() { |
|
83 return cachedMap.keySet(); |
|
84 } |
|
85 |
|
86 public String getLabel(int keyCode) { |
|
87 TableEntry entry = cachedMap.get(keyCode); |
|
88 |
|
89 if (entry == null) { |
|
90 return "" + keyCode; // default is just the decimal value string //$NON-NLS-1$ |
|
91 } |
|
92 |
|
93 return entry.label; |
|
94 } |
|
95 |
|
96 public String getEnum(int keyCode) { |
|
97 TableEntry entry = cachedMap.get(keyCode); |
|
98 |
|
99 if (entry == null) { |
|
100 return ""; //$NON-NLS-1$ |
|
101 } |
|
102 |
|
103 return entry.enumString; |
|
104 } |
|
105 |
|
106 public boolean isModified(int keyCode) { |
|
107 TableEntry cachedEntry = cachedMap.get(keyCode); |
|
108 |
|
109 if (cachedEntry == null) { |
|
110 if (srcMap.getKeyCodeSet().contains(keyCode)) { |
|
111 return true; |
|
112 } |
|
113 return false; |
|
114 } |
|
115 return cachedEntry.modified; |
|
116 } |
|
117 |
|
118 |
|
119 public boolean haveUncommitedChanges() { |
|
120 return modified; |
|
121 } |
|
122 |
|
123 public boolean flagAsModified(int keyCode) { |
|
124 boolean inCachedMap = cachedMap.keySet().contains(keyCode); |
|
125 boolean inSrcMap = srcMap.getKeyCodeSet().contains(keyCode); |
|
126 |
|
127 if (inCachedMap != inSrcMap) { |
|
128 return true; |
|
129 } |
|
130 |
|
131 if (inCachedMap == false) { |
|
132 return false; // both doesn't exist |
|
133 } |
|
134 |
|
135 TableEntry cachedEntry = cachedMap.get(keyCode); |
|
136 return (!cachedEntry.enumString.equals(srcMap.getEnum(keyCode)) || |
|
137 !cachedEntry.label.equals(srcMap.getLabel(keyCode))); |
|
138 } |
|
139 |
|
140 public void addMapping(int keyCode, String enumString, String label) { |
|
141 modified = true; |
|
142 TableEntry entry = cachedMap.get(keyCode); |
|
143 if (entry == null) { |
|
144 entry = new TableEntry(); |
|
145 cachedMap.put(new Integer(keyCode), entry); |
|
146 } |
|
147 entry.enumString = enumString; |
|
148 entry.label = label; |
|
149 entry.modified = true; |
|
150 } |
|
151 |
|
152 public void removeMapping(int keyCode) { |
|
153 modified = true; |
|
154 cachedMap.remove(keyCode); |
|
155 } |
|
156 |
|
157 /* (non-Javadoc) |
|
158 * @see com.nokia.carbide.cpp.pi.button.IBupEventMap#getProfile() |
|
159 */ |
|
160 public IBupEventMapProfile getProfile() { |
|
161 return srcMap.getProfile(); |
|
162 } |
|
163 |
|
164 /* (non-Javadoc) |
|
165 * @see com.nokia.carbide.cpp.pi.button.IBupEventMap#toEmfModel() |
|
166 */ |
|
167 public ButtonEventProfileType toEmfModel() { |
|
168 ButtonEventProfileType profile = PIConfigFactory.eINSTANCE.createButtonEventProfileType(); |
|
169 |
|
170 profile.setProfileId(getProfile().getProfileId()); |
|
171 EList<MappingType> mappingList = profile.getMapping(); |
|
172 for (Entry<Integer, TableEntry> entry : cachedMap.entrySet()) { |
|
173 MappingType mappingType = PIConfigFactory.eINSTANCE.createMappingType(); |
|
174 mappingType.setKeyCode(entry.getKey().longValue()); |
|
175 mappingType.setEnumString(entry.getValue().enumString); |
|
176 mappingType.setLabel(entry.getValue().label); |
|
177 mappingList.add(mappingType); |
|
178 } |
|
179 |
|
180 return profile; |
|
181 } |
|
182 } |