KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.MissingResourceException JavaDoc;
32 import java.util.Set JavaDoc;
33 import java.util.StringTokenizer JavaDoc;
34 import javax.swing.Action JavaDoc;
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 /**
48  * Bridge to old layers based system.
49  *
50  * @author Jan Jancura
51  */

52 public class LayersBridge {
53     
54     static final String JavaDoc KEYMAPS_FOLDER = "Keymaps";
55     private static final String JavaDoc SHORTCUTS_FOLDER = "Shortcuts";
56     private static final String JavaDoc TOOLBARS_FOLDER = "Toolbars";
57     
58     /** Map (GlobalAction > DataObject). */
59     private Map JavaDoc actionToDataObject = new HashMap JavaDoc ();
60     /** Map (String (folderName) > Set (GlobalAction)). */
61     private Map JavaDoc categoryToActions;
62     /** Set (GlobalAction). */
63     private Set JavaDoc actions = new HashSet JavaDoc ();
64     
65     
66     /**
67      * Returns Map (String (folderName) > Set (GlobalAction)).
68      */

69     Map JavaDoc getActions () {
70         if (categoryToActions == null) {
71             categoryToActions = new HashMap JavaDoc ();
72             initActions ("OptionsDialog/Actions", null); // NOI18N
73
initActions (
74                 "Actions",
75                 NbBundle.getMessage (LayersBridge.class, "CTL_Other")
76             );
77             categoryToActions.remove ("Hidden"); // NOI18N
78
categoryToActions = Collections.unmodifiableMap (categoryToActions);
79         }
80         return categoryToActions;
81     }
82
83     private void initActions (String JavaDoc folder, String JavaDoc 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 JavaDoc 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 JavaDoc folderName,
99         String JavaDoc category
100     ) {
101         
102         // 1) reslove name
103
String JavaDoc name = folder.getName ();
104         if (category != null)
105             name = category;
106         else {
107             String JavaDoc bundleName = (String JavaDoc) 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 JavaDoc ex) {
115                     ErrorManager.getDefault ().notify (ex);
116                 }
117             if (folderName != null)
118                 name = folderName + '/' + name;
119         }
120         
121         Enumeration JavaDoc 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             // add to actions (Map (String (folderName) > Set (GlobalAction))).
134
Set JavaDoc a = (Set JavaDoc) categoryToActions.get (name);
135             if (a == null) {
136                 a = new HashSet JavaDoc ();
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 JavaDoc keymapNames;
149     
150    List JavaDoc getProfiles () {
151         if (keymapNames == null) {
152             DataFolder root = getRootFolder (KEYMAPS_FOLDER, null);
153             Enumeration JavaDoc en = root.children (false);
154             keymapNames = new ArrayList JavaDoc ();
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     /** Map (String (profile) > Map (GlobalAction > Set (String (shortcut)))). */
167     private Map JavaDoc keymaps = new HashMap JavaDoc ();
168     
169     /**
170      * Returns Map (GlobalAction > Set (String (shortcut))).
171      */

172     Map JavaDoc getKeymap (String JavaDoc profile) {
173         if (!keymaps.containsKey (profile)) {
174             DataFolder root = getRootFolder (SHORTCUTS_FOLDER, null);
175             Map JavaDoc 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 JavaDoc) keymaps.get (profile));
181     }
182     
183     /** Map (String (profile) > Map (GlobalAction > Set (String (shortcut)))). */
184     private Map JavaDoc keymapDefaults = new HashMap JavaDoc ();
185     
186     /**
187      * Returns Map (GlobalAction > Set (String (shortcut))).
188      */

189     Map JavaDoc getKeymapDefaults (String JavaDoc profile) {
190         if (!keymapDefaults.containsKey (profile)) {
191             DataFolder root = getRootFolder (SHORTCUTS_FOLDER, null);
192             Map JavaDoc 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 JavaDoc) keymapDefaults.get (profile));
198     }
199     
200     DataObject getDataObject (Object JavaDoc action) {
201         return (DataObject) actionToDataObject.get (action);
202     }
203     
204     /**
205      * Read keymap from one folder Map (GlobalAction > Set (String (shortcut))).
206      */

207     private Map JavaDoc readKeymap (DataFolder root) {
208         Map JavaDoc keymap = new HashMap JavaDoc ();
209         if (root == null) return keymap;
210         Enumeration JavaDoc 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 JavaDoc shortcut = dataObject.getName ();
217             Set JavaDoc s = (Set JavaDoc) keymap.get (action);
218             if (s == null) {
219                 s = new HashSet JavaDoc ();
220                 keymap.put (action, s);
221             }
222             s.add (shortcut);
223         }
224         return keymap;
225     }
226
227     void deleteProfile (String JavaDoc 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 JavaDoc ex) {
237             ErrorManager.getDefault ().notify (ex);
238         }
239     }
240     
241     // actionToShortcuts Map (GlobalAction > Set (String (shortcut))
242
void saveKeymap (String JavaDoc profile, Map JavaDoc actionToShortcuts) {
243     
244         // 1) get / create Keymaps/Profile folder
245
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 JavaDoc 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 JavaDoc actionToShortcuts, boolean add) {
262         // 2) convert to: Map (String (shortcut AC-C X) > GlobalAction)
263
Map JavaDoc shortcutToAction = shortcutToAction (actionToShortcuts);
264         
265         // 3) delete obsolete DataObjects
266
Enumeration JavaDoc 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                     // shortcut already saved
275
shortcutToAction.remove (dataObject.getName ());
276                     continue;
277                 }
278             }
279             // obsolete shortcut
280
try {
281                 dataObject.delete ();
282             } catch (IOException JavaDoc ex) {
283                 ex.printStackTrace ();
284             }
285         }
286         
287         // 4) add new shortcuts
288
if (!add) return;
289         Iterator JavaDoc it = shortcutToAction.keySet ().iterator ();
290         while (it.hasNext ()) {
291             String JavaDoc shortcut = (String JavaDoc) 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 JavaDoc ex) {
302                 ex.printStackTrace ();
303                 continue;
304             }
305         }
306     }
307
308     private static DataFolder getRootFolder (String JavaDoc name1, String JavaDoc 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 JavaDoc ex) {
321             ErrorManager.getDefault ().notify (ex);
322             return null;
323         }
324     }
325
326     /**
327      * Returns instance of GlobalAction encapsulating action, or null.
328      */

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 JavaDoc action = ic.instanceCreate ();
335             if (action == null) return null;
336             if (!(action instanceof Action JavaDoc)) return null;
337             return new GlobalAction ((Action JavaDoc) action);
338         } catch (Exception JavaDoc ex) {
339             ex.printStackTrace ();
340             return null;
341         }
342     }
343     
344     /**
345      * converts: actionToShortcuts: Map (ActionImpl > Set (String (shortcut AC-C X)))
346      * to: Map (String (shortcut AC-C X) > GlobalAction).
347      * removes all non GlobalAction actions.
348      */

