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 | 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
|
---|
40 | }
|
---|
41 |
|
---|
42 | private EventType _eventType;
|
---|
43 | private Object _eventData;
|
---|
44 |
|
---|
45 | public KBMInputEvent(EventType type, Object data) throws Exception
|
---|
46 | {
|
---|
47 | super(InputEvent.InputType.KBM);
|
---|
48 | _eventType = type;
|
---|
49 | _eventData = data;
|
---|
50 | checkData();
|
---|
51 | }
|
---|
52 |
|
---|
53 | public EventType getEventType()
|
---|
54 | {
|
---|
55 | return _eventType;
|
---|
56 | }
|
---|
57 |
|
---|
58 | public Object getData()
|
---|
59 | {
|
---|
60 | return _eventData;
|
---|
61 | }
|
---|
62 |
|
---|
63 | /** Gets the key that went down if this is a key-down event. */
|
---|
64 | public Key getKeyDown()
|
---|
65 | {
|
---|
66 | if (_eventType == EventType.KEY_DOWN) return (Key) _eventData;
|
---|
67 |
|
---|
68 | return null;
|
---|
69 | }
|
---|
70 |
|
---|
71 | /** Gets the key that went up if this is a key-up event. */
|
---|
72 | public Key getKeyUp()
|
---|
73 | {
|
---|
74 | if (_eventType == EventType.KEY_UP) return (Key) _eventData;
|
---|
75 |
|
---|
76 | return null;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Gets the name of the key that was pressed/released.
|
---|
81 | * Returns the empty string if this is not a key event.
|
---|
82 | */
|
---|
83 | public String getKeyText()
|
---|
84 | {
|
---|
85 | Key key = getKeyDown();
|
---|
86 | if (key == null) key = getKeyUp();
|
---|
87 | if (key == null) return "";
|
---|
88 | return key.name();
|
---|
89 | }
|
---|
90 |
|
---|
91 | /** Gets the character that was typed if this is a CHAR_TYPED event. */
|
---|
92 | public Character getCharTyped()
|
---|
93 | {
|
---|
94 | if (_eventType == EventType.CHAR_TYPED) return (Character) _eventData;
|
---|
95 |
|
---|
96 | return null;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /** Gets the mouse button that went down if this is a mouse-button-down event. */
|
---|
100 | public MouseButton getMouseButtonDown()
|
---|
101 | {
|
---|
102 | if (_eventType == EventType.MOUSE_BUTTON_DOWN) return (MouseButton) _eventData;
|
---|
103 |
|
---|
104 | return null;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /** Gets the mouse button that went up if this is a mouse-button-up event. */
|
---|
108 | public MouseButton getMouseButtonUp()
|
---|
109 | {
|
---|
110 | if (_eventType == EventType.MOUSE_BUTTON_UP) return (MouseButton) _eventData;
|
---|
111 |
|
---|
112 | return null;
|
---|
113 | }
|
---|
114 |
|
---|
115 | /** Gets the position of the mouse if this is a mouse-move event. */
|
---|
116 | public Line getMouseMove()
|
---|
117 | {
|
---|
118 | if (_eventType == EventType.MOUSE_MOVE) return (Line) _eventData;
|
---|
119 |
|
---|
120 | return null;
|
---|
121 | }
|
---|
122 |
|
---|
123 | private void checkData() throws Exception
|
---|
124 | {
|
---|
125 | if (_eventType == null) throw new Exception("No event type specified");
|
---|
126 | if (_eventData == null) throw new Exception("No event data specified");
|
---|
127 |
|
---|
128 | switch (_eventType) {
|
---|
129 | case MOUSE_BUTTON_DOWN:
|
---|
130 | case MOUSE_BUTTON_UP:
|
---|
131 | if (!(_eventData instanceof MouseButton)) throw new Exception("MouseButton data expected");
|
---|
132 | break;
|
---|
133 | case KEY_DOWN:
|
---|
134 | case KEY_UP:
|
---|
135 | if (!(_eventData instanceof Key)) throw new Exception("Key data expected");
|
---|
136 | break;
|
---|
137 | case CHAR_TYPED:
|
---|
138 | if (!(_eventData instanceof Character)) throw new Exception("Character data expected");
|
---|
139 | break;
|
---|
140 | case MOUSE_MOVE:
|
---|
141 | if (!(_eventData instanceof Line)) throw new Exception("Line data expected");
|
---|
142 | break;
|
---|
143 | case MOUSE_WHEEL_SCROLL:
|
---|
144 | if (!(_eventData instanceof Integer)) throw new Exception("Integer data expected");
|
---|
145 | break;
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Override
|
---|
150 | public String toString()
|
---|
151 | {
|
---|
152 | return _eventType.toString() + " event : Data = " + _eventData.toString();
|
---|
153 | }
|
---|
154 | }
|
---|