1 19 20 package org.netbeans.modules.options.keymap; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.Enumeration ; 26 import java.util.HashMap ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.MissingResourceException ; 32 import java.util.Set ; 33 import java.util.StringTokenizer ; 34 import javax.swing.Action ; 35 import org.openide.ErrorManager; 36 37 import org.openide.cookies.InstanceCookie; 38 import org.openide.filesystems.FileObject; 39 import org.openide.filesystems.FileSystem; 40 import org.openide.filesystems.Repository; 41 import org.openide.loaders.DataFolder; 42 import org.openide.loaders.DataObject; 43 import org.openide.loaders.DataShadow; 44 import org.openide.util.NbBundle; 45 import org.openide.util.actions.Presenter; 46 47 52 public class LayersBridge { 53 54 static final String KEYMAPS_FOLDER = "Keymaps"; 55 private static final String SHORTCUTS_FOLDER = "Shortcuts"; 56 private static final String TOOLBARS_FOLDER = "Toolbars"; 57 58 59 private Map actionToDataObject = new HashMap (); 60 61 private Map categoryToActions; 62 63 private Set actions = new HashSet (); 64 65 66 69 Map getActions () { 70 if (categoryToActions == null) { 71 categoryToActions = new HashMap (); 72 initActions ("OptionsDialog/Actions", null); initActions ( 74 "Actions", 75 NbBundle.getMessage (LayersBridge.class, "CTL_Other") 76 ); 77 categoryToActions.remove ("Hidden"); categoryToActions = Collections.unmodifiableMap (categoryToActions); 79 } 80 return categoryToActions; 81 } 82 83 private void initActions (String folder, String category) { 84 FileSystem fs = Repository.getDefault ().getDefaultFileSystem (); 85 FileObject fo = fs.findResource (folder); 86 if (fo == null) return; 87 DataFolder root = DataFolder.findFolder (fo); 88 Enumeration en = root.children (); 89 while (en.hasMoreElements ()) { 90 DataObject dataObject = (DataObject) en.nextElement (); 91 if (dataObject instanceof DataFolder) 92 initActions ((DataFolder) dataObject, null, category); 93 } 94 } 95 96 private void initActions ( 97 DataFolder folder, 98 String folderName, 99 String category 100 ) { 101 102 String name = folder.getName (); 104 if (category != null) 105 name = category; 106 else { 107 String bundleName = (String ) folder.getPrimaryFile ().getAttribute 108 ("SystemFileSystem.localizingBundle"); 109 if (bundleName != null) 110 try { 111 name = NbBundle.getBundle (bundleName).getString ( 112 folder.getPrimaryFile ().getPath () 113 ); 114 } catch (MissingResourceException ex) { 115 ErrorManager.getDefault ().notify (ex); 116 } 117 if (folderName != null) 118 name = folderName + '/' + name; 119 } 120 121 Enumeration en = folder.children (); 122 while (en.hasMoreElements ()) { 123 DataObject dataObject = (DataObject) en.nextElement (); 124 if (dataObject instanceof DataFolder) { 125 initActions ((DataFolder) dataObject, name, category); 126 continue; 127 } 128 GlobalAction action = createAction (dataObject); 129 if (actions.contains (action)) continue; 130 if (action == null) continue; 131 actions.add (action); 132 133 Set a = (Set ) categoryToActions.get (name); 135 if (a == null) { 136 a = new HashSet (); 137 categoryToActions.put (name, a); 138 } 139 a.add (action); 140 141 while (dataObject instanceof DataShadow) 142 dataObject = ((DataShadow) dataObject).getOriginal (); 143 144 actionToDataObject.put (action, dataObject); 145 } 146 } 147 148 private List keymapNames; 149 150 List getProfiles () { 151 if (keymapNames == null) { 152 DataFolder root = getRootFolder (KEYMAPS_FOLDER, null); 153 Enumeration en = root.children (false); 154 keymapNames = new ArrayList (); 155 while (en.hasMoreElements ()) { 156 DataObject dataObject = (DataObject) en.nextElement (); 157 if (!(dataObject instanceof DataFolder)) continue; 158 keymapNames.add (dataObject.getName ()); 159 } 160 if (!keymapNames.contains ("NetBeans")) 161 keymapNames.add ("NetBeans"); 162 } 163 return Collections.unmodifiableList (keymapNames); 164 } 165 166 167 private Map keymaps = new HashMap (); 168 169 172 Map getKeymap (String profile) { 173 if (!keymaps.containsKey (profile)) { 174 DataFolder root = getRootFolder (SHORTCUTS_FOLDER, null); 175 Map m = readKeymap (root); 176 root = getRootFolder (KEYMAPS_FOLDER, profile); 177 m.putAll (readKeymap (root)); 178 keymaps.put (profile, m); 179 } 180 return Collections.unmodifiableMap ((Map ) keymaps.get (profile)); 181 } 182 183 184 private Map keymapDefaults = new HashMap (); 185 186 189 Map getKeymapDefaults (String profile) { 190 if (!keymapDefaults.containsKey (profile)) { 191 DataFolder root = getRootFolder (SHORTCUTS_FOLDER, null); 192 Map m = readKeymap (root); 193 root = getRootFolder (KEYMAPS_FOLDER, profile); 194 m.putAll (readKeymap (root)); 195 keymapDefaults.put (profile, m); 196 } 197 return Collections.unmodifiableMap ((Map ) keymapDefaults.get (profile)); 198 } 199 200 DataObject getDataObject (Object action) { 201 return (DataObject) actionToDataObject.get (action); 202 } 203 204 207 private Map readKeymap (DataFolder root) { 208 Map keymap = new HashMap (); 209 if (root == null) return keymap; 210 Enumeration en = root.children (false); 211 while (en.hasMoreElements ()) { 212 DataObject dataObject = (DataObject) en.nextElement (); 213 if (dataObject instanceof DataFolder) continue; 214 GlobalAction action = createAction (dataObject); 215 if (action == null) continue; 216 String shortcut = dataObject.getName (); 217 Set s = (Set ) keymap.get (action); 218 if (s == null) { 219 s = new HashSet (); 220 keymap.put (action, s); 221 } 222 s.add (shortcut); 223 } 224 return keymap; 225 } 226 227 void deleteProfile (String profile) { 228 FileObject root = Repository.getDefault (). 229 getDefaultFileSystem ().getRoot (); 230 root = root.getFileObject (KEYMAPS_FOLDER); 231 if (root == null) return; 232 root = root.getFileObject (profile); 233 if (root == null) return; 234 try { 235 root.delete (); 236 } catch (IOException ex) { 237 ErrorManager.getDefault ().notify (ex); 238 } 239 } 240 241 void saveKeymap (String profile, Map actionToShortcuts) { 243 244 DataFolder folder = getRootFolder (KEYMAPS_FOLDER, profile); 246 if (folder == null) { 247 folder = getRootFolder (KEYMAPS_FOLDER, null); 248 try { 249 folder = DataFolder.create (folder, profile); 250 } catch (IOException ex) { 251 ErrorManager.getDefault ().notify (ex); 252 return; 253 } 254 } 255 saveKeymap (folder, actionToShortcuts, true); 256 257 folder = getRootFolder (SHORTCUTS_FOLDER, null); 258 saveKeymap (folder, actionToShortcuts, false); 259 } 260 261 private void saveKeymap (DataFolder folder, Map actionToShortcuts, boolean add) { 262 Map shortcutToAction = shortcutToAction (actionToShortcuts); 264 265 Enumeration en = folder.children (); 267 while (en.hasMoreElements ()) { 268 DataObject dataObject = (DataObject) en.nextElement (); 269 GlobalAction a1 = (GlobalAction) shortcutToAction.get (dataObject.getName ()); 270 if (a1 != null) { 271 GlobalAction action = createAction (dataObject); 272 if (action == null) continue; 273 if (action.equals (a1)) { 274 shortcutToAction.remove (dataObject.getName ()); 276 continue; 277 } 278 } 279 try { 281 dataObject.delete (); 282 } catch (IOException ex) { 283 ex.printStackTrace (); 284 } 285 } 286 287 if (!add) return; 289 Iterator it = shortcutToAction.keySet ().iterator (); 290 while (it.hasNext ()) { 291 String shortcut = (String ) it.next (); 292 GlobalAction action = (GlobalAction) shortcutToAction.get (shortcut); 293 DataObject dataObject = (DataObject) actionToDataObject.get (action); 294 if (dataObject == null) { 295 if (System.getProperty ("org.netbeans.optionsDialog") != null) 296 System.out.println ("No original DataObject specified! Not possible to create shadow1. " + action); 297 continue; 298 } 299 try { 300 DataShadow.create (folder, shortcut, dataObject); 301 } catch (IOException ex) { 302 ex.printStackTrace (); 303 continue; 304 } 305 } 306 } 307 308 private static DataFolder getRootFolder (String name1, String name2) { 309 FileObject root = Repository.getDefault (). 310 getDefaultFileSystem ().getRoot (); 311 FileObject fo1 = root.getFileObject (name1); 312 try { 313 if (fo1 == null) root.createFolder (name1); 314 if (fo1 == null) return null; 315 if (name2 == null) return DataFolder.findFolder (fo1); 316 FileObject fo2 = fo1.getFileObject (name2); 317 if (fo2 == null) fo1.createFolder (name2); 318 if (fo2 == null) return null; 319 return DataFolder.findFolder (fo2); 320 } catch (IOException ex) { 321 ErrorManager.getDefault ().notify (ex); 322 return null; 323 } 324 } 325 326 329 private GlobalAction createAction (DataObject dataObject) { 330 InstanceCookie ic = (InstanceCookie) dataObject.getCookie 331 (InstanceCookie.class); 332 if (ic == null) return null; 333 try { 334 Object action = ic.instanceCreate (); 335 if (action == null) return null; 336 if (!(action instanceof Action )) return null; 337 return new GlobalAction ((Action ) action); 338 } catch (Exception ex) { 339 ex.printStackTrace (); 340 return null; 341 } 342 } 343 344 349 static Map shortcutToAction (Map actionToShortcuts) { 350 Map shortcutToAction = new HashMap (); 351 Iterator it = actionToShortcuts.keySet ().iterator (); 352 while (it.hasNext ()) { 353 ActionImpl action = (ActionImpl) it.next (); 354 Set shortcuts = (Set ) actionToShortcuts.get (action); 355 if (action instanceof CompoundAction) 356 action = ((CompoundAction) action).getLayersAction (); 357 if (!(action instanceof GlobalAction)) continue; 358 Iterator it2 = shortcuts.iterator (); 359 while (it2.hasNext ()) { 360 String multiShortcut = (String ) it2.next (); 361 shortcutToAction.put (multiShortcut, action); 362 } 363 } 364 return shortcutToAction; 365 } 366 367 private static String [] toArray (String multiShortcut) { 368 StringTokenizer st = new StringTokenizer (multiShortcut, " "); 369 List result = new ArrayList (); 370 while (st.hasMoreTokens ()) { 371 String shortcut = st.nextToken (); 372 if (shortcut == null) { 373 if (System.getProperty ("org.netbeans.optionsDialog") != null) 374 System.out.println ("can not parse shortcut: " + multiShortcut); 375 continue; 376 } 377 result.add (shortcut); 378 } 379 return (String []) result.toArray (new String [result.size ()]); 380 } 381 382 383 private static class GlobalAction implements ActionImpl { 384 private Action action; 385 private String name; 386 private String id; 387 388 private GlobalAction (Action a) { 389 action = a; 390 } 391 392 public String getDisplayName () { 393 if (name == null) { 394 name = (String ) action.getValue (Action.NAME); 395 if (name == null) name = action.toString (); 396 name = name.replaceAll ("&", "").trim (); 397 } 398 return name; 399 } 400 401 public String getId () { 402 if (id == null) 403 id = action.getClass ().getName (); 404 return id; 405 } 406 407 public String getDelegatingActionId () { 408 return null; 409 } 410 411 public boolean equals (Object o) { 412 if (!(o instanceof GlobalAction)) return false; 413 return ((GlobalAction) o).action.equals (action); 414 } 415 416 public int hashCode () { 417 return action.hashCode (); 418 } 419 420 public String toString () { 421 return super.toString () + ":" + getDisplayName (); 422 } 423 } 424 } 425 | Popular Tags |