349     static Map JavaDoc shortcutToAction (Map JavaDoc actionToShortcuts) {
350         Map JavaDoc shortcutToAction = new HashMap JavaDoc ();
351         Iterator JavaDoc it = actionToShortcuts.keySet ().iterator ();
352         while (it.hasNext ()) {
353             ActionImpl action = (ActionImpl) it.next ();
354             Set JavaDoc shortcuts = (Set JavaDoc) actionToShortcuts.get (action);
355             if (action instanceof CompoundAction)
356                 action = ((CompoundAction) action).getLayersAction ();
357             if (!(action instanceof GlobalAction)) continue;
358             Iterator JavaDoc it2 = shortcuts.iterator ();
359             while (it2.hasNext ()) {
360                 String JavaDoc multiShortcut = (String JavaDoc) it2.next ();
361                 shortcutToAction.put (multiShortcut, action);
362             }
363         }
364         return shortcutToAction;
365     }
366     
367     private static String JavaDoc[] toArray (String JavaDoc multiShortcut) {
368         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc (multiShortcut, " ");
369         List JavaDoc result = new ArrayList JavaDoc ();
370         while (st.hasMoreTokens ()) {
371             String JavaDoc 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 JavaDoc[]) result.toArray (new String JavaDoc [result.size ()]);
380     }
381     
382     
383     private static class GlobalAction implements ActionImpl {
384         private Action JavaDoc action;
385         private String JavaDoc name;
386         private String JavaDoc id;
387         
388         private GlobalAction (Action JavaDoc a) {
389             action = a;
390         }
391         
392         public String JavaDoc getDisplayName () {
393             if (name == null) {
394                 name = (String JavaDoc) 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 JavaDoc getId () {
402             if (id == null)
403                 id = action.getClass ().getName ();
404             return id;
405         }
406         
407         public String JavaDoc getDelegatingActionId () {
408             return null;
409         }
410         
411         public boolean equals (Object JavaDoc 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 JavaDoc toString () {
421             return super.toString () + ":" + getDisplayName ();
422         }
423     }
424 }
425
Popular Tags