1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| package org.ericmignot.modeler.util.misc; |
19 |
| |
20 |
| import java.awt.event.KeyEvent; |
21 |
| import java.awt.event.KeyListener; |
22 |
| import java.util.ArrayList; |
23 |
| import java.util.List; |
24 |
| |
25 |
| public class KeyboardAnalyzer implements KeyListener |
26 |
| { |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| private long delay; |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| private String potentialCommand; |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| private long lastInputTimeStamp; |
43 |
| |
44 |
| private List commands; |
45 |
| private List commandListeners; |
46 |
| |
47 |
122
| public KeyboardAnalyzer()
|
48 |
| { |
49 |
122
| this.setDelay(0);
|
50 |
122
| this.setLastInputTimeStamp(System.currentTimeMillis());
|
51 |
122
| this.commands = new ArrayList();
|
52 |
122
| this.commandListeners = new ArrayList();
|
53 |
122
| this.resetCommand();
|
54 |
| } |
55 |
| |
56 |
| |
57 |
119
| public void addCommand (String command)
|
58 |
| { |
59 |
119
| this.commands.add(command);
|
60 |
| } |
61 |
17
| public boolean containsCommand(String command)
|
62 |
| { |
63 |
17
| return this.commands.contains(command);
|
64 |
| } |
65 |
118
| public void addCommandListener (CommandListener listener)
|
66 |
| { |
67 |
118
| this.commandListeners.add(listener);
|
68 |
| } |
69 |
18
| public long getDelay()
|
70 |
| { |
71 |
18
| return delay;
|
72 |
| } |
73 |
243
| public void setDelay(long delay)
|
74 |
| { |
75 |
243
| this.delay = delay;
|
76 |
| } |
77 |
16
| public long getLastInputTimeStamp()
|
78 |
| { |
79 |
16
| return lastInputTimeStamp;
|
80 |
| } |
81 |
138
| public void setLastInputTimeStamp(long lastInputDate)
|
82 |
| { |
83 |
138
| this.lastInputTimeStamp = lastInputDate;
|
84 |
| } |
85 |
1
| public void waitAtLeast(long ms)
|
86 |
| { |
87 |
1
| this.lastInputTimeStamp = System.currentTimeMillis() - ms;
|
88 |
| } |
89 |
| |
90 |
123
| public void resetCommand()
|
91 |
| { |
92 |
123
| this.potentialCommand = "";
|
93 |
| } |
94 |
16
| public void addChar(char aChar)
|
95 |
| { |
96 |
16
| this.potentialCommand += aChar;
|
97 |
| } |
98 |
18
| public String getPotentialCommand()
|
99 |
| { |
100 |
18
| return this.potentialCommand;
|
101 |
| } |
102 |
| |
103 |
0
| public void keyTyped(KeyEvent e) {}
|
104 |
| |
105 |
0
| public void keyPressed(KeyEvent e) {}
|
106 |
| |
107 |
0
| public void keyReleased(KeyEvent e)
|
108 |
| { |
109 |
0
| treatChar(e.getKeyChar());
|
110 |
| } |
111 |
| |
112 |
16
| public synchronized void treatChar(char c)
|
113 |
| { |
114 |
16
| long current = System.currentTimeMillis();
|
115 |
16
| if ((current - this.getLastInputTimeStamp() > this.getDelay()))
|
116 |
| { |
117 |
1
| this.resetCommand();
|
118 |
| } |
119 |
16
| this.addChar(c);
|
120 |
16
| if (this.containsCommand(this.getPotentialCommand()))
|
121 |
| { |
122 |
0
| for (int i=0; i<this.commandListeners.size(); i++)
|
123 |
| { |
124 |
0
| ((CommandListener) this.commandListeners.get(i))
|
125 |
| .commandFound(this.getPotentialCommand()); |
126 |
| } |
127 |
| } |
128 |
16
| this.setLastInputTimeStamp(current);
|
129 |
| } |
130 |
| |
131 |
| } |