1 | |
---|
2 | package de.ugoe.cs.quest.eventcore.gui; |
---|
3 | |
---|
4 | import java.util.LinkedList; |
---|
5 | import java.util.List; |
---|
6 | import java.util.logging.Level; |
---|
7 | |
---|
8 | import de.ugoe.cs.quest.eventcore.Event; |
---|
9 | import de.ugoe.cs.tasktree.keyboardmaps.VirtualKey; |
---|
10 | import de.ugoe.cs.util.console.Console; |
---|
11 | |
---|
12 | /** |
---|
13 | * <p> |
---|
14 | * Because often not all events are recorded properly, it is possible that there is not always a |
---|
15 | * KEYDOWN interaction each KEYUP interaction. This class provides the functionality to correct |
---|
16 | * these mismatches with one of the following two strategies: |
---|
17 | * <ul> |
---|
18 | * <li>Removal: each interaction that does not form a proper KEYDOWN/KEYUP pair is removed.</li> |
---|
19 | * <li>Addition: for each interaction that does not form a proper KEYDOWN/KEYUP pair, the missing |
---|
20 | * message is added to the sequence directly before/after the existing message.</li> |
---|
21 | * </ul> |
---|
22 | * </p> |
---|
23 | * |
---|
24 | * @version $Revision: $ $Date: Sep 3, 2012$ |
---|
25 | * @author 2012, last modified by $Author: sherbold$ |
---|
26 | */ |
---|
27 | public class KeyInteractionCleaner { |
---|
28 | |
---|
29 | /** |
---|
30 | * <p> |
---|
31 | * Describes the clean-up mode. |
---|
32 | * </p> |
---|
33 | * |
---|
34 | * @version $Revision: $ $Date: Sep 3, 2012$ |
---|
35 | * @author 2012, last modified by $Author: sherbold$ |
---|
36 | */ |
---|
37 | public enum CleanupMode { |
---|
38 | REMOVAL, ADDITION |
---|
39 | }; |
---|
40 | |
---|
41 | /** |
---|
42 | * <p> |
---|
43 | * Creates a copy of the passed sequence and performs the clean-up (see class description) on |
---|
44 | * the copy. |
---|
45 | * </p> |
---|
46 | * |
---|
47 | * @param sequence |
---|
48 | * sequence on which the clean-up is performed |
---|
49 | * @param mode |
---|
50 | * defines whether removal or addition mode is used |
---|
51 | * @return copy of sequence with cleaned up key interactions |
---|
52 | */ |
---|
53 | public List<Event> cleanupKeyInteractions(List<Event> sequence, CleanupMode mode) { |
---|
54 | List<Event> sequenceCopy = new LinkedList<Event>(sequence); |
---|
55 | List<VirtualKey> pressedKeys = new LinkedList<VirtualKey>(); |
---|
56 | |
---|
57 | for( int i=0 ; i<sequenceCopy.size() ; i++ ) { |
---|
58 | Event event = sequenceCopy.get(i); |
---|
59 | if( event.getType() instanceof KeyPressed ) { |
---|
60 | pressedKeys.add(((KeyPressed) event.getType()).getKey()); |
---|
61 | } |
---|
62 | if( event.getType() instanceof KeyReleased ) { |
---|
63 | VirtualKey key = ((KeyReleased) event.getType()).getKey(); |
---|
64 | if( pressedKeys.contains(key)) { |
---|
65 | pressedKeys.remove(key); |
---|
66 | } |
---|
67 | else { |
---|
68 | Console.traceln(Level.INFO, "KeyReleased without KeyDown for key " + key + " found at index " + i); |
---|
69 | switch(mode) { |
---|
70 | case REMOVAL: |
---|
71 | sequenceCopy.remove(i); |
---|
72 | i--; |
---|
73 | break; |
---|
74 | case ADDITION: |
---|
75 | KeyPressed pressed = new KeyPressed(key); |
---|
76 | Event keyPressedEvent = new Event(pressed, event.getTarget()); |
---|
77 | sequenceCopy.add(i, keyPressedEvent); |
---|
78 | i++; |
---|
79 | break; |
---|
80 | default: |
---|
81 | throw new AssertionError("reached source code that should be unreachable"); |
---|
82 | } |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | return sequenceCopy; |
---|
87 | } |
---|
88 | } |
---|