1 19 20 21 package org.netbeans.modules.options.keymap; 22 23 import java.awt.Component ; 24 import java.awt.event.ActionEvent ; 25 import java.io.File ; 26 import java.io.FileWriter ; 27 import java.io.IOException ; 28 import java.io.OutputStream ; 29 import java.io.OutputStreamWriter ; 30 import java.io.StringWriter ; 31 import java.io.Writer ; 32 import java.util.ArrayList ; 33 import java.util.Collection ; 34 import java.util.Collections ; 35 import java.util.Comparator ; 36 import java.util.Enumeration ; 37 import java.util.HashMap ; 38 import java.util.Iterator ; 39 import java.util.List ; 40 import java.util.Map ; 41 import java.util.Set ; 42 import java.util.SortedMap ; 43 import java.util.StringTokenizer ; 44 import java.util.TreeMap ; 45 import javax.swing.AbstractAction ; 46 import javax.swing.AbstractButton ; 47 import javax.swing.Action ; 48 import javax.swing.JFileChooser ; 49 import javax.swing.JLabel ; 50 import org.netbeans.modules.options.keymap.XMLStorage.Attribs; 51 import org.openide.ErrorManager; 52 import org.openide.awt.Mnemonics; 53 import org.openide.filesystems.FileLock; 54 import org.openide.filesystems.FileObject; 55 import org.openide.filesystems.FileUtil; 56 import org.openide.filesystems.Repository; 57 import org.openide.loaders.DataObject; 58 import org.openide.util.NbBundle; 59 import org.openide.windows.TopComponent; 60 import org.openide.windows.WindowManager; 61 62 public class ExportShortcutsAction { 63 64 private static Action exportIDEActionsAction = new AbstractAction () { 65 {putValue (Action.NAME, loc ("CTL_Export_IDE_Actions_Action"));} 66 67 public void actionPerformed (ActionEvent e) { 68 69 LayersBridge layersBridge = new LayersBridge (); 71 Map categoryToActions = layersBridge.getActions (); 72 Map m = resolveNames (categoryToActions); 73 74 generateLayersXML (layersBridge, m); 75 } 76 }; 77 78 public static Action getExportIDEActionsAction () { 79 return exportIDEActionsAction; 80 } 81 82 private static Action exportIDEShortcutsAction = new AbstractAction () { 83 {putValue (Action.NAME, loc ("CTL_Export_IDE_Shortcuts_Action"));} 84 85 public void actionPerformed (ActionEvent e) { 86 87 Map allKeyMaps = new HashMap (); 89 LayersBridge layersBridge = new LayersBridge (); 90 layersBridge.getActions (); 91 List keyMaps = layersBridge.getProfiles (); 92 Iterator it3 = keyMaps.iterator (); 93 while (it3.hasNext ()) { 94 String keyMapName = (String ) it3.next (); 95 Map actionToShortcuts = layersBridge.getKeymap (keyMapName); 96 Map shortcutToAction = LayersBridge.shortcutToAction (actionToShortcuts); 97 allKeyMaps.put (keyMapName, shortcutToAction); 98 } 99 100 generateLayersXML (layersBridge, allKeyMaps); 101 } 102 }; 103 104 public static Action getExportIDEShortcutsAction () { 105 return exportIDEShortcutsAction; 106 } 107 108 private static Action exportEditorShortcutsAction = new AbstractAction () { 109 {putValue (Action.NAME, loc ("CTL_Export_Editor_Shortcuts_Action"));} 110 111 public void actionPerformed (ActionEvent e) { 112 113 Map allKeyMaps = new HashMap (); 115 EditorBridge editorBridge = new EditorBridge (); 116 editorBridge.getActions (); 117 Map actionToShortcuts = editorBridge.readKeymap (editorBridge.getCurrentProfile ()); 118 generateEditorXML (actionToShortcuts); 119 } 120 }; 121 122 public static Action getExportEditorShortcutsAction () { 123 return exportEditorShortcutsAction; 124 } 125 126 private static Action exportShortcutsToHTMLAction = new AbstractAction () { 127 {putValue (Action.NAME, loc ("CTL_Export_Shortcuts_to_HTML_Action"));} 128 129 public void actionPerformed (ActionEvent e) { 130 exportShortcutsToHTML (); 131 } 132 }; 133 134 public static Action getExportShortcutsToHTMLAction () { 135 return exportShortcutsToHTMLAction; 136 } 137 138 139 141 private static void exportShortcutsToHTML () { 142 KeymapModel keymapModel = new KeymapModel (); 144 Map keymaps = new TreeMap (); 145 Iterator it = keymapModel.getProfiles ().iterator (); 146 while (it.hasNext ()) { 147 String profile = (String ) it.next (); 148 keymaps.put ( 149 profile, 150 keymapModel.getKeymap (profile) 151 ); 152 } 153 154 try { 155 StringBuffer sb = new StringBuffer (); 156 157 Attribs attribs = new Attribs (true); 158 XMLStorage.generateFolderStart (sb, "html", attribs, ""); 159 XMLStorage.generateFolderStart (sb, "body", attribs, " "); 160 attribs.add ("border", "1"); 161 attribs.add ("cellpadding", "1"); 162 attribs.add ("cellspacing", "0"); 163 XMLStorage.generateFolderStart (sb, "table", attribs, " "); 164 attribs = new Attribs (true); 165 166 XMLStorage.generateFolderStart (sb, "tr", attribs, " "); 168 XMLStorage.generateFolderStart (sb, "td", attribs, " "); 169 XMLStorage.generateFolderStart (sb, "h2", attribs, " "); 170 sb.append ("Action Name"); 171 XMLStorage.generateFolderEnd (sb, "h2", " "); 172 XMLStorage.generateFolderEnd (sb, "td", " "); 173 it = keymaps.keySet ().iterator (); 174 while (it.hasNext ()) { 175 String profile = (String ) it.next (); 176 XMLStorage.generateFolderStart (sb, "td", attribs, " "); 177 XMLStorage.generateFolderStart (sb, "h2", attribs, " "); 178 sb.append (profile); 179 XMLStorage.generateFolderEnd (sb, "h2", " "); 180 XMLStorage.generateFolderEnd (sb, "td", " "); 181 } 182 183 exportShortcutsToHTML2 (keymapModel, sb, keymaps); 185 186 XMLStorage.generateFolderEnd (sb, "table", " "); 187 XMLStorage.generateFolderEnd (sb, "body", " "); 188 XMLStorage.generateFolderEnd (sb, "html", ""); 189 190 FileObject fo = FileUtil.createData ( 191 Repository.getDefault ().getDefaultFileSystem ().getRoot (), 192 "shortcuts.html" 193 ); 194 FileLock fileLock = fo.lock (); 195 try { 196 OutputStream outputStream = fo.getOutputStream (fileLock); 197 OutputStreamWriter writer = new OutputStreamWriter (outputStream); 198 writer.write (sb.toString ()); 199 writer.close (); 200 } catch (IOException ex) { 201 ErrorManager.getDefault ().notify (ex); 202 } finally { 203 fileLock.releaseLock (); 204 } 205 } catch (IOException ex) { 206 ErrorManager.getDefault ().notify (ex); 207 } 208 } 209 210 213 private static void exportShortcutsToHTML2 ( 214 KeymapModel keymapModel, 215 StringBuffer sb, 216 Map keymaps 217 ) { 218 List categories = new ArrayList (keymapModel.getActionCategories ()); 219 Collections.sort (categories); 220 Attribs attribs = new Attribs (true); 221 Iterator it = categories.iterator (); 222 while (it.hasNext ()) { 223 String category = (String ) it.next (); 224 225 XMLStorage.generateFolderStart (sb, "tr", attribs, " "); 227 attribs.add ("colspan", Integer.toString (keymaps.size () + 1)); 228 attribs.add ("rowspan", "1"); 229 XMLStorage.generateFolderStart (sb, "td", attribs, " "); 230 attribs = new Attribs (true); 231 XMLStorage.generateFolderStart (sb, "h3", attribs, " "); 232 sb.append (category); 233 XMLStorage.generateFolderEnd (sb, "h3", " "); 234 XMLStorage.generateFolderEnd (sb, "td", " "); 235 XMLStorage.generateFolderEnd (sb, "tr", " "); 236 237 exportShortcutsToHTML3 (sb, keymapModel, category, keymaps); 239 } 240 } 241 242 245 private static void exportShortcutsToHTML3 ( 246 StringBuffer sb, 247 KeymapModel keymapModel, 248 String category, 249 Map keymaps 250 ) { 251 Set actions = keymapModel.getActions (category); 252 253 Map sortedActions = new TreeMap (); 255 Iterator it = actions.iterator (); 256 while (it.hasNext ()) { 257 ActionImpl action = (ActionImpl) it.next (); 258 sortedActions.put ( 259 action.getDisplayName (), 260 action 261 ); 262 } 263 264 Attribs attribs = new Attribs (true); 266 it = sortedActions.keySet ().iterator (); 267 while (it.hasNext ()) { 268 String actionName = (String ) it.next (); 269 ActionImpl action = (ActionImpl) sortedActions.get (actionName); 270 271 XMLStorage.generateFolderStart (sb, "tr", attribs, " "); 273 XMLStorage.generateFolderStart (sb, "td", attribs, " "); 274 sb.append (actionName); 275 XMLStorage.generateFolderEnd (sb, "td", " "); 276 277 Iterator it2 = keymaps.keySet ().iterator (); 278 while (it2.hasNext ()) { 279 String profile = (String ) it2.next (); 280 Map keymap = (Map ) keymaps.get (profile); 281 Set shortcuts = (Set ) keymap.get (action); 282 283 XMLStorage.generateFolderStart (sb, "td", attribs, " "); 284 printShortcuts (shortcuts, sb); 285 XMLStorage.generateFolderEnd (sb, "td", " "); 286 } 287 288 XMLStorage.generateFolderEnd (sb, "tr", " "); 289 } 290 } 291 292 private static void printShortcuts (Set shortcuts, StringBuffer sb) { 293 if (shortcuts == null) { 294 sb.append ('-'); 295 return; 296 } 297 Iterator it = shortcuts.iterator (); 298 while (it.hasNext ()) { 299 String shortcut = (String ) it.next (); 300 sb.append (shortcut); 301 if (it.hasNext ()) sb.append (", "); 302 } 303 } 304 305 private static void generateLayersXML ( 306 LayersBridge layersBridge, 307 Map categoryToActions 308 ) { 309 Writer fw = null; 310 try { 311 fw = openWriter (); 312 if (fw == null) return; 313 314 StringBuffer sb = XMLStorage.generateHeader (); 315 Attribs attribs = new Attribs (true); 316 XMLStorage.generateFolderStart (sb, "filesystem", attribs, ""); 317 attribs.add ("name", "Keymaps"); 318 XMLStorage.generateFolderStart (sb, "folder", attribs, " "); 319 generateShadowsToXML (layersBridge, sb, categoryToActions, " "); 320 XMLStorage.generateFolderEnd (sb, "folder", " "); 321 XMLStorage.generateFolderEnd (sb, "filesystem", ""); 322 System.out.println(sb.toString ()); 323 fw.write (sb.toString ()); 324 } catch (IOException e) { 325 ErrorManager.getDefault ().notify (e); 326 } finally { 327 try { 328 if (fw != null) { 329 fw.flush (); 330 fw.close (); 331 } 332 } catch (IOException e) {} 333 } 334 } 335 336 private static void generateEditorXML ( 337 Map actionToShortcuts 338 ) { 339 Writer fw = null; 340 try { 341 fw = openWriter (); 342 if (fw == null) return; 343 344 StringBuffer sb = XMLStorage.generateHeader (); 345 Attribs attribs = new Attribs (true); 346 XMLStorage.generateFolderStart (sb, "bindings", attribs, ""); 347 348 Map sortedMap = new TreeMap (); 349 Iterator it = actionToShortcuts.keySet ().iterator (); 350 while (it.hasNext ()) { 351 ActionImpl action = (ActionImpl) it.next (); 352 sortedMap.put ( 353 action.getDisplayName (), 354 actionToShortcuts.get (action) 355 ); 356 } 357 it = sortedMap.keySet ().iterator (); 358 while (it.hasNext ()) { 359 String actionName = (String ) it.next (); 360 Set shortcuts = (Set ) sortedMap.get (actionName); 361 Iterator it2 = shortcuts.iterator (); 362 while (it2.hasNext ()) { 363 String shortcut = (String ) it2.next (); 364 attribs = new Attribs (true); 365 attribs.add ("actionName", actionName); 366 attribs.add ("key", shortcut); 367 XMLStorage.generateLeaf (sb, "bind", attribs, " "); 368 } 369 } 370 371 XMLStorage.generateFolderEnd (sb, "bindings", ""); 372 System.out.println(sb.toString ()); 373 fw.write (sb.toString ()); 374 } catch (IOException e) { 375 ErrorManager.getDefault ().notify (e); 376 } finally { 377 try { 378 if (fw != null) { 379 fw.flush (); 380 fw.close (); 381 } 382 } catch (IOException e) {} 383 } 384 } 385 386 private static Map resolveNames (Map categoryToActions) { 387 Map result = new HashMap (); 388 Iterator it = categoryToActions.keySet ().iterator (); 389 while (it.hasNext ()) { 390 String category = (String ) it.next (); 391 Set actions = (Set ) categoryToActions.get (category); 392 Map actionsMap = new HashMap (); 393 Iterator it1 = actions.iterator (); 394 while (it1.hasNext ()) { 395 ActionImpl action = (ActionImpl) it1.next (); 396 actionsMap.put (action.getDisplayName (), action); 397 } 398 result.put (category, actionsMap); 399 } 400 return result; 401 } 402 403 411 private static void generateShadowsToXML ( 412 LayersBridge layersBridge, 413 StringBuffer sb, 414 Map shortcutToAction, 415 String indentation 416 ) { 417 Iterator it = shortcutToAction.keySet ().iterator (); 418 while (it.hasNext ()) { 419 String key = (String ) it.next (); 420 Object value = shortcutToAction.get (key); 421 if (value instanceof Map ) { 422 Attribs attribs = new Attribs (true); 423 attribs.add ("name", key); 424 XMLStorage.generateFolderStart (sb, "folder", attribs, indentation); 425 generateShadowsToXML ( 426 layersBridge, 427 sb, 428 (Map ) value, 429 " " + indentation 430 ); 431 XMLStorage.generateFolderEnd (sb, "folder", indentation); 432 } else { 433 DataObject dob = layersBridge.getDataObject (value); 434 if (dob == null) { 435 System.out.println("no Dataobject " + value); 436 continue; 437 } 438 FileObject fo = dob.getPrimaryFile (); 439 Attribs attribs = new Attribs (true); 440 attribs.add ("name", (String ) key + ".shadow"); 441 XMLStorage.generateFolderStart (sb, "file", attribs, indentation); 442 Attribs attribs2 = new Attribs (true); 443 attribs2.add ("name", "originalFile"); 444 attribs2.add ("stringvalue", fo.getPath ()); 445 XMLStorage.generateLeaf (sb, "attr", attribs2, indentation + " "); 446 XMLStorage.generateFolderEnd (sb, "file", indentation); 447 } 448 } 449 } 450 451 private static Writer openWriter () throws IOException { 452 JFileChooser fileChooser = new JFileChooser (); 453 int result = fileChooser.showSaveDialog 454 (WindowManager.getDefault ().getMainWindow ()); 455 if (result != JFileChooser.APPROVE_OPTION) return null; 456 File f = fileChooser.getSelectedFile (); 457 return new FileWriter (f); 458 } 459 460 private static String loc (String key) { 461 return NbBundle.getMessage (ExportShortcutsAction.class, key); 462 } 463 } 464 465 | Popular Tags |