KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > options > keymap > ExportShortcutsAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.options.keymap;
22
23 import java.awt.Component JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileWriter JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.io.OutputStreamWriter JavaDoc;
30 import java.io.StringWriter JavaDoc;
31 import java.io.Writer JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.Collections JavaDoc;
35 import java.util.Comparator JavaDoc;
36 import java.util.Enumeration JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.Map JavaDoc;
41 import java.util.Set JavaDoc;
42 import java.util.SortedMap JavaDoc;
43 import java.util.StringTokenizer JavaDoc;
44 import java.util.TreeMap JavaDoc;
45 import javax.swing.AbstractAction JavaDoc;
46 import javax.swing.AbstractButton JavaDoc;
47 import javax.swing.Action JavaDoc;
48 import javax.swing.JFileChooser JavaDoc;
49 import javax.swing.JLabel JavaDoc;
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 JavaDoc exportIDEActionsAction = new AbstractAction JavaDoc () {
65         {putValue (Action.NAME, loc ("CTL_Export_IDE_Actions_Action"));}
66         
67         public void actionPerformed (ActionEvent JavaDoc e) {
68
69             // 1) load all keymaps to allKeyMaps
70
LayersBridge layersBridge = new LayersBridge ();
71             Map JavaDoc categoryToActions = layersBridge.getActions ();
72             Map JavaDoc m = resolveNames (categoryToActions);
73
74             generateLayersXML (layersBridge, m);
75         }
76     };
77     
78     public static Action JavaDoc getExportIDEActionsAction () {
79         return exportIDEActionsAction;
80     }
81     
82     private static Action JavaDoc exportIDEShortcutsAction = new AbstractAction JavaDoc () {
83         {putValue (Action.NAME, loc ("CTL_Export_IDE_Shortcuts_Action"));}
84         
85         public void actionPerformed (ActionEvent JavaDoc e) {
86
87             // 1) load all keymaps to allKeyMaps
88
Map JavaDoc allKeyMaps = new HashMap JavaDoc ();
89             LayersBridge layersBridge = new LayersBridge ();
90             layersBridge.getActions ();
91             List JavaDoc keyMaps = layersBridge.getProfiles ();
92             Iterator JavaDoc it3 = keyMaps.iterator ();
93             while (it3.hasNext ()) {
94                 String JavaDoc keyMapName = (String JavaDoc) it3.next ();
95                 Map JavaDoc actionToShortcuts = layersBridge.getKeymap (keyMapName);
96                 Map JavaDoc shortcutToAction = LayersBridge.shortcutToAction (actionToShortcuts);
97                 allKeyMaps.put (keyMapName, shortcutToAction);
98             }
99
100             generateLayersXML (layersBridge, allKeyMaps);
101         }
102     };
103     
104     public static Action JavaDoc getExportIDEShortcutsAction () {
105         return exportIDEShortcutsAction;
106     }
107     
108     private static Action JavaDoc exportEditorShortcutsAction = new AbstractAction JavaDoc () {
109         {putValue (Action.NAME, loc ("CTL_Export_Editor_Shortcuts_Action"));}
110         
111         public void actionPerformed (ActionEvent JavaDoc e) {
112
113             // 1) load all keymaps to allKeyMaps
114
Map JavaDoc allKeyMaps = new HashMap JavaDoc ();
115             EditorBridge editorBridge = new EditorBridge ();
116             editorBridge.getActions ();
117             Map JavaDoc actionToShortcuts = editorBridge.readKeymap (editorBridge.getCurrentProfile ());
118             generateEditorXML (actionToShortcuts);
119         }
120     };
121     
122     public static Action JavaDoc getExportEditorShortcutsAction () {
123         return exportEditorShortcutsAction;
124     }
125     
126     private static Action JavaDoc exportShortcutsToHTMLAction = new AbstractAction JavaDoc () {
127         {putValue (Action.NAME, loc ("CTL_Export_Shortcuts_to_HTML_Action"));}
128         
129         public void actionPerformed (ActionEvent JavaDoc e) {
130             exportShortcutsToHTML ();
131         }
132     };
133     
134     public static Action JavaDoc getExportShortcutsToHTMLAction () {
135         return exportShortcutsToHTMLAction;
136     }
137
138     
139     // helper methods ..........................................................
140

