KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > settings > storage > KeyMapsStorage


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.editor.settings.storage;
21
22 import java.util.Arrays JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31 import javax.swing.KeyStroke JavaDoc;
32 import org.netbeans.api.editor.mimelookup.MimePath;
33 import org.netbeans.api.editor.settings.MultiKeyBinding;
34
35 import org.openide.filesystems.FileObject;
36 import org.openide.util.Utilities;
37 import org.openide.xml.XMLUtil;
38 import org.w3c.dom.Document JavaDoc;
39 import org.w3c.dom.Element JavaDoc;
40 import org.w3c.dom.Node JavaDoc;
41 import org.xml.sax.Attributes JavaDoc;
42 import org.xml.sax.InputSource JavaDoc;
43 import org.xml.sax.SAXException JavaDoc;
44
45
46 /**
47  * This class contains support static methods for loading / saving and
48  * translating coloring (fontsColors.xml) files. It calls XMLStorage utilities.
49  *
50  * @author Jan Jancura
51  */

52 public final class KeyMapsStorage {
53
54     private static final Logger JavaDoc LOG = Logger.getLogger(KeyMapsStorage.class.getName());
55
56     // XXX: These constants are package private only for EditorSettingsImpl.init*() method.
57
// Once we get rid of filenames these will be removed.
58
/* package */ static final String JavaDoc KEYBINDING_FILE_NAME = "keybindings.xml"; // NOI18N
59

60     /** The name of the folder within a profile's folder containing module installed defaults. */
61     /* package */ static final String JavaDoc DEFAULTS_FOLDER = "Defaults"; //NOI18N
62

63     private static final String JavaDoc ROOT = "bindings"; //NOI18N
64
private static final String JavaDoc E_BIND = "bind"; //NOI18N
65
private static final String JavaDoc A_ACTION_NAME = "actionName"; //NOI18N
66
private static final String JavaDoc A_KEY = "key"; //NOI18N
67
private static final String JavaDoc A_REMOVE = "remove"; //NOI18N
68
private static final String JavaDoc V_TRUE = "true"; //NOI18N
69

70     private static final String JavaDoc PUBLIC_ID = "-//NetBeans//DTD Editor KeyBindings settings 1.1//EN"; //NOI18N
71
private static final String JavaDoc SYSTEM_ID = "http://www.netbeans.org/dtds/EditorKeyBindings-1_1.dtd"; //NOI18N
72

73     private KeyMapsStorage() {
74     }
75     
76     // load ....................................................................
77

78     /**
79      * Object [Map (List (KeyStroke) > MultiKeyBinding),
80      * Set (List (KeyStroke))]
81      * [modified, removed]
82      */

83     public static Object JavaDoc[] loadKeyMaps (
84         MimePath mimePath,
85         String JavaDoc profile,
86         boolean defaults
87     ) {
88         // 1) load colorings
89
if (profile.equals(EditorSettingsImpl.DEFAULT_PROFILE)) profile = null;
90
91         String JavaDoc fileName = defaults ? DEFAULTS_FOLDER + "/" + KEYBINDING_FILE_NAME : KEYBINDING_FILE_NAME;
92         FileObject fo = Utils.getFileObject(mimePath, profile, fileName);
93         if (fo == null) {
94             return new Object JavaDoc[] {
95                 Collections.<Collection JavaDoc<KeyStroke JavaDoc>, MultiKeyBinding>emptyMap(),
96                 Collections.<Collection JavaDoc<KeyStroke JavaDoc>>emptySet()
97             };
98         } else {
99             return (Object JavaDoc[]) XMLStorage.load(fo, new KeyMapsReader());
100         }
101     }
102     
103     private static class KeyMapsReader extends XMLStorage.Handler {
104         private Map JavaDoc<Collection JavaDoc<KeyStroke JavaDoc>, MultiKeyBinding> keyMap = new HashMap JavaDoc<Collection JavaDoc<KeyStroke JavaDoc>, MultiKeyBinding>();
105         private Set JavaDoc<Collection JavaDoc<KeyStroke JavaDoc>> removedShortcuts = new HashSet JavaDoc<Collection JavaDoc<KeyStroke JavaDoc>>();
106         
107         public Object JavaDoc getResult() {
108             return new Object JavaDoc[] {keyMap, removedShortcuts};
109         }
110         
111         public void startElement(
112             String JavaDoc uri,
113             String JavaDoc localName,
114             String JavaDoc name,
115             Attributes JavaDoc attributes
116         ) throws SAXException JavaDoc {
117             try {
118                 if (name.equals(ROOT)) {
119                     // We don't read anything from the root element
120

121                 } else if (name.equals(E_BIND)) {
122                     String JavaDoc actionName = attributes.getValue(A_ACTION_NAME);
123                     String JavaDoc key = attributes.getValue(A_KEY);
124                     
125                     if (!Utilities.isMac() &&
126                         isModuleFile() && key != null && key.length() > 0)
127                     {
128                         // check the key, it should never start with 'A' or 'C', because
129
// these characters do not work on MAC, Alt should be coded as 'O'
130
// and Ctrl as 'D'
131
int idx = key.indexOf('-');
132                         if (idx != -1 && (key.charAt(0) == 'A' || key.charAt(0) == 'C')) {
133                             LOG.warning("The keybinding '" + key + "' for action '" + actionName + //NOI18N
134
"' in " + getProcessedFile().getPath() + " may not work correctly on Mac. " + //NOI18N
135
"Keybindings starting with Alt or Ctrl should " + //NOI18N
136
"be coded with latin capital letters 'O' " + //NOI18N
137
"or 'D' respectively. For details see org.openide.util.Utilities.stringToKey()."); //NOI18N
138
}
139                     }
140                     
141                     KeyStroke JavaDoc[] shortcut = Utils.stringToKeyStrokes(key);
142                     String JavaDoc remove = attributes.getValue(A_REMOVE);
143                     
144                     if (Boolean.valueOf(remove)) {
145                         removedShortcuts.add(Arrays.asList(shortcut));
146                     } else {
147                         MultiKeyBinding mkb = new MultiKeyBinding(shortcut, actionName);
148                         keyMap.put(Arrays.asList(shortcut), mkb);
149                     }
150                 }
151             } catch (Exception JavaDoc ex) {
152                 LOG.log(Level.WARNING, "Can't parse keybindings file " + getProcessedFile().getPath(), ex); //NOI18N
153
}
154         }
155         
156         public InputSource JavaDoc resolveEntity(String JavaDoc pubid, String JavaDoc sysid) {
157             return new InputSource JavaDoc(
158                 new java.io.ByteArrayInputStream JavaDoc(new byte [0])
159             );
160         }
161     } // End of KeyMapsReader class
162

163     
164     // delete ..........................................................
165

166     public static void deleteProfile(
167         MimePath mimePath,
168         String JavaDoc profile,
169         boolean defaults
170     ) {
171         if (profile.equals(EditorSettingsImpl.DEFAULT_PROFILE)) {
172             profile = null;
173         }
174
175         String JavaDoc fileName = defaults ? DEFAULTS_FOLDER + "/" + KEYBINDING_FILE_NAME : KEYBINDING_FILE_NAME;
176         Utils.deleteFileObject(mimePath, profile, fileName);
177     }
178     
179     // save ..........................................................
180

181     public static void saveKeyMaps (
182         MimePath mimePath,
183         String JavaDoc profile,
184         boolean defaults,
185         Collection JavaDoc<MultiKeyBinding> keyMap, // modified shortcuts
186
Set JavaDoc<Collection JavaDoc<KeyStroke JavaDoc>> removed // shortcuts
187
) {
188         if (profile.equals(EditorSettingsImpl.DEFAULT_PROFILE)) {
189             profile = null;
190         }
191         
192         String JavaDoc fileName = defaults ? DEFAULTS_FOLDER + "/" + KEYBINDING_FILE_NAME : KEYBINDING_FILE_NAME;
193         FileObject fo = Utils.createFileObject(mimePath, profile, fileName);
194         saveKeyMaps (fo, keyMap, removed);
195     }
196     
197     private static void saveKeyMaps(
198         FileObject fo,
199         Collection JavaDoc<MultiKeyBinding> keyMap,
200         Set JavaDoc<Collection JavaDoc<KeyStroke JavaDoc>> removed
201         ) {
202         Document JavaDoc doc = XMLUtil.createDocument(ROOT, null, PUBLIC_ID, SYSTEM_ID);
203         Node JavaDoc root = doc.getElementsByTagName(ROOT).item(0);
204         
205         for(MultiKeyBinding mkb : keyMap) {
206             Element JavaDoc bind = doc.createElement(E_BIND);
207             root.appendChild(bind);
208             
209             bind.setAttribute(A_ACTION_NAME, mkb.getActionName());
210             bind.setAttribute(A_KEY, Utils.keyStrokesToString(mkb.getKeyStrokeList()));
211         }
212         
213         for(Collection JavaDoc<KeyStroke JavaDoc> keyStrokes : removed) {
214             String JavaDoc shortcut = Utils.keyStrokesToString(keyStrokes);
215             Element JavaDoc bind = doc.createElement(E_BIND);
216             root.appendChild(bind);
217             
218             bind.setAttribute(A_KEY, shortcut);
219             bind.setAttribute(A_REMOVE, V_TRUE);
220         }
221         
222         XMLStorage.save(fo, doc);
223     }
224 }
225
Popular Tags