1 19 20 package org.netbeans.modules.options.keymap; 21 22 import java.util.Collection ; 23 import java.util.Collections ; 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import java.util.logging.Level ; 31 import java.util.logging.Logger ; 32 33 37 public class KeymapModel { 38 39 private static final Logger LOG = Logger.getLogger(KeymapModel.class.getName ()); 40 41 private LayersBridge layersBridge = new LayersBridge (); 42 private EditorBridge editorBridge = new EditorBridge (); 43 44 46 public Set getActionCategories () { 47 Set result = new HashSet (); 48 result.addAll (layersBridge.getActions ().keySet ()); 49 result.addAll (editorBridge.getActions ().keySet ()); 50 return Collections.unmodifiableSet (result); 51 } 52 53 54 private Map categoryToActions = new HashMap (); 55 56 59 public Set getActions (String category) { 60 if (!categoryToActions.containsKey (category)) { 61 Set actions = new HashSet (); 62 Set s = (Set ) layersBridge.getActions ().get (category); 63 if (s != null) actions.addAll (s); 64 s = (Set ) editorBridge.getActions ().get (category); 65 if (s != null) 66 actions = mergeActions (s, actions); 67 categoryToActions.put (category, actions); 68 } 69 return (Set ) categoryToActions.get (category); 70 } 71 72 75 public void refreshActions () { 76 categoryToActions = new HashMap (); 77 editorBridge.refreshActions (); 78 } 79 80 82 public String getCurrentProfile () { 83 return editorBridge.getCurrentProfile (); 84 } 85 86 public void setCurrentProfile (String profile) { 87 editorBridge.setCurrentProfile (profile); 89 } 90 91 public List getProfiles () { 92 return layersBridge.getProfiles (); 93 } 94 95 public boolean isCustomProfile (String profile) { 96 return editorBridge.isCustomProfile (profile); 97 } 98 99 100 private Map keyMaps = new HashMap (); 101 102 105 public Map getKeymap (String profile) { 106 if (!keyMaps.containsKey (profile)) { 107 keyMaps.put ( 108 profile, 109 mergeShortcuts ( 110 (Map ) editorBridge.readKeymap (profile), 111 (Map ) layersBridge.getKeymap (profile) 112 ) 113 ); 114 } 115 return (Map ) keyMaps.get (profile); 116 } 117 118 119 private Map keyMapDefaults = new HashMap (); 120 121 124 public Map getKeymapDefaults (String profile) { 125 if (!keyMapDefaults.containsKey (profile)) { 126 keyMapDefaults.put ( 127 profile, 128 mergeShortcuts ( 129 (Map ) editorBridge.readKeymapDefaults(profile), 130 (Map ) layersBridge.getKeymapDefaults(profile) 131 ) 132 ); 133 } 134 return (Map ) keyMapDefaults.get (profile); 135 } 136 137 public void deleteProfile (String profile) { 138 layersBridge.deleteProfile (profile); 139 editorBridge.deleteProfile (profile); 140 } 141 142 146 public void changeKeymap (String profile, Map actionToShortcuts) { 147 log ("changeKeymap.actionToShortcuts", actionToShortcuts.entrySet ()); 148 149 Map m = new HashMap (getKeymap (profile)); 151 m.putAll (actionToShortcuts); 152 keyMaps.put (profile, m); 153 log ("changeKeymap.m", m.entrySet ()); 154 155 layersBridge.saveKeymap (profile, m); 156 editorBridge.saveKeymap (profile, m); 157 } 158 159 160 162 private void log(String name, Collection items) { 163 if (!LOG.isLoggable(Level.FINE)) return; 164 165 LOG.fine(name); 166 for(Iterator i = items.iterator(); i.hasNext(); ) { 167 Object item = i.next(); 168 LOG.fine(" " + item); 169 } 170 } 171 172 private Map sharedActions = new HashMap (); 173 174 178 private Set mergeActions ( 179 Collection editorActions, 180 Collection layersActions 181 ) { 182 Set result = new HashSet (); 183 Map idToAction = new HashMap (); 184 if (editorActions != null) { 185 Iterator it = editorActions.iterator (); 186 while (it.hasNext ()) { 187 ActionImpl action = (ActionImpl) it.next (); 188 String id = action.getDelegatingActionId (); 189 if (id != null) 190 idToAction.put (id, action); 191 else 192 result.add (action); 193 } 194 } 195 196 if (layersActions != null) { 197 Iterator it = layersActions.iterator (); 198 while (it.hasNext ()) { 199 ActionImpl layersAction = (ActionImpl) it.next (); 200 String id = layersAction.getId (); 201 if (!idToAction.containsKey (id)) 202 result.add (layersAction); 203 else { 204 ActionImpl editorAction = (ActionImpl) idToAction. 205 remove (id); 206 CompoundAction compoundAction = new CompoundAction ( 207 editorAction, layersAction ); 210 result.add (compoundAction); 211 sharedActions.put (editorAction, compoundAction); 212 sharedActions.put (layersAction, compoundAction); 213 } 214 } 215 } 216 217 result.addAll (idToAction.values ()); 218 219 return result; 220 } 221 222 226 private Map mergeShortcuts ( 227 Map editorActions, 228 Map layersActions 229 ) { 230 Map result = new HashMap (); 231 Iterator it = editorActions.keySet ().iterator (); 232 while (it.hasNext ()) { 233 ActionImpl action = (ActionImpl) it.next (); 234 Set shortcuts = (Set ) editorActions.get (action); 235 if (sharedActions.containsKey (action)) 236 action = (CompoundAction) sharedActions.get (action); 237 result.put (action, shortcuts); 238 } 239 it = layersActions.keySet ().iterator (); 240 while (it.hasNext ()) { 241 ActionImpl action = (ActionImpl) it.next (); 242 Set shortcuts = (Set ) layersActions.get (action); 243 if (sharedActions.containsKey (action)) 244 action = (CompoundAction) sharedActions.get (action); 245 result.put (action, shortcuts); 246 } 247 return result; 248 } 249 250 { 251 Iterator it = getActionCategories ().iterator (); 254 while (it.hasNext ()) 255 getActions ((String ) it.next ()); 256 } 257 } 258 | Popular Tags |