1 | package org.expeditee.gio.input;
|
---|
2 |
|
---|
3 | import org.expeditee.core.Line;
|
---|
4 |
|
---|
5 | public class KBMInputEvent extends InputEvent {
|
---|
6 |
|
---|
7 | public enum EventType {
|
---|
8 | MOUSE_BUTTON_DOWN,
|
---|
9 | MOUSE_BUTTON_UP,
|
---|
10 | MOUSE_MOVE,
|
---|
11 | MOUSE_WHEEL_SCROLL,
|
---|
12 | KEY_DOWN,
|
---|
13 | KEY_UP,
|
---|
14 | CHAR_TYPED
|
---|
15 | }
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Enumerates the mouse buttons of interest to Expeditee.
|
---|
19 | */
|
---|
20 | public enum MouseButton {
|
---|
21 | LEFT,
|
---|
22 | MIDDLE,
|
---|
23 | RIGHT
|
---|
24 | }
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Enumerates the keyboard keys of interest to Expeditee.
|
---|
28 | */
|
---|
29 | public enum Key {
|
---|
30 | SHIFT,CTRL,ALT,
|
---|
31 | ESC,
|
---|
32 | F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12,
|
---|
33 | UP_ARROW,DOWN_ARROW,LEFT_ARROW,RIGHT_ARROW,
|
---|
34 | HOME,END,
|
---|
35 | DELETE,
|
---|
36 | PAGE_UP,PAGE_DOWN,
|
---|
37 | BACKSPACE,
|
---|
38 | TAB,
|
---|
39 | ENTER,
|
---|
40 | A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z
|
---|
41 | }
|
---|
42 |
|
---|
43 | private EventType _eventType;
|
---|
44 | private Object _eventData;
|
---|
45 |
|
---|
46 | public KBMInputEvent(EventType type, Object data) throws Exception
|
---|
47 | {
|
---|
48 | super(InputEvent.InputType.KBM);
|
---|
49 | _eventType = type;
|
---|
50 | _eventData = data;
|
---|
51 | checkData();
|
---|
52 | }
|
---|
53 |
|
---|
54 | public EventType getEventType()
|
---|
55 | {
|
---|
56 | return _eventType;
|
---|
57 | }
|
---|
58 |
|
---|
59 | public Object getData()
|
---|
60 | {
|
---|
61 | return _eventData;
|
---|
62 | }
|
---|
63 |
|
---|
64 | /** Gets the key that went down if this is a key-down event. */
|
---|
65 | public Key getKeyDown()
|
---|
66 | {
|
---|
67 | if (_eventType == EventType.KEY_DOWN) return (Key) _eventData;
|
---|
68 |
|
---|
69 | return null;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /** Gets the key that went up if this is a key-up event. */
|
---|
73 | public Key getKeyUp()
|
---|
74 | {
|
---|
75 | if (_eventType == EventType.KEY_UP) return (Key) _eventData;
|
---|
76 |
|
---|
77 | return null;
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Gets the name of the key that was pressed/released.
|
---|
82 | * Returns the empty string if this is not a key event.
|
---|
83 | */
|
---|
84 | public String getKeyText()
|
---|
85 | {
|
---|
86 | Key key = getKeyDown();
|
---|
87 | if (key == null) key = getKeyUp();
|
---|
88 | if (key == null) return "";
|
---|
89 | return key.name();
|
---|
90 | }
|
---|
91 |
|
---|
92 | /** Gets the character that was typed if this is a CHAR_TYPED event. */
|
---|
93 | public Character getCharTyped()
|
---|
94 | {
|
---|
95 | if (_eventType == EventType.CHAR_TYPED) return (Character) _eventData;
|
---|
96 |
|
---|
97 | return null;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /** Gets the mouse button that went down if this is a mouse-button-down event. */
|
---|
101 | public MouseButton getMouseButtonDown()
|
---|
102 | {
|
---|
103 | if (_eventType == EventType.MOUSE_BUTTON_DOWN) return (MouseButton) _eventData;
|
---|
104 |
|
---|
105 | return null;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /** Gets the mouse button that went up if this is a mouse-button-up event. */
|
---|
109 | public MouseButton getMouseButtonUp()
|
---|
110 | {
|
---|
111 | if (_eventType == EventType.MOUSE_BUTTON_UP) return (MouseButton) _eventData;
|
---|
112 |
|
---|
113 | return null;
|
---|
114 | }
|
---|
115 |
|
---|
116 | /** Gets the position of the mouse if this is a mouse-move event. */
|
---|
117 | public Line getMouseMove()
|
---|
118 | {
|
---|
119 | if (_eventType == EventType.MOUSE_MOVE) return (Line) _eventData;
|
---|
120 |
|
---|
121 | return null;
|
---|
122 | }
|
---|
123 |
|
---|
124 | private void checkData() throws Exception
|
---|
125 | {
|
---|
126 | if (_eventType == null) throw new Exception("No event type specified");
|
---|
127 | if (_eventData == null) throw new Exception("No event data specified");
|
---|
128 |
|
---|
129 | switch (_eventType) {
|
---|
130 | case MOUSE_BUTTON_DOWN:
|
---|
131 | case MOUSE_BUTTON_UP:
|
---|
132 | if (!(_eventData instanceof MouseButton)) throw new Exception("MouseButton data expected");
|
---|
133 | break;
|
---|
134 | case KEY_DOWN:
|
---|
135 | case KEY_UP:
|
---|
136 | if (!(_eventData instanceof Key)) throw new Exception("Key data expected");
|
---|
137 | break;
|
---|
138 | case CHAR_TYPED:
|
---|
139 | if (!(_eventData instanceof Character)) throw new Exception("Character data expected");
|
---|
140 | break;
|
---|
141 | case MOUSE_MOVE:
|
---|
142 | if (!(_eventData instanceof Line)) throw new Exception("Line data expected");
|
---|
143 | break;
|
---|
144 | case MOUSE_WHEEL_SCROLL:
|
---|
145 | if (!(_eventData instanceof Integer)) throw new Exception("Integer data expected");
|
---|
146 | break;
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | @Override
|
---|
151 | public String toString()
|
---|
152 | {
|
---|
153 | return _eventType.toString() + " event : Data = " + _eventData.toString();
|
---|
154 | }
|
---|
155 | }
|
---|