141     private static void exportShortcutsToHTML () {
142         // read all shortcuts to keymaps
143
KeymapModel keymapModel = new KeymapModel ();
144         Map JavaDoc keymaps = new TreeMap JavaDoc ();
145         Iterator JavaDoc it = keymapModel.getProfiles ().iterator ();
146         while (it.hasNext ()) {
147             String JavaDoc profile = (String JavaDoc) it.next ();
148             keymaps.put (
149                 profile,
150                 keymapModel.getKeymap (profile)
151             );
152         }
153         
154         try {
155             StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
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             // print header of table
167
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 JavaDoc profile = (String JavaDoc) 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             // print body of table
184
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 JavaDoc outputStream = fo.getOutputStream (fileLock);
197                 OutputStreamWriter JavaDoc writer = new OutputStreamWriter JavaDoc (outputStream);
198                 writer.write (sb.toString ());
199                 writer.close ();
200             } catch (IOException JavaDoc ex) {
201                 ErrorManager.getDefault ().notify (ex);
202             } finally {
203                 fileLock.releaseLock ();
204             }
205         } catch (IOException JavaDoc ex) {
206             ErrorManager.getDefault ().notify (ex);
207         }
208     }
209
210     /**
211      * Writes body of shortcuts table to given StringBuffer.
212      */

213     private static void exportShortcutsToHTML2 (
214         KeymapModel keymapModel,
215         StringBuffer JavaDoc sb,
216         Map JavaDoc keymaps
217     ) {
218         List JavaDoc categories = new ArrayList JavaDoc (keymapModel.getActionCategories ());
219         Collections.sort (categories);
220         Attribs attribs = new Attribs (true);
221         Iterator JavaDoc it = categories.iterator ();
222         while (it.hasNext ()) {
223             String JavaDoc category = (String JavaDoc) it.next ();
224             
225             // print category title
226
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             // print body of one category
238
exportShortcutsToHTML3 (sb, keymapModel, category, keymaps);
239         }
240     }
241
242     /**
243      * Writes body of given category.
244      */

