KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > ShortcutsFolder


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.core;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26 import javax.swing.Action JavaDoc;
27 import javax.swing.KeyStroke JavaDoc;
28 import javax.swing.text.Keymap JavaDoc;
29 import org.netbeans.core.NbKeymap.KeymapAction;
30 import org.openide.cookies.InstanceCookie;
31 import org.openide.filesystems.FileAttributeEvent;
32 import org.openide.filesystems.FileChangeAdapter;
33 import org.openide.filesystems.FileEvent;
34 import org.openide.filesystems.FileObject;
35 import org.openide.filesystems.Repository;
36 import org.openide.loaders.DataFolder;
37 import org.openide.loaders.DataObject;
38 import org.openide.util.Exceptions;
39 import org.openide.util.Lookup;
40 import org.openide.util.RequestProcessor;
41 import org.openide.util.Utilities;
42
43 /**
44  * Bridge to old layers based system.
45  *
46  * @author Jan Jancura
47  */

48 class ShortcutsFolder {
49     
50     private static final String JavaDoc PROFILES_FOLDER = "Keymaps";
51     private static final String JavaDoc SHORTCUTS_FOLDER = "Shortcuts";
52     private static final String JavaDoc CURRENT_PROFILE_ATTRIBUTE = "currentKeymap";
53
54     private static ShortcutsFolder shortcutsFolder;
55     private Listener listener = new Listener ();
56     private FileObject profilesFileObject;
57     private FileObject shortcutsFileObject;
58     private FileObject currentFolder;
59     private Logger JavaDoc debug = Logger.getLogger(ShortcutsFolder.class.getName ());
60     
61     
62     static void initShortcuts () {
63         if (shortcutsFolder != null) return;
64         shortcutsFolder = new ShortcutsFolder ();
65     }
66     
67     private ShortcutsFolder () {
68         try {
69             FileObject root = Repository.getDefault ().
70                 getDefaultFileSystem ().getRoot ();
71             profilesFileObject = root.getFileObject (PROFILES_FOLDER);
72             if (profilesFileObject == null)
73                 profilesFileObject = root.createFolder (PROFILES_FOLDER);
74             profilesFileObject.addFileChangeListener (listener);
75             
76             shortcutsFileObject = root.getFileObject (SHORTCUTS_FOLDER);
77             if (shortcutsFileObject == null)
78                 shortcutsFileObject = root.createFolder (SHORTCUTS_FOLDER);
79             shortcutsFileObject.addFileChangeListener (listener);
80         } catch (IOException JavaDoc ex) {
81             Exceptions.printStackTrace(ex);
82         }
83         refresh ();
84     }
85     
86     static void waitFinished () {
87         shortcutsFolder.listener.task.waitFinished ();
88     }
89     
90     private void refresh () {
91         
92         // get keymap and delete old shortcuts
93
NbKeymap keymap = (NbKeymap) Lookup.getDefault ().lookup (Keymap JavaDoc.class);
94         keymap.removeBindings ();
95
96         // update main shortcuts
97
readShortcuts (keymap, shortcutsFileObject);
98         
99         // update shortcuts from profile
100
String JavaDoc keymapName = (String JavaDoc) profilesFileObject.getAttribute
101             (CURRENT_PROFILE_ATTRIBUTE);
102         if (keymapName == null || "".equals (keymapName))
103             keymapName = "NetBeans"; // NOI18N
104
if (currentFolder != null)
105             currentFolder.removeFileChangeListener (listener);
106         currentFolder = Repository.getDefault ().getDefaultFileSystem ().
107             getRoot ().getFileObject (PROFILES_FOLDER + '/' + keymapName);
108         if (currentFolder == null) {
109             try {
110                 currentFolder = profilesFileObject.createFolder(keymapName);
111             } catch (IOException JavaDoc ioe) {
112                 Exceptions.printStackTrace(ioe);
113             }
114         }
115         if (currentFolder != null) {
116             readShortcuts (keymap, currentFolder);
117             // add listener to current profile folder
118
currentFolder.addFileChangeListener (listener);
119         }
120     }
121     
122     
123     private void readShortcuts (NbKeymap keymap, FileObject fileObject) {
124         debug.fine("\nreadShortcuts " + fileObject);
125         DataFolder folder = DataFolder.findFolder (fileObject);
126         Enumeration JavaDoc en = folder.children (false);
127         while (en.hasMoreElements ()) {
128             DataObject dataObject = (DataObject) en.nextElement ();
129             if (dataObject instanceof DataFolder) continue;
130             InstanceCookie ic = (InstanceCookie) dataObject.getCookie
131                 (InstanceCookie.class);
132             if (ic == null) continue;
133             try {
134                 Action JavaDoc action = (Action JavaDoc) ic.instanceCreate ();
135                 String JavaDoc shortcuts = dataObject.getName ();
136                 debug.fine(" " + shortcuts + " : " + action);
137                 KeyStroke JavaDoc[] keyStrokes = Utilities.stringToKeys (shortcuts);
138                 if (keyStrokes != null) {
139                     addShortcut(keymap, action, keyStrokes);
140                 } else { // see e.g. secondary exception in #74169
141
debug.warning("Unrecognized shortcut name from " + dataObject.getPrimaryFile().getPath()); // NOI18N
142
}
143             } catch (ClassNotFoundException JavaDoc x) {
144                 Logger.getLogger(ShortcutsFolder.class.getName()).log(Level.WARNING,
145                         "{0} ignored; cannot load class {1}",
146                         new Object JavaDoc[] {dataObject.getPrimaryFile().getPath(), ic.instanceName()});
147             } catch (Exception JavaDoc ex) {
148                 Logger.getLogger(ShortcutsFolder.class.getName()).log(Level.WARNING, null, ex);
149             }
150         }
151     }
152         
153     private static void addShortcut (
154         NbKeymap keymap,
155         Action JavaDoc action,
156         KeyStroke JavaDoc[] keyStrokes
157     ) {
158         Keymap JavaDoc currentKeymap = keymap;
159         int i, k = keyStrokes.length - 1;
160         for (i = 0; i < k; i++) {
161             Action JavaDoc a = currentKeymap.getAction (keyStrokes [i]);
162             if (a == null) {
163                 a = keymap.createMapAction
164                     (new NbKeymap.SubKeymap (null), keyStrokes [i]);
165                 currentKeymap.addActionForKeyStroke (keyStrokes [i], a);
166             }
167             if (!(a instanceof KeymapAction)) return;
168             currentKeymap = ((KeymapAction) a).getSubMap ();
169         }
170         currentKeymap.addActionForKeyStroke (keyStrokes [k], action);
171     }
172     
173     private class Listener extends FileChangeAdapter implements Runnable JavaDoc {
174         
175         private RequestProcessor.Task task = new RequestProcessor ("ShortcutsFolder").create (this);
176         
177         public void run () {
178             refresh ();
179         }
180         
181         public void fileDataCreated (FileEvent fe) {
182             task.schedule (500);
183         }
184
185         public void fileChanged (FileEvent fe) {
186             task.schedule (500);
187         }
188
189         public void fileDeleted (FileEvent fe) {
190             task.schedule (500);
191         }
192         
193         public void fileAttributeChanged (FileAttributeEvent fe) {
194             if (fe.getName () != null &&
195                 !CURRENT_PROFILE_ATTRIBUTE.equals (fe.getName ())
196             ) return;
197             task.schedule (500);
198         }
199     }
200 }
201
Popular Tags