-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfile.java
More file actions
567 lines (534 loc) · 19.7 KB
/
Profile.java
File metadata and controls
567 lines (534 loc) · 19.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/**************************************************************************************
* bias_tree
* Copyright (c) 2014-2017 National University of Colombia, https://github.com/remixlab
* @author Jean Pierre Charalambos, http://otrolado.info/
*
* All rights reserved. Library that eases the creation of interactive
* scenes, released under the terms of the GNU Public License v3.0
* which is available at http://www.gnu.org/licenses/gpl.html
**************************************************************************************/
package remixlab.bias;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.Map.Entry;
/**
* A {@link Grabber} extension which allows to define
* {@link Shortcut} to {@link java.lang.reflect.Method} bindings. See
* {@link #setBinding(Shortcut, String)} and {@link #setBinding(Object, Shortcut, String)}
* .
* <p>
* To attach a profile to a grabber first override your
* {@link Grabber#performInteraction(BogusEvent)} method like this:
* <p>
* <pre>
* {@code
* public void performInteraction(BogusEvent event) {
* profile.handle(event);
* }
* }
* </pre>
* <p>
* (see {@link #handle(BogusEvent)}) and then simply pass the grabber instance to the
* {@link #Profile(Grabber)} constructor.
*/
public class Profile {
class ObjectMethodTuple {
Object object;
Method method;
ObjectMethodTuple(Object o, Method m) {
object = o;
method = m;
}
}
protected HashMap<Shortcut, ObjectMethodTuple> map;
protected Grabber grabber;
// static stuff
/**
* Utility function to programmatically register virtual keys to a {@link remixlab.bias.Shortcut} class,
* typically {@code KeyboardShortcuts}.
*/
public static void registerVKeys(Class<? extends Shortcut> shortcutClass, Class<?> keyEventClass) {
// TODO android needs testing
// idea took from here:
// http://stackoverflow.com/questions/15313469/java-keyboard-keycodes-list
// and here:
// http://www.java2s.com/Code/JavaAPI/java.lang.reflect/FieldgetIntObjectobj.htm
String prefix = keyEventClass.getName().contains("android") ? "KEYCODE_" : "VK_";
int l = prefix.length();
Field[] fields = keyEventClass.getDeclaredFields();
for (Field f : fields) {
if (Modifier.isStatic(f.getModifiers()) && Modifier.isPublic(f.getModifiers())) {
Class<?> clazzType = f.getType();
if (clazzType.toString().equals("int")) {
int id = -1;
try {
id = f.getInt(keyEventClass);
String name = f.getName();
if (!Shortcut.hasID(shortcutClass, id) && name.substring(0, l).equals(prefix))
Shortcut.registerID(shortcutClass, id, name);
} catch (Exception e) {
System.out.println("Warning: couldn't register key");
e.printStackTrace();
}
}
}
}
}
public static Object context = null;
/**
* Attaches a profile to the given grabber.
*/
public Profile(Grabber g) {
map = new HashMap<Shortcut, ObjectMethodTuple>();
grabber = g;
}
/**
* Instantiates this profile from another profile. Both Profile {@link #grabber()}
* should be of the same type.
*/
public void set(Profile p) {
if (grabber.getClass() != p.grabber.getClass()) {
System.err.println("Profile grabbers should be of the same type");
return;
}
map = new HashMap<Shortcut, ObjectMethodTuple>();
for (Map.Entry<Shortcut, ObjectMethodTuple> entry : p.map().entrySet()) {
if (entry.getValue().object == p.grabber)
map.put(entry.getKey(), new ObjectMethodTuple(grabber, entry.getValue().method));
else
map.put(entry.getKey(), new ObjectMethodTuple(entry.getValue().object, entry.getValue().method));
}
}
// public HashMap<Shortcut, Method>
/**
* Returns this profile set of shortcuts.
*/
public Set<Shortcut> shortcuts() {
return map.keySet();
}
/**
* Returns the grabber to which this profile is attached.
*/
public Grabber grabber() {
return grabber;
}
/**
* Internal use. Shortcut to object-method map.
*/
protected HashMap<Shortcut, ObjectMethodTuple> map() {
return map;
}
/**
* Returns the {@link java.lang.reflect.Method} binding for the given
* {@link Shortcut} key.
*
* @see #action(Shortcut)
*/
public Method method(Shortcut shortcut) {
return map.get(shortcut) == null ? null : map.get(shortcut).method;
}
/**
* Returns the {@link java.lang.reflect.Method} binding for the given
* {@link Shortcut} key.
*
* @see #method(Shortcut)
*/
public String action(Shortcut shortcut) {
Method m = method(shortcut);
if (m == null)
return null;
return m.getName();
}
/**
* Returns the action performing object. Either the {@link #grabber()} or an external
* object.
*/
public Object object(Shortcut shortcut) {
return map.get(shortcut) == null ? null : map.get(shortcut).object;
}
/**
* Main class method to be called from
* {@link Grabber#performInteraction(BogusEvent)}. Calls an action
* handler if the {@link BogusEvent#shortcut()} is bound.
*
* @see #setBinding(Shortcut, String)
* @see #setBinding(Object, Shortcut, String)
*/
public boolean handle(BogusEvent event) {
Method iHandlerMethod = method(event.shortcut());
if (iHandlerMethod != null) {
try {
if (object(event.shortcut()) == grabber)
iHandlerMethod.invoke(object(event.shortcut()), new Object[]{event});
else
iHandlerMethod.invoke(object(event.shortcut()), new Object[]{grabber, event});
return true;
} catch (Exception e) {
try {
if (object(event.shortcut()) == grabber)
iHandlerMethod.invoke(object(event.shortcut()), new Object[]{});
else
iHandlerMethod.invoke(object(event.shortcut()), new Object[]{grabber});
return true;
} catch (Exception empty) {
System.out.println("Something went wrong when invoking your " + iHandlerMethod.getName() + " method");
empty.printStackTrace();
}
System.out.println("Something went wrong when invoking your " + iHandlerMethod.getName() + " method");
e.printStackTrace();
}
}
return false;
}
/**
* Internal macro.
*/
protected boolean printWarning(Shortcut shortcut, String action) {
if (action == null) {
this.removeBinding(shortcut);
System.out.println(shortcut.description() + " removed");
return true;
}
if (hasBinding(shortcut)) {
Method a = method(shortcut);
if (a.getName().equals(action)) {
System.out.println("Warning: shortcut " + shortcut.description() + " already bound to " + a.getName());
return true;
} else {
System.out.println(
"Warning: overwriting shortcut " + shortcut.description() + " which was previously bound to " + a.getName());
return false;
}
}
return false;
}
/**
* Defines the shortcut that triggers the given action.
* <p>
* Attempt to set a shortcut for the {@code action} implemented by the {@link #context}
* (e.g., the {@code PApplet} in the case of a Processing application) or the
* {@link #grabber()} (e.g., the {@code InteractiveFrame} instance this profile is
* attached to, in the case of a Processing application) when no prototype is found in
* the {@link #context}.
* <p>
* The available action prototypes for the {@link #context} are:
* <ol>
* <li><b>public void action(grabber.getClass(), BogusEvent)</b></li>
* <li><b>public void action(grabber.getClass())</b></li>
* </ol>
* <p>
* The available action prototypes for the {@link #grabber()} are:
* <ol>
* <li><b>public void action(BogusEvent)</b></li>
* <li><b>public void action()</b></li>
* </ol>
* <p>
* The bogus-event type that may be passed to the above prototypes is the one specified
* by the {@link Shortcut#eventClass()} method:
* <ol>
* <li>A {@link remixlab.bias.event.ClickEvent} for a
* {@link remixlab.bias.event.ClickShortcut}</li>
* <li>A {@link remixlab.bias.event.KeyboardEvent} for a
* {@link remixlab.bias.event.KeyboardShortcut}</li>
* <li>A {@code DOFnEvent} for a {@link remixlab.bias.event.MotionShortcut}, where
* {@code n} is the {@link remixlab.bias.event.MotionShortcut#dofs(int)} of the
* motion-shortcut {@link remixlab.bias.event.MotionShortcut#id()}.</li>
* </ol>
* <b>Note</b> that in the latter case a {@link remixlab.bias.event.MotionEvent} may be
* passed too.
*
* @param shortcut {@link Shortcut}
* @param action {@link java.lang.String}
* @see #setBinding(Object, Shortcut, String)
* @see Shortcut#eventClass()
* @see remixlab.bias.event.MotionShortcut#eventClass()
* @see remixlab.bias.event.MotionShortcut#dofs(int)
* @see remixlab.bias.event.MotionShortcut#registerID(int, int, String)
*/
public boolean setBinding(Shortcut shortcut, String action) {
if (printWarning(shortcut, action))
return false;
// 1. Search at context:
String proto1 = null;
Method method = null;
if (context != null && context != grabber) {
try {
method = context.getClass().getMethod(action, new Class<?>[]{grabber.getClass(), shortcut.eventClass()});
} catch (Exception clazz) {
try {
method = context.getClass().getMethod(action, new Class<?>[]{grabber.getClass()});
} catch (Exception empty) {
if (shortcut.defaultEventClass() != null)
try {
method = context.getClass().getMethod(action, new Class<?>[]{grabber.getClass(), shortcut.defaultEventClass()});
} catch (Exception e) {
proto1 = prototypes(context, shortcut, action);
}
else {
proto1 = prototypes(context, shortcut, action);
}
}
}
if (method != null) {
map.put(shortcut, new ObjectMethodTuple(context, method));
return true;
}
}
// 2. If not found, search at grabber:
String proto2 = null;
String other = ". Or, if your binding lies within other object, use setBinding(Object object, Shortcut key, String action) instead.";
try {
method = grabber.getClass().getMethod(action, new Class<?>[]{shortcut.eventClass()});
} catch (Exception clazz) {
try {
method = grabber.getClass().getMethod(action, new Class<?>[]{});
} catch (Exception empty) {
if (shortcut.defaultEventClass() != null)
try {
method = grabber.getClass().getMethod(action, new Class<?>[]{shortcut.defaultEventClass()});
} catch (Exception motion) {
proto2 = prototypes(shortcut, action);
System.out.println("Warning: not binding set! Check the existence of one of the following method prototypes: " + (
proto1 != null ?
proto1 + ", " + proto2 :
proto2) + other);
}
else {
proto2 = prototypes(shortcut, action);
System.out.println("Warning: not binding set! Check the existence of one of the following method prototypes: " + (
proto1 != null ?
proto1 + ", " + proto2 :
proto2) + other);
}
}
}
if (method != null) {
map.put(shortcut, new ObjectMethodTuple(grabber, method));
return true;
}
return false;
}
/**
* Defines the shortcut that triggers the given action.
* <p>
* Attempt to set a shortcut for the {@code action} implemented by {@code object}. The
* action procedure may have two different prototypes:
* <ol>
* <li><b>public void action(BogusEvent)</b></li>
* <li><b>public void action()</b></li>
* </ol>
* The bogus-event type that may be passed to the first prototype is the one specified
* by the {@link Shortcut#eventClass()} method:
* <ol>
* <li>A {@link remixlab.bias.event.ClickEvent} for a
* {@link remixlab.bias.event.ClickShortcut}</li>
* <li>A {@link remixlab.bias.event.KeyboardEvent} for a
* {@link remixlab.bias.event.KeyboardShortcut}</li>
* <li>A {@code DOFnEvent} for a {@link remixlab.bias.event.MotionShortcut}, where
* {@code n} is the {@link remixlab.bias.event.MotionShortcut#dofs(int)} of the
* motion-shortcut {@link remixlab.bias.event.MotionShortcut#id()}.</li>
* </ol>
* <b>Note</b> that in the latter case a {@link remixlab.bias.event.MotionEvent} may be
* passed too.
*
* @param object {@link java.lang.Object}
* @param shortcut {@link Shortcut}
* @param action {@link java.lang.String}
* @see #setBinding(Object, Shortcut, String)
* @see Shortcut#eventClass()
* @see remixlab.bias.event.MotionShortcut#eventClass()
* @see remixlab.bias.event.MotionShortcut#dofs(int)
* @see remixlab.bias.event.MotionShortcut#registerID(int, int, String)
*/
public boolean setBinding(Object object, Shortcut shortcut, String action) {
if (object == null) {
System.out.println("Warning: no binding set. Object can't be null");
return false;
}
if (object == grabber())
return setBinding(shortcut, action);
if (printWarning(shortcut, action))
return false;
Method method = null;
try {
method = object.getClass().getMethod(action, new Class<?>[]{grabber.getClass(), shortcut.eventClass()});
} catch (Exception clazz) {
try {
method = object.getClass().getMethod(action, new Class<?>[]{grabber.getClass()});
} catch (Exception empty) {
if (shortcut.defaultEventClass() != null)
try {
method = object.getClass().getMethod(action, new Class<?>[]{grabber.getClass(), shortcut.defaultEventClass()});
} catch (Exception e) {
System.out.println(
"Warning: not binding set! Check the existence of one of the following method prototypes: " + prototypes(
object, shortcut, action));
}
else {
System.out.println(
"Warning: not binding set! Check the existence of one of the following method prototypes:: " + prototypes(
object, shortcut, action));
}
}
}
if (method != null) {
map.put(shortcut, new ObjectMethodTuple(object, method));
return true;
}
return false;
}
/**
* Internal use.
*
* @see #setBinding(Shortcut, String)
* @see #setBinding(Object, Shortcut, String)
*/
protected String prototypes(Object object, Shortcut shortcut, String action) {
String sgn1 =
"public void " + object.getClass().getSimpleName() + "." + action + "(" + grabber().getClass().getSimpleName()
+ ")";
String sgn2 =
"public void " + object.getClass().getSimpleName() + "." + action + "(" + grabber().getClass().getSimpleName()
+ ", " + shortcut.eventClass().getSimpleName() + ")";
if (shortcut.defaultEventClass() != null) {
String sgn3 =
"public void " + object.getClass().getSimpleName() + "." + action + "(" + grabber().getClass().getSimpleName()
+ ", " + shortcut.defaultEventClass().getSimpleName() + ")";
return sgn1 + ", " + sgn2 + ", " + sgn3;
} else
return sgn1 + ", " + sgn2;
}
/**
* Internal use.
*
* @see #setBinding(Shortcut, String)
*/
protected String prototypes(Shortcut shortcut, String action) {
String sgn1 = "public void " + grabber.getClass().getSimpleName() + "." + action + "()";
String sgn2 =
"public void " + grabber.getClass().getSimpleName() + "." + action + "(" + shortcut.eventClass().getSimpleName() + ")";
if (shortcut.defaultEventClass() != null) {
String sgn3 =
"public void " + grabber.getClass().getSimpleName() + "." + action + "(" + shortcut.defaultEventClass().getSimpleName()
+ ")";
return sgn1 + ", " + sgn2 + ", " + sgn3;
} else
return sgn1 + ", " + sgn2;
}
/**
* Removes the shortcut binding.
*
* @param shortcut {@link Shortcut}
*/
public void removeBinding(Shortcut shortcut) {
map.remove(shortcut);
}
/**
* Removes all the shortcuts from this object.
*/
public void removeBindings() {
map.clear();
}
/**
* Removes all the shortcuts from the given shortcut class.
*/
public void removeBindings(Class<? extends Shortcut> cls) {
Iterator<Entry<Shortcut, ObjectMethodTuple>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Shortcut, ObjectMethodTuple> pair = it.next();
if (cls.equals(pair.getKey().getClass()))
it.remove();
}
}
/**
* Returns a description of all the bindings this profile holds from the given shortcut
* class.
*/
public String info(Class<? extends Shortcut> cls) {
String result = new String();
HashMap<Shortcut, ObjectMethodTuple> clsMap = map(cls);
String info = new String();
for (Entry<Shortcut, ObjectMethodTuple> entry : clsMap.entrySet())
info += entry.getKey().description() + " -> " + entry.getValue().method.getName() + "\n";
if (!info.isEmpty()) {
result += cls.getSimpleName() + " bindings:\n";
result += info;
}
return result;
}
/**
* (Internal) Used by {@link #info(Class)}.
*/
protected HashMap<Shortcut, ObjectMethodTuple> map(Class<? extends Shortcut> cls) {
HashMap<Shortcut, ObjectMethodTuple> result = new HashMap<Shortcut, ObjectMethodTuple>();
for (Entry<Shortcut, ObjectMethodTuple> entry : map.entrySet())
if (entry.getKey() != null && entry.getValue() != null)
if (cls.equals(entry.getKey().getClass()))
result.put(entry.getKey(), entry.getValue());
return result;
}
/**
* Returns a description of all the bindings this profile holds.
*/
public String info() {
// 1. Shortcut class list
ArrayList<Class<? extends Shortcut>> list = new ArrayList<Class<? extends Shortcut>>();
for (Shortcut s : map.keySet())
if (!list.contains(s.getClass()))
list.add(s.getClass());
// 2. Print info per Shortcut class
String result = new String();
for (Class<? extends Shortcut> clazz : list) {
String info = info(clazz);
if (!info.isEmpty())
result += info;
}
return result;
}
/**
* Returns true if this object contains a binding for the specified shortcut.
*
* @param shortcut {@link Shortcut}
* @return true if this object contains a binding for the specified shortcut.
*/
public boolean hasBinding(Shortcut shortcut) {
return map.containsKey(shortcut);
}
/**
* Returns true if this object maps one or more shortcuts to the action specified by the
* {@link #grabber()}.
*
* @param action {@link java.lang.String}
* @return true if this object maps one or more shortcuts to the specified action.
*/
public boolean isActionBound(String action) {
for (ObjectMethodTuple tuple : map.values()) {
if (grabber == tuple.object && tuple.method.getName().equals(action))
return true;
}
return false;
}
/**
* Returns true if this object maps one or more shortcuts to method specified by the
* {@link #grabber()}.
*
* @param method {@link java.lang.reflect.Method}
* @return true if this object maps one or more shortcuts to the specified action.
*/
protected boolean isMethodBound(Method method) {
return isMethodBound(grabber, method);
}
/**
* Returns true if this object maps one or more shortcuts to the {@code method}
* specified by the {@code object}.
*
* @param object {@link java.lang.Object}
* @param method {@link java.lang.reflect.Method}
* @return true if this object maps one or more shortcuts to the specified action.
*/
protected boolean isMethodBound(Object object, Method method) {
return map.containsValue(new ObjectMethodTuple(object, method));
}
}