245     private static void exportShortcutsToHTML3 (
246         StringBuffer JavaDoc sb,
247         KeymapModel keymapModel,
248         String JavaDoc category,
249         Map JavaDoc keymaps
250     ) {
251         Set JavaDoc actions = keymapModel.getActions (category);
252
253         // sort actions
254
Map JavaDoc sortedActions = new TreeMap JavaDoc ();
255         Iterator JavaDoc 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         // print actions
265
Attribs attribs = new Attribs (true);
266         it = sortedActions.keySet ().iterator ();
267         while (it.hasNext ()) {
268             String JavaDoc actionName = (String JavaDoc) it.next ();
269             ActionImpl action = (ActionImpl) sortedActions.get (actionName);
270
271             // print action name to the first column
272
XMLStorage.generateFolderStart (sb, "tr", attribs, " ");
273             XMLStorage.generateFolderStart (sb, "td", attribs, " ");
274             sb.append (actionName);
275             XMLStorage.generateFolderEnd (sb, "td", " ");
276             
277             Iterator JavaDoc it2 = keymaps.keySet ().iterator ();
278             while (it2.hasNext ()) {
279                 String JavaDoc profile = (String JavaDoc) it2.next ();
280                 Map JavaDoc keymap = (Map JavaDoc) keymaps.get (profile);
281                 Set JavaDoc shortcuts = (Set JavaDoc) 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 JavaDoc shortcuts, StringBuffer JavaDoc sb) {
293         if (shortcuts == null) {
294             sb.append ('-');
295             return;
296         }
297         Iterator JavaDoc it = shortcuts.iterator ();
298         while (it.hasNext ()) {
299             String JavaDoc shortcut = (String JavaDoc) 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 JavaDoc categoryToActions
308     ) {
309         Writer JavaDoc fw = null;
310         try {
311             fw = openWriter ();
312             if (fw == null) return;
313
314             StringBuffer JavaDoc 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 JavaDoc e) {
325             ErrorManager.getDefault ().notify (e);
326         } finally {
327             try {
328                 if (fw != null) {
329                     fw.flush ();
330                     fw.close ();
331                 }
332             } catch (IOException JavaDoc e) {}
333         }
334     }
335     
336     private static void generateEditorXML (
337         Map JavaDoc actionToShortcuts
338     ) {
339         Writer JavaDoc fw = null;
340         try {
341             fw = openWriter ();
342             if (fw == null) return;
343
344             StringBuffer JavaDoc sb = XMLStorage.generateHeader ();
345             Attribs attribs = new Attribs (true);
346             XMLStorage.generateFolderStart (sb, "bindings", attribs, "");
347             
348             Map JavaDoc sortedMap = new TreeMap JavaDoc ();
349             Iterator JavaDoc 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 JavaDoc actionName = (String JavaDoc) it.next ();
360                 Set JavaDoc shortcuts = (Set JavaDoc) sortedMap.get (actionName);
361                 Iterator JavaDoc it2 = shortcuts.iterator ();
362                 while (it2.hasNext ()) {
363                     String JavaDoc shortcut = (String JavaDoc) 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 JavaDoc e) {
375             ErrorManager.getDefault ().notify (e);
376         } finally {
377             try {
378                 if (fw != null) {
379                     fw.flush ();
380                     fw.close ();
381                 }
382             } catch (IOException JavaDoc e) {}
383         }
384     }
385     
386     private static Map JavaDoc resolveNames (Map JavaDoc categoryToActions) {
387         Map JavaDoc result = new HashMap JavaDoc ();
388         Iterator JavaDoc it = categoryToActions.keySet ().iterator ();
389         while (it.hasNext ()) {
390             String JavaDoc category = (String JavaDoc) it.next ();
391             Set JavaDoc actions = (Set JavaDoc) categoryToActions.get (category);
392             Map JavaDoc actionsMap = new HashMap JavaDoc ();
393             Iterator JavaDoc 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     /**
404      * Converts:
405      * Map (String (profile | category) > Map (String (category)) |
406      * ActionImpl)
407      * to xml.
408      * (String > Map) is represented by folder and
409      * (String > DataObject) by ShadowDO
410      */

411     private static void generateShadowsToXML (
412         LayersBridge layersBridge,
413         StringBuffer JavaDoc sb,
414         Map JavaDoc shortcutToAction,
415         String JavaDoc indentation
416     ) {
417         Iterator JavaDoc it = shortcutToAction.keySet ().iterator ();
418         while (it.hasNext ()) {
419             String JavaDoc key = (String JavaDoc) it.next ();
420             Object JavaDoc value = shortcutToAction.get (key);
421             if (value instanceof Map JavaDoc) {
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 JavaDoc) 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 JavaDoc) 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 JavaDoc openWriter () throws IOException JavaDoc {
452         JFileChooser JavaDoc fileChooser = new JFileChooser JavaDoc ();
453         int result = fileChooser.showSaveDialog
454             (WindowManager.getDefault ().getMainWindow ());
455         if (result != JFileChooser.APPROVE_OPTION) return null;
456         File JavaDoc f = fileChooser.getSelectedFile ();
457         return new FileWriter JavaDoc (f);
458     }
459     
460     private static String JavaDoc loc (String JavaDoc key) {
461         return NbBundle.getMessage (ExportShortcutsAction.class, key);
462     }
463 }
464
465
Popular Tags