1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
2 | //
|
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
4 | // you may not use this file except in compliance with the License.
|
---|
5 | // You may obtain a copy of the License at
|
---|
6 | //
|
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
8 | //
|
---|
9 | // Unless required by applicable law or agreed to in writing, software
|
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
12 | // See the License for the specific language governing permissions and
|
---|
13 | // limitations under the License.
|
---|
14 |
|
---|
15 | package de.ugoe.cs.autoquest.jfcmonitor;
|
---|
16 |
|
---|
17 | import java.awt.AWTEvent;
|
---|
18 | import java.awt.Component;
|
---|
19 | import java.awt.event.AWTEventListener;
|
---|
20 | import java.awt.event.FocusEvent;
|
---|
21 | import java.awt.event.KeyEvent;
|
---|
22 | import java.awt.event.MouseEvent;
|
---|
23 |
|
---|
24 | import de.ugoe.cs.util.StringTools;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * <p>
|
---|
28 | * This class implements monitoring of AWT and Swing mouse and keyboard events. Each of the events
|
---|
29 | * is written to an output stream.
|
---|
30 | * </p>
|
---|
31 | *
|
---|
32 | * @author Steffen Herbold
|
---|
33 | * @author Fabian Glaser
|
---|
34 | * @version 1.0
|
---|
35 | */
|
---|
36 | public class JFCListener implements AWTEventListener {
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * <p>
|
---|
40 | * Writer for logging events.
|
---|
41 | * </p>
|
---|
42 | */
|
---|
43 | final private JFCMonitorOutputWriter outputWriter;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * <p>
|
---|
47 | * Constructor. Creates a new JFCListener with a given {@link JFCMonitorOutputWriter}, where the
|
---|
48 | * monitored information is logged.
|
---|
49 | * </p>
|
---|
50 | *
|
---|
51 | * @param outputWriter
|
---|
52 | * writer for the logged information
|
---|
53 | */
|
---|
54 | public JFCListener(JFCMonitorOutputWriter outputWriter) {
|
---|
55 | this.outputWriter = outputWriter;
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * <p>
|
---|
60 | * Writes all received {@link MouseEvent}s and {@link KeyEvent}s to the {@link #outputWriter}.
|
---|
61 | * </p>
|
---|
62 | *
|
---|
63 | * @see java.awt.event.AWTEventListener#eventDispatched(java.awt.AWTEvent)
|
---|
64 | */
|
---|
65 | @Override
|
---|
66 | public void eventDispatched(AWTEvent event) {
|
---|
67 | StringBuilder builder = new StringBuilder();
|
---|
68 |
|
---|
69 | if (event instanceof MouseEvent) {
|
---|
70 | if (!isMouseMovement(event.getID())) {
|
---|
71 | MouseEvent mouseEvent = (MouseEvent) event;
|
---|
72 | builder.append("<event id=\"" + event.getID() + "\">" + StringTools.ENDLINE);
|
---|
73 | builder.append(" <param name=\"X\" value=\"" + mouseEvent.getX() + "\" />" +
|
---|
74 | StringTools.ENDLINE);
|
---|
75 | builder.append(" <param name=\"Y\" value=\"" + mouseEvent.getY() + "\" />" +
|
---|
76 | StringTools.ENDLINE);
|
---|
77 | builder.append(" <param name=\"Button\" value=\"" + mouseEvent.getButton() +
|
---|
78 | "\" />" + StringTools.ENDLINE);
|
---|
79 | builder.append(" <param name=\"Modifiers\" value=\"" + mouseEvent.getModifiers() +
|
---|
80 | "\" />" + StringTools.ENDLINE);
|
---|
81 | addSourceInfo(builder, event);
|
---|
82 | addTimestampInfo(builder);
|
---|
83 | builder.append("</event>" + StringTools.ENDLINE);
|
---|
84 | }
|
---|
85 | }
|
---|
86 | else if (event instanceof KeyEvent) {
|
---|
87 | if (event.getID() == KeyEvent.KEY_PRESSED || event.getID() == KeyEvent.KEY_RELEASED) {
|
---|
88 | KeyEvent keyEvent = (KeyEvent) event;
|
---|
89 | builder.append("<event id=\"" + event.getID() + "\">" + StringTools.ENDLINE);
|
---|
90 | builder.append(" <param name=\"KeyCode\" value=\"" + keyEvent.getKeyCode() +
|
---|
91 | "\" />" + StringTools.ENDLINE);
|
---|
92 | builder.append(" <param name=\"Modifiers\" value=\"" + keyEvent.getModifiers() +
|
---|
93 | "\" />" + StringTools.ENDLINE);
|
---|
94 | addSourceInfo(builder, event);
|
---|
95 | addTimestampInfo(builder);
|
---|
96 | builder.append("</event>" + StringTools.ENDLINE);
|
---|
97 | }
|
---|
98 | }
|
---|
99 | else if (event instanceof FocusEvent) {
|
---|
100 | if (event.getID() == FocusEvent.FOCUS_GAINED) {
|
---|
101 | builder.append("<event id=\"" + event.getID() + "\">" + StringTools.ENDLINE);
|
---|
102 | addSourceInfo(builder, event);
|
---|
103 | addTimestampInfo(builder);
|
---|
104 | builder.append("</event>" + StringTools.ENDLINE);
|
---|
105 | }
|
---|
106 | }
|
---|
107 | if (builder.length() > 0 && outputWriter != null) {
|
---|
108 | outputWriter.write(builder.toString());
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * <p>
|
---|
114 | * Appends information about the event to a {@link StringBuilder}.
|
---|
115 | * </p>
|
---|
116 | *
|
---|
117 | * @param builder
|
---|
118 | * {@link StringBuilder} where the information is appended
|
---|
119 | * @param event
|
---|
120 | * event whose information is appended
|
---|
121 | */
|
---|
122 | private void addSourceInfo(StringBuilder builder, AWTEvent event) {
|
---|
123 | builder.append(" <param name=\"source\"");
|
---|
124 | if (event.getSource() instanceof Component) {
|
---|
125 | Component source = (Component) event.getSource();
|
---|
126 | builder.append(" value=\"" + Integer.toHexString(source.hashCode()) + "\"");
|
---|
127 | }
|
---|
128 | builder.append(" />" + StringTools.ENDLINE);
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * <p>
|
---|
133 | * Appends timestamp information of the event to a {@link StringBuilder}.
|
---|
134 | * </p>
|
---|
135 | *
|
---|
136 | * @param builder
|
---|
137 | * {@link StringBuilder} where the information is appended
|
---|
138 | */
|
---|
139 | private void addTimestampInfo(StringBuilder builder){
|
---|
140 | long timestamp = System.currentTimeMillis();
|
---|
141 | builder.append(" <param name=\"timestamp\"");
|
---|
142 | builder.append(" value=\"" + Long.toString(timestamp) + "\"/>");
|
---|
143 | builder.append(StringTools.ENDLINE);
|
---|
144 | }
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * <p>
|
---|
148 | * Checks if the Id of an {@link AWTEvent} is a mouse movement Id.
|
---|
149 | * </p>
|
---|
150 | *
|
---|
151 | * @param eventId
|
---|
152 | * id of the {@link AWTEvent}
|
---|
153 | * @return true, if the event is a mouse movement event; false otherwise
|
---|
154 | */
|
---|
155 | private boolean isMouseMovement(int eventId) {
|
---|
156 | return eventId == MouseEvent.MOUSE_MOVED || eventId == MouseEvent.MOUSE_DRAGGED ||
|
---|
157 | eventId == MouseEvent.MOUSE_ENTERED || eventId == MouseEvent.MOUSE_EXITED;
|
---|
158 | }
|
---|
159 |
|
---|
160 | }
|
---|