1 11 12 package org.eclipse.ui.internal.commands; 13 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.Collections ; 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Map ; 21 import java.util.SortedMap ; 22 import java.util.TreeMap ; 23 24 import org.eclipse.ui.internal.util.Util; 25 import org.eclipse.ui.keys.KeySequence; 26 27 final class KeySequenceBindingMachine { 28 29 34 private Map activeContextIdMap; 35 private String [] activeKeyConfigurationIds; 36 private String [] activeLocales; 37 private String [] activePlatforms; 38 private List [] keySequenceBindings; 39 private Map keySequenceBindingsByCommandId; 40 private Map matchesByKeySequence; 41 private boolean solved; 42 private SortedMap tree; 43 44 KeySequenceBindingMachine() { 45 activeContextIdMap = new HashMap (); 46 activeKeyConfigurationIds = new String [0]; 47 activeLocales = new String [0]; 48 activePlatforms = new String [0]; 49 keySequenceBindings = new List [] { new ArrayList (), new ArrayList ()}; 50 } 51 52 Map getActiveContextIds() { 53 return activeContextIdMap; 54 } 55 56 String [] getActiveKeyConfigurationIds() { 57 return (String []) activeKeyConfigurationIds.clone(); 58 } 59 60 String [] getActiveLocales() { 61 return (String []) activeLocales.clone(); 62 } 63 64 String [] getActivePlatforms() { 65 return (String []) activePlatforms.clone(); 66 } 67 68 List getKeySequenceBindings0() { 69 return keySequenceBindings[0]; 70 } 71 72 List getKeySequenceBindings1() { 73 return keySequenceBindings[1]; 74 } 75 76 Map 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 97 Map 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 132 boolean setActiveContextIds(Map activeContextTree) { 133 if (activeContextTree == null) 134 throw new NullPointerException (); 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 [] activeKeyConfigurationIds) { 146 if (activeKeyConfigurationIds == null) 147 throw new NullPointerException (); 148 149 activeKeyConfigurationIds = 150 (String []) 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 [] activeLocales) { 165 if (activeLocales == null) 166 throw new NullPointerException (); 167 168 activeLocales = (String []) 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 [] activePlatforms) { 180 if (activePlatforms == null) 181 throw new NullPointerException (); 182 183 activePlatforms = (String []) 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 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 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 (); 240 241 for (int i = 0; i < keySequenceBindings.length; i++) { 242 Iterator 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 |