KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.modules.options.keymap;
21
22 import java.util.Collection JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.logging.Level JavaDoc;
31 import java.util.logging.Logger JavaDoc;
32
33 /**
34  *
35  * @author Jan Jancura
36  */

37 public class KeymapModel {
38
39     private static final Logger JavaDoc LOG = Logger.getLogger(KeymapModel.class.getName ());
40     
41     private LayersBridge layersBridge = new LayersBridge ();
42     private EditorBridge editorBridge = new EditorBridge ();
43                                     
44     // actions .................................................................
45

46     public Set JavaDoc /*<String>*/ getActionCategories () {
47         Set JavaDoc result = new HashSet JavaDoc ();
48         result.addAll (layersBridge.getActions ().keySet ());
49         result.addAll (editorBridge.getActions ().keySet ());
50         return Collections.unmodifiableSet (result);
51     }
52     
53     /** Map (String (category name) > Set (ActionImpl)). */
54     private Map JavaDoc categoryToActions = new HashMap JavaDoc ();
55     
56     /**
57      * Returns List (ActionImpl) of all global and editor actions.
58      */

59     public Set JavaDoc getActions (String JavaDoc category) {
60         if (!categoryToActions.containsKey (category)) {
61             Set JavaDoc actions = new HashSet JavaDoc ();
62             Set JavaDoc s = (Set JavaDoc) layersBridge.getActions ().get (category);
63             if (s != null) actions.addAll (s);
64             s = (Set JavaDoc) editorBridge.getActions ().get (category);
65             if (s != null)
66                 actions = mergeActions (s, actions);
67             categoryToActions.put (category, actions);
68         }
69         return (Set JavaDoc) categoryToActions.get (category);
70     }
71
72     /**
73      * Clear action caches.
74      */

75     public void refreshActions () {
76         categoryToActions = new HashMap JavaDoc ();
77         editorBridge.refreshActions ();
78     }
79     
80     // keymaps .................................................................
81

82     public String JavaDoc getCurrentProfile () {
83         return editorBridge.getCurrentProfile ();
84     }
85     
86     public void setCurrentProfile (String JavaDoc profile) {
87         //layersBridge.setCurrentProfile (profile); !!!!!!!!!!!!!
88
editorBridge.setCurrentProfile (profile);
89     }
90     
91     public List JavaDoc getProfiles () {
92         return layersBridge.getProfiles ();
93     }
94     
95     public boolean isCustomProfile (String JavaDoc profile) {
96         return editorBridge.isCustomProfile (profile);
97     }
98     
99     /** Map (String (profile) > Map (ActionImpl > Set (String (shortcut AS-M)))). */
100     private Map JavaDoc keyMaps = new HashMap JavaDoc ();
101     
102     /**
103      * Returns Map (ActionImpl > Set (String (shortcut))).
104      */

105     public Map JavaDoc getKeymap (String JavaDoc profile) {
106         if (!keyMaps.containsKey (profile)) {
107             keyMaps.put (
108                 profile,
109                 mergeShortcuts (
110                     (Map JavaDoc) editorBridge.readKeymap (profile),
111                     (Map JavaDoc) layersBridge.getKeymap (profile)
112                 )
113             );
114         }
115         return (Map JavaDoc) keyMaps.get (profile);
116     }
117     
118     /** Map (String (keymap name) > Map (ActionImpl > Set (String (shortcut AS-M)))). */
119     private Map JavaDoc keyMapDefaults = new HashMap JavaDoc ();
120     
121     /**
122      * Returns Map (ActionImpl > Set (String (shortcut))).
123      */

124     public Map JavaDoc getKeymapDefaults (String JavaDoc profile) {
125         if (!keyMapDefaults.containsKey (profile)) {
126             keyMapDefaults.put (
127                 profile,
128                 mergeShortcuts (
129                     (Map JavaDoc) editorBridge.readKeymapDefaults(profile),
130                     (Map JavaDoc) layersBridge.getKeymapDefaults(profile)
131                 )
132             );
133         }
134         return (Map JavaDoc) keyMapDefaults.get (profile);
135     }
136     
137     public void deleteProfile (String JavaDoc profile) {
138         layersBridge.deleteProfile (profile);
139         editorBridge.deleteProfile (profile);
140     }
141     
142     /**
143      * Defines new shortcuts for some actions in given keymap.
144      * Map (ActionImpl > Set (String (shortcut AS-M P)).
145      */

146     public void changeKeymap (String JavaDoc profile, Map JavaDoc actionToShortcuts) {
147         log ("changeKeymap.actionToShortcuts", actionToShortcuts.entrySet ());
148         
149         // 1) mix changes with current keymap and put them to cached current shortcuts
150
Map JavaDoc m = new HashMap JavaDoc (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     // private methods .........................................................
161

162     private void log(String JavaDoc name, Collection JavaDoc items) {
163         if (!LOG.isLoggable(Level.FINE)) return;
164         
165         LOG.fine(name);
166         for(Iterator JavaDoc i = items.iterator(); i.hasNext(); ) {
167             Object JavaDoc item = i.next();
168             LOG.fine(" " + item);
169         }
170     }
171     
172     private Map JavaDoc sharedActions = new HashMap JavaDoc ();
173     
174     /**
175      * Merges editor actions and layers actions. Creates CompoundAction for
176      * actions like Copy, registerred to both contexts.
177      */

178     private Set JavaDoc mergeActions (
179         Collection JavaDoc editorActions,
180         Collection JavaDoc layersActions
181     ) {
182         Set JavaDoc result = new HashSet JavaDoc ();
183         Map JavaDoc idToAction = new HashMap JavaDoc ();
184         if (editorActions != null) {
185             Iterator JavaDoc it = editorActions.iterator ();
186             while (it.hasNext ()) {
187                 ActionImpl action = (ActionImpl) it.next ();
188                 String JavaDoc 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 JavaDoc it = layersActions.iterator ();
198             while (it.hasNext ()) {
199                 ActionImpl layersAction = (ActionImpl) it.next ();
200                 String JavaDoc 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, // editor action
208
layersAction // layers action
209
);
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     /**
223      * Merges editor actions and layers actions. Creates CompoundAction for
224      * actions like Copy, registerred to both contexts.
225      */

226     private Map JavaDoc mergeShortcuts (
227         Map JavaDoc editorActions,
228         Map JavaDoc layersActions
229     ) {
230         Map JavaDoc result = new HashMap JavaDoc ();
231         Iterator JavaDoc it = editorActions.keySet ().iterator ();
232         while (it.hasNext ()) {
233             ActionImpl action = (ActionImpl) it.next ();
234             Set JavaDoc shortcuts = (Set JavaDoc) 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 JavaDoc shortcuts = (Set JavaDoc) 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         // HACK - loads all actions. othervise during second open of Options
252
// Dialog (after cancel) map of sharedActions is not initialized.
253
Iterator JavaDoc it = getActionCategories ().iterator ();
254         while (it.hasNext ())
255             getActions ((String JavaDoc) it.next ());
256     }
257 }
258
Popular Tags