PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/options.keymap/src/org/netbeans/modules/options/keymap/ExportShortcutsAction.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 477 lines | 355 code | 54 blank | 68 comment | 34 complexity | 38e664d3f296fab5a557f77fb5f909c4 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package org.netbeans.modules.options.keymap;
  45. import java.awt.event.ActionEvent;
  46. import java.io.File;
  47. import java.io.FileWriter;
  48. import java.io.IOException;
  49. import java.io.OutputStream;
  50. import java.io.OutputStreamWriter;
  51. import java.io.Writer;
  52. import java.util.ArrayList;
  53. import java.util.Collections;
  54. import java.util.HashMap;
  55. import java.util.Iterator;
  56. import java.util.List;
  57. import java.util.Map;
  58. import java.util.Set;
  59. import java.util.TreeMap;
  60. import javax.swing.AbstractAction;
  61. import javax.swing.Action;
  62. import javax.swing.JFileChooser;
  63. import org.netbeans.core.options.keymap.api.ShortcutAction;
  64. import org.netbeans.core.options.keymap.spi.KeymapManager;
  65. import org.netbeans.modules.options.keymap.XMLStorage.Attribs;
  66. import org.openide.ErrorManager;
  67. import org.openide.filesystems.FileLock;
  68. import org.openide.filesystems.FileObject;
  69. import org.openide.filesystems.FileUtil;
  70. import org.openide.loaders.DataObject;
  71. import org.openide.util.NbBundle;
  72. import org.openide.windows.WindowManager;
  73. public class ExportShortcutsAction {
  74. private ExportShortcutsAction() {}
  75. private static Action exportIDEActionsAction = new AbstractAction () {
  76. {putValue (Action.NAME, loc ("CTL_Export_IDE_Actions_Action"));}
  77. public void actionPerformed (ActionEvent e) {
  78. // 1) load all keymaps to allKeyMaps
  79. LayersBridge layersBridge = new LayersBridge ();
  80. Map<String, Set<ShortcutAction>> categoryToActions = layersBridge.getActions ();
  81. Map<String, Map<String, ShortcutAction>> m = resolveNames (categoryToActions);
  82. generateLayersXML (layersBridge, m);
  83. }
  84. };
  85. public static Action getExportIDEActionsAction () {
  86. return exportIDEActionsAction;
  87. }
  88. private static Action exportIDEShortcutsAction = new AbstractAction () {
  89. {putValue (Action.NAME, loc ("CTL_Export_IDE_Shortcuts_Action"));}
  90. public void actionPerformed (ActionEvent e) {
  91. // 1) load all keymaps to allKeyMaps
  92. Map<String, Map<String, ShortcutAction>> allKeyMaps =
  93. new HashMap<String, Map<String, ShortcutAction>> ();
  94. LayersBridge layersBridge = new LayersBridge ();
  95. layersBridge.getActions ();
  96. List keyMaps = layersBridge.getProfiles ();
  97. Iterator it3 = keyMaps.iterator ();
  98. while (it3.hasNext ()) {
  99. String keyMapName = (String) it3.next ();
  100. Map<ShortcutAction, Set<String>> actionToShortcuts = layersBridge.getKeymap (keyMapName);
  101. Map<String, ShortcutAction> shortcutToAction = LayersBridge.shortcutToAction (actionToShortcuts);
  102. allKeyMaps.put (keyMapName, shortcutToAction);
  103. }
  104. generateLayersXML (layersBridge, allKeyMaps);
  105. }
  106. };
  107. public static Action getExportIDEShortcutsAction () {
  108. return exportIDEShortcutsAction;
  109. }
  110. private static Action exportEditorShortcutsAction = new AbstractAction () {
  111. {putValue (Action.NAME, loc ("CTL_Export_Editor_Shortcuts_Action"));}
  112. public void actionPerformed (ActionEvent e) {
  113. KeymapManager editorBridge = null;
  114. for (KeymapManager km : KeymapModel.getKeymapManagerInstances()) {
  115. if ("EditorBridge".equals(km.getName())) {
  116. editorBridge = km;
  117. break;
  118. }
  119. }
  120. if (editorBridge != null) {
  121. Map<ShortcutAction, Set<String>> actionToShortcuts =
  122. editorBridge.getKeymap(editorBridge.getCurrentProfile ());
  123. generateEditorXML (actionToShortcuts);
  124. }
  125. }
  126. };
  127. public static Action getExportEditorShortcutsAction () {
  128. return exportEditorShortcutsAction;
  129. }
  130. private static Action exportShortcutsToHTMLAction = new AbstractAction () {
  131. {putValue (Action.NAME, loc ("CTL_Export_Shortcuts_to_HTML_Action"));}
  132. public void actionPerformed (ActionEvent e) {
  133. exportShortcutsToHTML ();
  134. }
  135. };
  136. public static Action getExportShortcutsToHTMLAction () {
  137. return exportShortcutsToHTMLAction;
  138. }
  139. // helper methods ..........................................................
  140. private static void exportShortcutsToHTML () {
  141. // read all shortcuts to keymaps
  142. KeymapModel keymapModel = new KeymapModel ();
  143. Map<String, Map<ShortcutAction, Set<String>>> keymaps =
  144. new TreeMap<String, Map<ShortcutAction, Set<String>>> ();
  145. for (String profile: keymapModel.getProfiles ()) {
  146. keymaps.put (
  147. profile,
  148. keymapModel.getKeymap (profile)
  149. );
  150. }
  151. try {
  152. StringBuffer sb = new StringBuffer ();
  153. Attribs attribs = new Attribs (true);
  154. XMLStorage.generateFolderStart (sb, "html", attribs, "");
  155. XMLStorage.generateFolderStart (sb, "body", attribs, " ");
  156. attribs.add ("border", "1");
  157. attribs.add ("cellpadding", "1");
  158. attribs.add ("cellspacing", "0");
  159. XMLStorage.generateFolderStart (sb, "table", attribs, " ");
  160. attribs = new Attribs (true);
  161. // print header of table
  162. XMLStorage.generateFolderStart (sb, "tr", attribs, " ");
  163. XMLStorage.generateFolderStart (sb, "td", attribs, " ");
  164. XMLStorage.generateFolderStart (sb, "h2", attribs, " ");
  165. sb.append ("Action Name");
  166. XMLStorage.generateFolderEnd (sb, "h2", " ");
  167. XMLStorage.generateFolderEnd (sb, "td", " ");
  168. for (String profile: keymaps.keySet ()) {
  169. XMLStorage.generateFolderStart (sb, "td", attribs, " ");
  170. XMLStorage.generateFolderStart (sb, "h2", attribs, " ");
  171. sb.append (profile);
  172. XMLStorage.generateFolderEnd (sb, "h2", " ");
  173. XMLStorage.generateFolderEnd (sb, "td", " ");
  174. }
  175. // print body of table
  176. exportShortcutsToHTML2 (keymapModel, sb, keymaps);
  177. XMLStorage.generateFolderEnd (sb, "table", " ");
  178. XMLStorage.generateFolderEnd (sb, "body", " ");
  179. XMLStorage.generateFolderEnd (sb, "html", "");
  180. FileObject fo = FileUtil.createData (
  181. FileUtil.getConfigRoot (),
  182. "shortcuts.html"
  183. );
  184. FileLock fileLock = fo.lock ();
  185. try {
  186. OutputStream outputStream = fo.getOutputStream (fileLock);
  187. OutputStreamWriter writer = new OutputStreamWriter (outputStream);
  188. writer.write (sb.toString ());
  189. writer.close ();
  190. } catch (IOException ex) {
  191. ErrorManager.getDefault ().notify (ex);
  192. } finally {
  193. fileLock.releaseLock ();
  194. }
  195. } catch (IOException ex) {
  196. ErrorManager.getDefault ().notify (ex);
  197. }
  198. }
  199. /**
  200. * Writes body of shortcuts table to given StringBuffer.
  201. */
  202. private static void exportShortcutsToHTML2 (
  203. KeymapModel keymapModel,
  204. StringBuffer sb,
  205. Map<String, Map<ShortcutAction, Set<String>>> keymaps
  206. ) {
  207. List<String> categories = new ArrayList<String> (keymapModel.getActionCategories ());
  208. Collections.<String>sort (categories);
  209. Attribs attribs = new Attribs (true);
  210. for (String category: categories) {
  211. // print category title
  212. XMLStorage.generateFolderStart (sb, "tr", attribs, " ");
  213. attribs.add ("colspan", Integer.toString (keymaps.size () + 1));
  214. attribs.add ("rowspan", "1");
  215. XMLStorage.generateFolderStart (sb, "td", attribs, " ");
  216. attribs = new Attribs (true);
  217. XMLStorage.generateFolderStart (sb, "h3", attribs, " ");
  218. sb.append (category);
  219. XMLStorage.generateFolderEnd (sb, "h3", " ");
  220. XMLStorage.generateFolderEnd (sb, "td", " ");
  221. XMLStorage.generateFolderEnd (sb, "tr", " ");
  222. // print body of one category
  223. exportShortcutsToHTML3 (sb, keymapModel, category, keymaps);
  224. }
  225. }
  226. /**
  227. * Writes body of given category.
  228. */
  229. private static void exportShortcutsToHTML3 (
  230. StringBuffer sb,
  231. KeymapModel keymapModel,
  232. String category,
  233. Map<String, Map<ShortcutAction, Set<String>>> keymaps
  234. ) {
  235. Set<ShortcutAction> actions = keymapModel.getActions (category);
  236. // sort actions
  237. Map<String, ShortcutAction> sortedActions = new TreeMap<String, ShortcutAction> ();
  238. for (ShortcutAction action: actions) {
  239. sortedActions.put (
  240. action.getDisplayName (),
  241. action
  242. );
  243. }
  244. // print actions
  245. Attribs attribs = new Attribs (true);
  246. for (Map.Entry<String, ShortcutAction> entry: sortedActions.entrySet()) {
  247. String actionName = entry.getKey();
  248. ShortcutAction action = entry.getValue();
  249. // print action name to the first column
  250. XMLStorage.generateFolderStart (sb, "tr", attribs, " ");
  251. XMLStorage.generateFolderStart (sb, "td", attribs, " ");
  252. sb.append (actionName);
  253. XMLStorage.generateFolderEnd (sb, "td", " ");
  254. for (String profile: keymaps.keySet ()) {
  255. Map<ShortcutAction, Set<String>> keymap = keymaps.get (profile);
  256. Set<String> shortcuts = keymap.get (action);
  257. XMLStorage.generateFolderStart (sb, "td", attribs, " ");
  258. printShortcuts (shortcuts, sb);
  259. XMLStorage.generateFolderEnd (sb, "td", " ");
  260. }
  261. XMLStorage.generateFolderEnd (sb, "tr", " ");
  262. }
  263. }
  264. private static void printShortcuts (Set<String> shortcuts, StringBuffer sb) {
  265. if (shortcuts == null) {
  266. sb.append ('-');
  267. return;
  268. }
  269. Iterator<String> it = shortcuts.iterator ();
  270. while (it.hasNext ()) {
  271. String shortcut = it.next ();
  272. sb.append (shortcut);
  273. if (it.hasNext ()) sb.append (", ");
  274. }
  275. }
  276. private static void generateLayersXML (
  277. LayersBridge layersBridge,
  278. Map<String, Map<String, ShortcutAction>> categoryToActions
  279. ) {
  280. Writer fw = null;
  281. try {
  282. fw = openWriter ();
  283. if (fw == null) return;
  284. StringBuffer sb = XMLStorage.generateHeader ();
  285. Attribs attribs = new Attribs (true);
  286. XMLStorage.generateFolderStart (sb, "filesystem", attribs, "");
  287. attribs.add ("name", "Keymaps");
  288. XMLStorage.generateFolderStart (sb, "folder", attribs, " ");
  289. generateShadowsToXML (layersBridge, sb, categoryToActions, " ");
  290. XMLStorage.generateFolderEnd (sb, "folder", " ");
  291. XMLStorage.generateFolderEnd (sb, "filesystem", "");
  292. fw.write (sb.toString ());
  293. } catch (IOException e) {
  294. ErrorManager.getDefault ().notify (e);
  295. } finally {
  296. try {
  297. if (fw != null) {
  298. fw.flush ();
  299. fw.close ();
  300. }
  301. } catch (IOException e) {}
  302. }
  303. }
  304. private static void generateEditorXML (
  305. Map<ShortcutAction, Set<String>> actionToShortcuts
  306. ) {
  307. Writer fw = null;
  308. try {
  309. fw = openWriter ();
  310. if (fw == null) return;
  311. StringBuffer sb = XMLStorage.generateHeader ();
  312. Attribs attribs = new Attribs (true);
  313. XMLStorage.generateFolderStart (sb, "bindings", attribs, "");
  314. Map<String, Set<String>> sortedMap = new TreeMap<String, Set<String>> ();
  315. for (ShortcutAction action: actionToShortcuts.keySet ()) {
  316. sortedMap.put (
  317. action.getDisplayName (),
  318. actionToShortcuts.get (action)
  319. );
  320. }
  321. for (String actionName: sortedMap.keySet ()) {
  322. Set<String> shortcuts = sortedMap.get (actionName);
  323. for (String shortcut: shortcuts) {
  324. attribs = new Attribs (true);
  325. attribs.add ("actionName", actionName);
  326. attribs.add ("key", shortcut);
  327. XMLStorage.generateLeaf (sb, "bind", attribs, " ");
  328. }
  329. }
  330. XMLStorage.generateFolderEnd (sb, "bindings", "");
  331. fw.write (sb.toString ());
  332. } catch (IOException e) {
  333. ErrorManager.getDefault ().notify (e);
  334. } finally {
  335. try {
  336. if (fw != null) {
  337. fw.flush ();
  338. fw.close ();
  339. }
  340. } catch (IOException e) {}
  341. }
  342. }
  343. private static Map<String, Map<String, ShortcutAction>> resolveNames (Map<String, Set<ShortcutAction>> categoryToActions) {
  344. Map<String, Map<String, ShortcutAction>> result = new HashMap<String, Map<String, ShortcutAction>> ();
  345. for (Map.Entry<String, Set<ShortcutAction>> entry: categoryToActions.entrySet ()) {
  346. String category = entry.getKey();
  347. Set<ShortcutAction> actions = entry.getValue();
  348. Map<String, ShortcutAction> actionsMap = new HashMap<String, ShortcutAction> ();
  349. for (ShortcutAction action: actions) {
  350. actionsMap.put (action.getDisplayName (), action);
  351. }
  352. result.put (category, actionsMap);
  353. }
  354. return result;
  355. }
  356. /**
  357. * Converts:
  358. * Map (String (profile | category) > Map (String (category)) |
  359. * ShortcutAction)
  360. * to xml.
  361. * (String > Map) is represented by folder and
  362. * (String > DataObject) by ShadowDO
  363. */
  364. private static void generateShadowsToXML (
  365. LayersBridge layersBridge,
  366. StringBuffer sb,
  367. Map<String, Map<String, ShortcutAction>> shortcutToAction,
  368. String indentation
  369. ) {
  370. Iterator<String> it = shortcutToAction.keySet ().iterator ();
  371. while (it.hasNext ()) {
  372. String key = it.next ();
  373. Map<String, ShortcutAction> value = shortcutToAction.get (key);
  374. Attribs attribs = new Attribs (true);
  375. attribs.add ("name", key);
  376. XMLStorage.generateFolderStart (sb, "folder", attribs, indentation);
  377. generateShadowsToXML2 (
  378. layersBridge,
  379. sb,
  380. value,
  381. " " + indentation
  382. );
  383. XMLStorage.generateFolderEnd (sb, "folder", indentation);
  384. }
  385. }
  386. private static void generateShadowsToXML2 (
  387. LayersBridge layersBridge,
  388. StringBuffer sb,
  389. Map<String, ShortcutAction> shortcutToAction,
  390. String indentation
  391. ) {
  392. Iterator<String> it = shortcutToAction.keySet ().iterator ();
  393. while (it.hasNext ()) {
  394. String key = it.next ();
  395. ShortcutAction value = shortcutToAction.get (key);
  396. DataObject dob = layersBridge.getDataObject (value);
  397. if (dob == null) {
  398. System.out.println("no Dataobject " + value);
  399. continue;
  400. }
  401. FileObject fo = dob.getPrimaryFile ();
  402. Attribs attribs = new Attribs (true);
  403. attribs.add ("name", key + ".shadow");
  404. XMLStorage.generateFolderStart (sb, "file", attribs, indentation);
  405. Attribs attribs2 = new Attribs (true);
  406. attribs2.add ("name", "originalFile");
  407. attribs2.add ("stringvalue", fo.getPath ());
  408. XMLStorage.generateLeaf (sb, "attr", attribs2, indentation + " ");
  409. XMLStorage.generateFolderEnd (sb, "file", indentation);
  410. }
  411. }
  412. private static Writer openWriter () throws IOException {
  413. JFileChooser fileChooser = new JFileChooser ();
  414. int result = fileChooser.showSaveDialog
  415. (WindowManager.getDefault ().getMainWindow ());
  416. if (result != JFileChooser.APPROVE_OPTION) return null;
  417. File f = fileChooser.getSelectedFile ();
  418. return new FileWriter (f);
  419. }
  420. private static String loc (String key) {
  421. return NbBundle.getMessage (ExportShortcutsAction.class, key);
  422. }
  423. }