1 19 20 package org.netbeans.core; 21 22 import java.io.IOException ; 23 import java.util.Enumeration ; 24 import java.util.logging.Level ; 25 import java.util.logging.Logger ; 26 import javax.swing.Action ; 27 import javax.swing.KeyStroke ; 28 import javax.swing.text.Keymap ; 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 48 class ShortcutsFolder { 49 50 private static final String PROFILES_FOLDER = "Keymaps"; 51 private static final String SHORTCUTS_FOLDER = "Shortcuts"; 52 private static final String 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 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 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 NbKeymap keymap = (NbKeymap) Lookup.getDefault ().lookup (Keymap .class); 94 keymap.removeBindings (); 95 96 readShortcuts (keymap, shortcutsFileObject); 98 99 String keymapName = (String ) profilesFileObject.getAttribute 101 (CURRENT_PROFILE_ATTRIBUTE); 102 if (keymapName == null || "".equals (keymapName)) 103 keymapName = "NetBeans"; 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 ioe) { 112 Exceptions.printStackTrace(ioe); 113 } 114 } 115 if (currentFolder != null) { 116 readShortcuts (keymap, currentFolder); 117 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 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 action = (Action ) ic.instanceCreate (); 135 String shortcuts = dataObject.getName (); 136 debug.fine(" " + shortcuts + " : " + action); 137 KeyStroke [] keyStrokes = Utilities.stringToKeys (shortcuts); 138 if (keyStrokes != null) { 139 addShortcut(keymap, action, keyStrokes); 140 } else { debug.warning("Unrecognized shortcut name from " + dataObject.getPrimaryFile().getPath()); } 143 } catch (ClassNotFoundException x) { 144 Logger.getLogger(ShortcutsFolder.class.getName()).log(Level.WARNING, 145 "{0} ignored; cannot load class {1}", 146 new Object [] {dataObject.getPrimaryFile().getPath(), ic.instanceName()}); 147 } catch (Exception 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 action, 156 KeyStroke [] keyStrokes 157 ) { 158 Keymap currentKeymap = keymap; 159 int i, k = keyStrokes.length - 1; 160 for (i = 0; i < k; i++) { 161 Action 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 { 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 |