Lines 43-66
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 javax.swing.AbstractAction; |
55 |
import javax.swing.AbstractAction; |
59 |
import javax.swing.Action; |
56 |
import javax.swing.Action; |
60 |
import javax.swing.KeyStroke; |
57 |
import javax.swing.KeyStroke; |
61 |
import javax.swing.text.Keymap; |
58 |
import javax.swing.text.Keymap; |
62 |
import org.openide.awt.StatusDisplayer; |
59 |
import org.openide.awt.StatusDisplayer; |
63 |
import org.openide.util.Mutex; |
60 |
import org.openide.cookies.InstanceCookie; |
|
|
61 |
import org.openide.filesystems.FileObject; |
62 |
import org.openide.filesystems.Repository; |
63 |
import org.openide.loaders.DataObject; |
64 |
import org.openide.util.Exceptions; |
65 |
import org.openide.util.Utilities; |
64 |
import org.openide.util.lookup.ServiceProvider; |
66 |
import org.openide.util.lookup.ServiceProvider; |
65 |
|
67 |
|
66 |
/** Implementation of standard key - action mappings. |
68 |
/** Implementation of standard key - action mappings. |
Lines 74-80
Link Here
|
74 |
/** Parent keymap */ |
76 |
/** Parent keymap */ |
75 |
Keymap parent; |
77 |
Keymap parent; |
76 |
/** Hashtable holding KeyStroke > Action mappings */ |
78 |
/** Hashtable holding KeyStroke > Action mappings */ |
77 |
Map<KeyStroke,Action> bindings; |
79 |
//Map<KeyStroke,Action> bindings; |
78 |
/** Default action */ |
80 |
/** Default action */ |
79 |
Action defaultAction; |
81 |
Action defaultAction; |
80 |
/** hash table to map (Action -> ArrayList of KeyStrokes) */ |
82 |
/** hash table to map (Action -> ArrayList of KeyStrokes) */ |
Lines 126-132
Link Here
|
126 |
NbKeymap(final String name, final Keymap parent) { |
128 |
NbKeymap(final String name, final Keymap parent) { |
127 |
this.name = name; |
129 |
this.name = name; |
128 |
this.parent = parent; |
130 |
this.parent = parent; |
129 |
bindings = new HashMap<KeyStroke,Action>(); |
131 |
// bindings = new HashMap<KeyStroke,Action>(); |
130 |
} |
132 |
} |
131 |
|
133 |
|
132 |
public Action getDefaultAction() { |
134 |
public Action getDefaultAction() { |
Lines 153-159
Link Here
|
153 |
Keymap activ = this; |
155 |
Keymap activ = this; |
154 |
for (int i=0; i<ctx.length; i++) { |
156 |
for (int i=0; i<ctx.length; i++) { |
155 |
if (activ == this) { |
157 |
if (activ == this) { |
156 |
a = bindings.get(ctx[i]); |
158 |
// a = bindings.get(ctx[i]); |
|
|
159 |
a = find(ctx[i]); |
157 |
if ((a == null) && (parent != null)) { |
160 |
if ((a == null) && (parent != null)) { |
158 |
a = parent.getAction(ctx[i]); |
161 |
a = parent.getAction(ctx[i]); |
159 |
} |
162 |
} |
Lines 175-181
Link Here
|
175 |
} |
178 |
} |
176 |
|
179 |
|
177 |
if (activ == this) { |
180 |
if (activ == this) { |
178 |
a = bindings.get(key); |
181 |
// a = bindings.get(key); |
|
|
182 |
a = find(key); |
179 |
if ((a == null) && (parent != null)) { |
183 |
if ((a == null) && (parent != null)) { |
180 |
a = parent.getAction(key); |
184 |
a = parent.getAction(key); |
181 |
} |
185 |
} |
Lines 210-290
Link Here
|
210 |
} |
214 |
} |
211 |
|
215 |
|
212 |
public KeyStroke[] getBoundKeyStrokes() { |
216 |
public KeyStroke[] getBoundKeyStrokes() { |
213 |
int i = 0; |
217 |
// int i = 0; |
214 |
KeyStroke[] keys = null; |
218 |
// KeyStroke[] keys = null; |
215 |
synchronized (this) { |
219 |
// synchronized (this) { |
216 |
keys = new KeyStroke[bindings.size()]; |
220 |
// keys = new KeyStroke[bindings.size()]; |
217 |
for (KeyStroke ks: bindings.keySet()) { |
221 |
// for (KeyStroke ks: bindings.keySet()) { |
218 |
keys[i++] = ks; |
222 |
// keys[i++] = ks; |
219 |
} |
223 |
// } |
220 |
} |
224 |
// } |
221 |
return keys; |
225 |
// return keys; |
|
|
226 |
assert false; |
227 |
return null; |
222 |
} |
228 |
} |
223 |
|
229 |
|
224 |
public Action[] getBoundActions() { |
230 |
public Action[] getBoundActions() { |
225 |
int i = 0; |
231 |
// int i = 0; |
226 |
Action[] actionsArray = null; |
232 |
// Action[] actionsArray = null; |
227 |
synchronized (this) { |
233 |
// synchronized (this) { |
228 |
actionsArray = new Action[bindings.size()]; |
234 |
// actionsArray = new Action[bindings.size()]; |
229 |
for (Iterator iter = bindings.values().iterator(); iter.hasNext(); ) { |
235 |
// for (Iterator iter = bindings.values().iterator(); iter.hasNext(); ) { |
230 |
actionsArray[i++] = (Action) iter.next(); |
236 |
// actionsArray[i++] = (Action) iter.next(); |
231 |
} |
237 |
// } |
232 |
} |
238 |
// } |
233 |
return actionsArray; |
239 |
// return actionsArray; |
|
|
240 |
assert false; |
241 |
return null; |
234 |
} |
242 |
} |
235 |
|
243 |
|
236 |
public KeyStroke[] getKeyStrokesForAction(Action a) { |
244 |
public KeyStroke[] getKeyStrokesForAction(Action a) { |
237 |
Map<Action,List<KeyStroke>> localActions = actions; |
245 |
// Map<Action,List<KeyStroke>> localActions = actions; |
238 |
if (localActions == null) { |
246 |
// if (localActions == null) { |
239 |
localActions = buildReverseMapping (); |
247 |
// localActions = buildReverseMapping (); |
240 |
} |
248 |
// } |
241 |
|
249 |
// |
242 |
List<KeyStroke> strokes = localActions.get (a); |
250 |
// List<KeyStroke> strokes = localActions.get (a); |
243 |
if (strokes != null) { |
251 |
// if (strokes != null) { |
244 |
return strokes.toArray(new KeyStroke[strokes.size ()]); |
252 |
// return strokes.toArray(new KeyStroke[strokes.size ()]); |
245 |
} else { |
253 |
// } else { |
|
|
254 |
// return new KeyStroke[0]; |
255 |
// } |
256 |
FileObject definingFile = (FileObject) a.getValue("definingFile"); // cf. o.o.awt.Toolbar.setAccelerator |
257 |
if (definingFile == null) { |
258 |
System.err.println("XXX no defining file known for " + a); |
246 |
return new KeyStroke[0]; |
259 |
return new KeyStroke[0]; |
247 |
} |
260 |
} |
|
|
261 |
synchronized (this) { |
262 |
if (id2Stroke == null) { |
263 |
id2Stroke = new TreeMap<String,String>(); |
264 |
FileObject shortcuts = Repository.getDefault().getDefaultFileSystem().findResource("Shortcuts"); |
265 |
for (FileObject f : shortcuts.getChildren()) { |
266 |
id2Stroke.put(idForFile(f), f.getName()); |
267 |
} |
268 |
shortcuts = Repository.getDefault().getDefaultFileSystem().findResource("Keymaps/NetBeans"); |
269 |
for (FileObject f : shortcuts.getChildren()) { |
270 |
id2Stroke.put(idForFile(f), f.getName()); |
271 |
} |
272 |
{ // XXX for debugging |
273 |
for (Map.Entry<String,String> entry : id2Stroke.entrySet()) { |
274 |
System.err.println(entry.getValue() + " => " + entry.getKey()); |
275 |
} |
276 |
} |
277 |
} |
278 |
} |
279 |
String id = idForFile(definingFile); |
280 |
String k = id2Stroke.get(id); |
281 |
System.err.println("XXX found keystroke " + k + " for " + a + " with ID " + id); |
282 |
// XXX return all available keystrokes, or make Toolbar.setAccelerator pick the most common, e.g. C-V over PASTE |
283 |
return k != null ? new KeyStroke[] {Utilities.stringToKey(k)} : new KeyStroke[0]; |
284 |
} |
285 |
private Map<String,String> id2Stroke; |
286 |
/** |
287 |
* Traverses shadow files to origin. |
288 |
* Returns impl class name if that is obvious (common for SystemAction's); |
289 |
* else just returns file path (usual for more modern registrations). |
290 |
*/ |
291 |
private static String idForFile(FileObject f) { |
292 |
if (f.hasExt("shadow")) { |
293 |
String path = (String) f.getAttribute("originalFile"); |
294 |
f = Repository.getDefault().getDefaultFileSystem().findResource(path); |
295 |
} |
296 |
// Cannot actually load instanceCreate methodvalue=... attribute; just want to see if it is there. |
297 |
if (f.hasExt("instance") && !Collections.list(f.getAttributes()).contains("instanceCreate")) { |
298 |
String clazz = (String) f.getAttribute("instanceClass"); |
299 |
if (clazz != null) { |
300 |
return clazz; |
301 |
} else { |
302 |
return f.getName().replace('-', '.'); |
303 |
} |
304 |
} |
305 |
return f.getPath(); |
248 |
} |
306 |
} |
249 |
|
307 |
|
250 |
private Map<Action,List<KeyStroke>> buildReverseMapping () { |
308 |
private Action find(KeyStroke key) { |
251 |
Map<Action,List<KeyStroke>> localActions = actions = new HashMap<Action,List<KeyStroke>> (); |
309 |
FileObject shortcuts = Repository.getDefault().getDefaultFileSystem().findResource("Keymaps/NetBeans"); |
252 |
|
310 |
String nm = Utilities.keyToString(key); |
253 |
synchronized (this) { |
311 |
FileObject def = shortcuts.getFileObject(nm + ".shadow"); |
254 |
for (Map.Entry<KeyStroke,Action> curEntry: bindings.entrySet()) { |
312 |
if (def == null) { |
255 |
Action curAction = curEntry.getValue(); |
313 |
def = shortcuts.getFileObject(nm + ".instance"); |
256 |
KeyStroke curKey = curEntry.getKey(); |
314 |
} |
257 |
|
315 |
if (def == null) { |
258 |
List<KeyStroke> keysForAction = localActions.get (curAction); |
316 |
shortcuts = Repository.getDefault().getDefaultFileSystem().findResource("Shortcuts"); |
259 |
if (keysForAction == null) { |
317 |
def = shortcuts.getFileObject(nm + ".shadow"); |
260 |
keysForAction = Collections.synchronizedList (new ArrayList<KeyStroke> (1)); |
318 |
if (def == null) { |
261 |
localActions.put (curAction, keysForAction); |
319 |
def = shortcuts.getFileObject(nm + ".instance"); |
262 |
} |
320 |
} |
263 |
keysForAction.add (curKey); |
321 |
if (def == null) { |
|
|
322 |
return null; |
264 |
} |
323 |
} |
265 |
} |
324 |
} |
266 |
|
325 |
try { |
267 |
return localActions; |
326 |
DataObject d = DataObject.find(def); |
|
|
327 |
InstanceCookie ic = d.getLookup().lookup(InstanceCookie.class); |
328 |
if (ic == null) { |
329 |
return null; |
330 |
} |
331 |
return (Action) ic.instanceCreate(); |
332 |
} catch (ClassNotFoundException x) { |
333 |
Exceptions.printStackTrace(x); |
334 |
} catch (IOException x) { |
335 |
Exceptions.printStackTrace(x); |
336 |
} |
337 |
return null; |
268 |
} |
338 |
} |
269 |
|
339 |
|
|
|
340 |
// private Map<Action,List<KeyStroke>> buildReverseMapping () { |
341 |
// Map<Action,List<KeyStroke>> localActions = actions = new HashMap<Action,List<KeyStroke>> (); |
342 |
// |
343 |
// synchronized (this) { |
344 |
// for (Map.Entry<KeyStroke,Action> curEntry: bindings.entrySet()) { |
345 |
// Action curAction = curEntry.getValue(); |
346 |
// KeyStroke curKey = curEntry.getKey(); |
347 |
// |
348 |
// List<KeyStroke> keysForAction = localActions.get (curAction); |
349 |
// if (keysForAction == null) { |
350 |
// keysForAction = Collections.synchronizedList (new ArrayList<KeyStroke> (1)); |
351 |
// localActions.put (curAction, keysForAction); |
352 |
// } |
353 |
// keysForAction.add (curKey); |
354 |
// } |
355 |
// } |
356 |
// |
357 |
// return localActions; |
358 |
// } |
359 |
|
270 |
public synchronized boolean isLocallyDefined(KeyStroke key) { |
360 |
public synchronized boolean isLocallyDefined(KeyStroke key) { |
271 |
return bindings.containsKey(key); |
361 |
// return bindings.containsKey(key); |
|
|
362 |
assert false; |
363 |
return false; |
272 |
} |
364 |
} |
273 |
|
365 |
|
274 |
/** Updates action accelerator. */ |
366 |
// /** Updates action accelerator. */ |
275 |
private void updateActionAccelerator(final Action a) { |
367 |
// private void updateActionAccelerator(final Action a) { |
276 |
if(a == null) { |
368 |
// if(a == null) { |
277 |
return; |
369 |
// return; |
278 |
} |
370 |
// } |
279 |
|
371 |
// |
280 |
Mutex.EVENT.writeAccess(new Runnable() { |
372 |
// Mutex.EVENT.writeAccess(new Runnable() { |
281 |
public void run() { |
373 |
// public void run() { |
282 |
KeyStroke[] keystrokes = getKeyStrokesForAction(a); |
374 |
// KeyStroke[] keystrokes = getKeyStrokesForAction(a); |
283 |
Arrays.sort (keystrokes, NbKeymap.this); |
375 |
// Arrays.sort (keystrokes, NbKeymap.this); |
284 |
a.putValue(Action.ACCELERATOR_KEY, keystrokes.length > 0 ? keystrokes[0] : null); |
376 |
// a.putValue(Action.ACCELERATOR_KEY, keystrokes.length > 0 ? keystrokes[0] : null); |
285 |
} |
377 |
// } |
286 |
}); |
378 |
// }); |
287 |
} |
379 |
// } |
288 |
|
380 |
|
289 |
public int compare(KeyStroke k1, KeyStroke k2) { |
381 |
public int compare(KeyStroke k1, KeyStroke k2) { |
290 |
//#47024 and 32733 - "Find" should not be shown as an accelerator, |
382 |
//#47024 and 32733 - "Find" should not be shown as an accelerator, |
Lines 295-359
Link Here
|
295 |
|
387 |
|
296 |
|
388 |
|
297 |
public void addActionForKeyStroke(KeyStroke key, Action a) { |
389 |
public void addActionForKeyStroke(KeyStroke key, Action a) { |
298 |
// Update reverse binding for old action too (#30455): |
390 |
// // Update reverse binding for old action too (#30455): |
299 |
Action old; |
391 |
// Action old; |
300 |
synchronized (this) { |
392 |
// synchronized (this) { |
301 |
old = bindings.put(key, a); |
393 |
// old = bindings.put(key, a); |
302 |
actions = null; |
394 |
// actions = null; |
303 |
} |
395 |
// } |
304 |
|
396 |
// |
305 |
updateActionAccelerator(a); |
397 |
// updateActionAccelerator(a); |
306 |
updateActionAccelerator(old); |
398 |
// updateActionAccelerator(old); |
307 |
setChanged(); |
399 |
// setChanged(); |
308 |
notifyObservers(); |
400 |
// notifyObservers(); |
|
|
401 |
assert false; |
309 |
} |
402 |
} |
310 |
|
403 |
|
311 |
void addActionForKeyStrokeMap(Map<KeyStroke,Action> map) { |
404 |
// void addActionForKeyStrokeMap(Map<KeyStroke,Action> map) { |
312 |
Set<Action> actionsSet = new HashSet<Action>(); |
405 |
// Set<Action> actionsSet = new HashSet<Action>(); |
313 |
synchronized (this) { |
406 |
// synchronized (this) { |
314 |
for (Entry<KeyStroke,Action> entry: map.entrySet ()) { |
407 |
// for (Entry<KeyStroke,Action> entry: map.entrySet ()) { |
315 |
KeyStroke key = entry.getKey(); |
408 |
// KeyStroke key = entry.getKey(); |
316 |
Action value = entry.getValue(); |
409 |
// Action value = entry.getValue(); |
317 |
// Add both old and new action: |
410 |
// // Add both old and new action: |
318 |
actionsSet.add(value); |
411 |
// actionsSet.add(value); |
319 |
actionsSet.add(bindings.put(key, value)); |
412 |
// actionsSet.add(bindings.put(key, value)); |
320 |
} |
413 |
// } |
321 |
actions = null; |
414 |
// actions = null; |
322 |
} |
415 |
// } |
323 |
|
416 |
// |
324 |
for(Action a: actionsSet) { |
417 |
// for(Action a: actionsSet) { |
325 |
updateActionAccelerator(a); |
418 |
// updateActionAccelerator(a); |
326 |
} |
419 |
// } |
327 |
|
420 |
// |
328 |
setChanged(); |
421 |
// setChanged(); |
329 |
notifyObservers(); |
422 |
// notifyObservers(); |
330 |
} |
423 |
// } |
331 |
|
424 |
|
332 |
public void removeKeyStrokeBinding(KeyStroke key) { |
425 |
public void removeKeyStrokeBinding(KeyStroke key) { |
333 |
Action a; |
426 |
// Action a; |
334 |
synchronized (this) { |
427 |
// synchronized (this) { |
335 |
a = bindings.remove(key); |
428 |
// a = bindings.remove(key); |
336 |
actions = null; |
429 |
// actions = null; |
337 |
} |
430 |
// } |
338 |
updateActionAccelerator(a); |
431 |
// updateActionAccelerator(a); |
339 |
setChanged(); |
432 |
// setChanged(); |
340 |
notifyObservers(); |
433 |
// notifyObservers(); |
|
|
434 |
assert false; |
341 |
} |
435 |
} |
342 |
|
436 |
|
343 |
public void removeBindings() { |
437 |
public void removeBindings() { |
344 |
Set<Action> actionsSet; |
438 |
// Set<Action> actionsSet; |
345 |
synchronized (this) { |
439 |
// synchronized (this) { |
346 |
actionsSet = new HashSet<Action>(bindings.values()); |
440 |
// actionsSet = new HashSet<Action>(bindings.values()); |
347 |
bindings.clear(); |
441 |
// bindings.clear(); |
348 |
actions = null; |
442 |
// actions = null; |
349 |
} |
443 |
// } |
350 |
|
444 |
// |
351 |
for(Action a: actionsSet) { |
445 |
// for(Action a: actionsSet) { |
352 |
updateActionAccelerator(a); |
446 |
// updateActionAccelerator(a); |
353 |
} |
447 |
// } |
354 |
|
448 |
// |
355 |
setChanged(); |
449 |
// setChanged(); |
356 |
notifyObservers(); |
450 |
// notifyObservers(); |
|
|
451 |
assert false; |
357 |
} |
452 |
} |
358 |
|
453 |
|
359 |
public Keymap getResolveParent() { |
454 |
public Keymap getResolveParent() { |
Lines 366-377
Link Here
|
366 |
notifyObservers(); |
461 |
notifyObservers(); |
367 |
} |
462 |
} |
368 |
|
463 |
|
369 |
/** Returns string representation - can be looong. |
464 |
// /** Returns string representation - can be looong. |
370 |
*/ |
465 |
// */ |
371 |
@Override |
466 |
// @Override |
372 |
public String toString() { |
467 |
// public String toString() { |
373 |
return "Keymap[" + name + "]" + bindings; // NOI18N |
468 |
// return "Keymap[" + name + "]" + bindings; // NOI18N |
374 |
} |
469 |
// } |
375 |
|
470 |
|
376 |
public static class SubKeymap implements Keymap { |
471 |
public static class SubKeymap implements Keymap { |
377 |
Object hold; |
472 |
Object hold; |