Lines 43-60
Link Here
|
43 |
|
43 |
|
44 |
import java.awt.event.ActionEvent; |
44 |
import java.awt.event.ActionEvent; |
45 |
import java.awt.event.KeyEvent; |
45 |
import java.awt.event.KeyEvent; |
|
|
46 |
import java.io.IOException; |
46 |
import java.util.ArrayList; |
47 |
import java.util.ArrayList; |
47 |
import java.util.Arrays; |
|
|
48 |
import java.util.Collections; |
48 |
import java.util.Collections; |
49 |
import java.util.Comparator; |
49 |
import java.util.Comparator; |
50 |
import java.util.HashMap; |
50 |
import java.util.HashMap; |
51 |
import java.util.HashSet; |
|
|
52 |
import java.util.Iterator; |
53 |
import java.util.List; |
51 |
import java.util.List; |
54 |
import java.util.Map; |
52 |
import java.util.Map; |
55 |
import java.util.Map.Entry; |
|
|
56 |
import java.util.Observable; |
53 |
import java.util.Observable; |
57 |
import java.util.Set; |
54 |
import java.util.TreeMap; |
58 |
import java.util.logging.Level; |
55 |
import java.util.logging.Level; |
59 |
import java.util.logging.Logger; |
56 |
import java.util.logging.Logger; |
60 |
import javax.swing.AbstractAction; |
57 |
import javax.swing.AbstractAction; |
Lines 62-68
Link Here
|
62 |
import javax.swing.KeyStroke; |
59 |
import javax.swing.KeyStroke; |
63 |
import javax.swing.text.Keymap; |
60 |
import javax.swing.text.Keymap; |
64 |
import org.openide.awt.StatusDisplayer; |
61 |
import org.openide.awt.StatusDisplayer; |
65 |
import org.openide.util.Mutex; |
62 |
import org.openide.cookies.InstanceCookie; |
|
|
63 |
import org.openide.filesystems.FileObject; |
64 |
import org.openide.filesystems.FileUtil; |
65 |
import org.openide.loaders.DataObject; |
66 |
import org.openide.util.Exceptions; |
67 |
import org.openide.util.Utilities; |
66 |
import org.openide.util.actions.SystemAction; |
68 |
import org.openide.util.actions.SystemAction; |
67 |
import org.openide.util.lookup.ServiceProvider; |
69 |
import org.openide.util.lookup.ServiceProvider; |
68 |
|
70 |
|
Lines 76-83
Link Here
|
76 |
String name; |
78 |
String name; |
77 |
/** Parent keymap */ |
79 |
/** Parent keymap */ |
78 |
Keymap parent; |
80 |
Keymap parent; |
79 |
/** Hashtable holding KeyStroke > Action mappings */ |
|
|
80 |
Map<KeyStroke,Action> bindings; |
81 |
/** Default action */ |
81 |
/** Default action */ |
82 |
Action defaultAction; |
82 |
Action defaultAction; |
83 |
/** hash table to map (Action -> ArrayList of KeyStrokes) */ |
83 |
/** hash table to map (Action -> ArrayList of KeyStrokes) */ |
Lines 130-136
Link Here
|
130 |
NbKeymap(final String name, final Keymap parent) { |
130 |
NbKeymap(final String name, final Keymap parent) { |
131 |
this.name = name; |
131 |
this.name = name; |
132 |
this.parent = parent; |
132 |
this.parent = parent; |
133 |
bindings = new HashMap<KeyStroke,Action>(); |
|
|
134 |
} |
133 |
} |
135 |
|
134 |
|
136 |
public Action getDefaultAction() { |
135 |
public Action getDefaultAction() { |
Lines 161-167
Link Here
|
161 |
Keymap activ = this; |
160 |
Keymap activ = this; |
162 |
for (int i=0; i<ctx.length; i++) { |
161 |
for (int i=0; i<ctx.length; i++) { |
163 |
if (activ == this) { |
162 |
if (activ == this) { |
164 |
a = bindings.get(ctx[i]); |
163 |
a = find(ctx[i]); |
165 |
if ((a == null) && (parent != null)) { |
164 |
if ((a == null) && (parent != null)) { |
166 |
a = parent.getAction(ctx[i]); |
165 |
a = parent.getAction(ctx[i]); |
167 |
} |
166 |
} |
Lines 183-189
Link Here
|
183 |
} |
182 |
} |
184 |
|
183 |
|
185 |
if (activ == this) { |
184 |
if (activ == this) { |
186 |
a = bindings.get(key); |
185 |
a = find(key); |
187 |
if ((a == null) && (parent != null)) { |
186 |
if ((a == null) && (parent != null)) { |
188 |
a = parent.getAction(key); |
187 |
a = parent.getAction(key); |
189 |
} |
188 |
} |
Lines 218-304
Link Here
|
218 |
} |
217 |
} |
219 |
|
218 |
|
220 |
public KeyStroke[] getBoundKeyStrokes() { |
219 |
public KeyStroke[] getBoundKeyStrokes() { |
221 |
LOG.log(Level.FINE, "getBoundKeyStrokes"); |
220 |
assert false; |
222 |
int i = 0; |
221 |
return null; |
223 |
KeyStroke[] keys = null; |
|
|
224 |
synchronized (this) { |
225 |
keys = new KeyStroke[bindings.size()]; |
226 |
for (KeyStroke ks: bindings.keySet()) { |
227 |
keys[i++] = ks; |
228 |
} |
229 |
} |
230 |
return keys; |
231 |
} |
222 |
} |
232 |
|
223 |
|
233 |
public Action[] getBoundActions() { |
224 |
public Action[] getBoundActions() { |
234 |
LOG.log(Level.FINE, "getBoundActions"); |
225 |
assert false; |
235 |
int i = 0; |
226 |
return null; |
236 |
Action[] actionsArray = null; |
|
|
237 |
synchronized (this) { |
238 |
actionsArray = new Action[bindings.size()]; |
239 |
for (Iterator iter = bindings.values().iterator(); iter.hasNext(); ) { |
240 |
actionsArray[i++] = (Action) iter.next(); |
241 |
} |
242 |
} |
243 |
return actionsArray; |
244 |
} |
227 |
} |
245 |
|
228 |
|
246 |
public KeyStroke[] getKeyStrokesForAction(Action a) { |
229 |
public KeyStroke[] getKeyStrokesForAction(Action a) { |
247 |
LOG.log(Level.FINE, "getKeyStrokesForAction {0}", id(a)); |
230 |
LOG.log(Level.FINE, "getKeyStrokesForAction {0}", id(a)); |
248 |
|
231 |
FileObject definingFile = (FileObject) a.getValue("definingFile"); // cf. o.o.awt.Toolbar.setAccelerator |
249 |
Map<Action,List<KeyStroke>> localActions = actions; |
232 |
if (definingFile == null) { |
250 |
if (localActions == null) { |
233 |
System.err.println("XXX no defining file known for " + a); |
251 |
localActions = buildReverseMapping (); |
|
|
252 |
} |
253 |
|
254 |
List<KeyStroke> strokes = localActions.get (a); |
255 |
if (strokes != null) { |
256 |
return strokes.toArray(new KeyStroke[strokes.size ()]); |
257 |
} else { |
258 |
return new KeyStroke[0]; |
234 |
return new KeyStroke[0]; |
259 |
} |
235 |
} |
|
|
236 |
synchronized (this) { |
237 |
if (id2Stroke == null) { |
238 |
id2Stroke = new TreeMap<String,String>(); |
239 |
FileObject shortcuts = FileUtil.getConfigFile("Shortcuts"); |
240 |
for (FileObject f : shortcuts.getChildren()) { |
241 |
id2Stroke.put(idForFile(f), f.getName()); |
242 |
} |
243 |
shortcuts = FileUtil.getConfigFile("Keymaps/NetBeans"); |
244 |
for (FileObject f : shortcuts.getChildren()) { |
245 |
id2Stroke.put(idForFile(f), f.getName()); |
246 |
} |
247 |
{ // XXX for debugging |
248 |
for (Map.Entry<String,String> entry : id2Stroke.entrySet()) { |
249 |
System.err.println(entry.getValue() + " => " + entry.getKey()); |
250 |
} |
251 |
} |
252 |
} |
253 |
} |
254 |
String id = idForFile(definingFile); |
255 |
String k = id2Stroke.get(id); |
256 |
System.err.println("XXX found keystroke " + k + " for " + a + " with ID " + id); |
257 |
// XXX return all available keystrokes, or make Toolbar.setAccelerator pick the most common, e.g. C-V over PASTE |
258 |
return k != null ? new KeyStroke[] {Utilities.stringToKey(k)} : new KeyStroke[0]; |
259 |
} |
260 |
private Map<String,String> id2Stroke; |
261 |
/** |
262 |
* Traverses shadow files to origin. |
263 |
* Returns impl class name if that is obvious (common for SystemAction's); |
264 |
* else just returns file path (usual for more modern registrations). |
265 |
*/ |
266 |
private static String idForFile(FileObject f) { |
267 |
if (f.hasExt("shadow")) { |
268 |
String path = (String) f.getAttribute("originalFile"); |
269 |
f = FileUtil.getConfigFile(path); |
270 |
} |
271 |
// Cannot actually load instanceCreate methodvalue=... attribute; just want to see if it is there. |
272 |
if (f.hasExt("instance") && !Collections.list(f.getAttributes()).contains("instanceCreate")) { |
273 |
String clazz = (String) f.getAttribute("instanceClass"); |
274 |
if (clazz != null) { |
275 |
return clazz; |
276 |
} else { |
277 |
return f.getName().replace('-', '.'); |
278 |
} |
279 |
} |
280 |
return f.getPath(); |
260 |
} |
281 |
} |
261 |
|
282 |
|
262 |
private Map<Action,List<KeyStroke>> buildReverseMapping () { |
283 |
private Action find(KeyStroke key) { |
263 |
Map<Action,List<KeyStroke>> localActions = actions = new HashMap<Action,List<KeyStroke>> (); |
284 |
FileObject shortcuts = FileUtil.getConfigFile("Keymaps/NetBeans"); |
264 |
|
285 |
String nm = Utilities.keyToString(key); |
265 |
synchronized (this) { |
286 |
FileObject def = shortcuts.getFileObject(nm + ".shadow"); |
266 |
for (Map.Entry<KeyStroke,Action> curEntry: bindings.entrySet()) { |
287 |
if (def == null) { |
267 |
Action curAction = curEntry.getValue(); |
288 |
def = shortcuts.getFileObject(nm + ".instance"); |
268 |
KeyStroke curKey = curEntry.getKey(); |
289 |
} |
269 |
|
290 |
if (def == null) { |
270 |
List<KeyStroke> keysForAction = localActions.get (curAction); |
291 |
shortcuts = FileUtil.getConfigFile("Shortcuts"); |
271 |
if (keysForAction == null) { |
292 |
def = shortcuts.getFileObject(nm + ".shadow"); |
272 |
keysForAction = Collections.synchronizedList (new ArrayList<KeyStroke> (1)); |
293 |
if (def == null) { |
273 |
localActions.put (curAction, keysForAction); |
294 |
def = shortcuts.getFileObject(nm + ".instance"); |
274 |
} |
295 |
} |
275 |
keysForAction.add (curKey); |
296 |
if (def == null) { |
|
|
297 |
return null; |
276 |
} |
298 |
} |
277 |
} |
299 |
} |
278 |
|
300 |
try { |
279 |
return localActions; |
301 |
DataObject d = DataObject.find(def); |
|
|
302 |
InstanceCookie ic = d.getLookup().lookup(InstanceCookie.class); |
303 |
if (ic == null) { |
304 |
return null; |
305 |
} |
306 |
return (Action) ic.instanceCreate(); |
307 |
} catch (ClassNotFoundException x) { |
308 |
Exceptions.printStackTrace(x); |
309 |
} catch (IOException x) { |
310 |
Exceptions.printStackTrace(x); |
311 |
} |
312 |
return null; |
280 |
} |
313 |
} |
281 |
|
314 |
|
282 |
public synchronized boolean isLocallyDefined(KeyStroke key) { |
315 |
public synchronized boolean isLocallyDefined(KeyStroke key) { |
283 |
LOG.log(Level.FINE, "isLocallyDefined {0}", key); |
316 |
assert false; |
284 |
return bindings.containsKey(key); |
317 |
return false; |
285 |
} |
318 |
} |
286 |
|
319 |
|
287 |
/** Updates action accelerator. */ |
|
|
288 |
private void updateActionAccelerator(final Action a) { |
289 |
if(a == null) { |
290 |
return; |
291 |
} |
292 |
|
293 |
Mutex.EVENT.writeAccess(new Runnable() { |
294 |
public void run() { |
295 |
KeyStroke[] keystrokes = getKeyStrokesForAction(a); |
296 |
Arrays.sort (keystrokes, NbKeymap.this); |
297 |
a.putValue(Action.ACCELERATOR_KEY, keystrokes.length > 0 ? keystrokes[0] : null); |
298 |
} |
299 |
}); |
300 |
} |
301 |
|
302 |
public int compare(KeyStroke k1, KeyStroke k2) { |
320 |
public int compare(KeyStroke k1, KeyStroke k2) { |
303 |
//#47024 and 32733 - "Find" should not be shown as an accelerator, |
321 |
//#47024 and 32733 - "Find" should not be shown as an accelerator, |
304 |
//nor should "Backspace" for Delete. Solution: The shorter text wins. |
322 |
//nor should "Backspace" for Delete. Solution: The shorter text wins. |
Lines 308-377
Link Here
|
308 |
|
326 |
|
309 |
|
327 |
|
310 |
public void addActionForKeyStroke(KeyStroke key, Action a) { |
328 |
public void addActionForKeyStroke(KeyStroke key, Action a) { |
311 |
LOG.log(Level.FINE, "addActionForKeyStroke {0} => {1}", new Object[] { key, id(a) }); |
329 |
assert false; |
312 |
// Update reverse binding for old action too (#30455): |
|
|
313 |
Action old; |
314 |
synchronized (this) { |
315 |
old = bindings.put(key, a); |
316 |
actions = null; |
317 |
} |
318 |
|
319 |
updateActionAccelerator(a); |
320 |
updateActionAccelerator(old); |
321 |
setChanged(); |
322 |
notifyObservers(); |
323 |
} |
324 |
|
325 |
void addActionForKeyStrokeMap(Map<KeyStroke,Action> map) { |
326 |
Set<Action> actionsSet = new HashSet<Action>(); |
327 |
synchronized (this) { |
328 |
for (Entry<KeyStroke,Action> entry: map.entrySet ()) { |
329 |
KeyStroke key = entry.getKey(); |
330 |
Action value = entry.getValue(); |
331 |
// Add both old and new action: |
332 |
actionsSet.add(value); |
333 |
actionsSet.add(bindings.put(key, value)); |
334 |
} |
335 |
actions = null; |
336 |
} |
337 |
|
338 |
for(Action a: actionsSet) { |
339 |
updateActionAccelerator(a); |
340 |
} |
341 |
|
342 |
setChanged(); |
343 |
notifyObservers(); |
344 |
} |
330 |
} |
345 |
|
331 |
|
346 |
public void removeKeyStrokeBinding(KeyStroke key) { |
332 |
public void removeKeyStrokeBinding(KeyStroke key) { |
347 |
LOG.log(Level.FINE, "removeKeyStrokeBinding {0}", key); |
333 |
assert false; |
348 |
|
|
|
349 |
Action a; |
350 |
synchronized (this) { |
351 |
a = bindings.remove(key); |
352 |
actions = null; |
353 |
} |
354 |
updateActionAccelerator(a); |
355 |
setChanged(); |
356 |
notifyObservers(); |
357 |
} |
334 |
} |
358 |
|
335 |
|
359 |
public void removeBindings() { |
336 |
public void removeBindings() { |
360 |
LOG.log(Level.FINE, "removeBindings"); |
337 |
assert false; |
361 |
|
|
|
362 |
Set<Action> actionsSet; |
363 |
synchronized (this) { |
364 |
actionsSet = new HashSet<Action>(bindings.values()); |
365 |
bindings.clear(); |
366 |
actions = null; |
367 |
} |
368 |
|
369 |
for(Action a: actionsSet) { |
370 |
updateActionAccelerator(a); |
371 |
} |
372 |
|
373 |
setChanged(); |
374 |
notifyObservers(); |
375 |
} |
338 |
} |
376 |
|
339 |
|
377 |
public Keymap getResolveParent() { |
340 |
public Keymap getResolveParent() { |
Lines 385-397
Link Here
|
385 |
notifyObservers(); |
348 |
notifyObservers(); |
386 |
} |
349 |
} |
387 |
|
350 |
|
388 |
/** Returns string representation - can be looong. |
|
|
389 |
*/ |
390 |
@Override |
391 |
public String toString() { |
392 |
return "Keymap[" + name + "]" + bindings; // NOI18N |
393 |
} |
394 |
|
395 |
private static Object id(Action a) { |
351 |
private static Object id(Action a) { |
396 |
if (a instanceof SystemAction) { |
352 |
if (a instanceof SystemAction) { |
397 |
return a.getClass(); |
353 |
return a.getClass(); |