[927] | 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.
|
---|
[822] | 14 |
|
---|
[922] | 15 | package de.ugoe.cs.autoquest.jfcmonitor;
|
---|
[263] | 16 |
|
---|
| 17 | import java.awt.AWTEvent;
|
---|
[271] | 18 | import java.awt.Component;
|
---|
[263] | 19 | import java.awt.event.AWTEventListener;
|
---|
[370] | 20 | import java.awt.event.FocusEvent;
|
---|
[263] | 21 | import java.awt.event.KeyEvent;
|
---|
| 22 | import java.awt.event.MouseEvent;
|
---|
| 23 |
|
---|
[347] | 24 | import de.ugoe.cs.util.StringTools;
|
---|
| 25 |
|
---|
[271] | 26 | /**
|
---|
| 27 | * <p>
|
---|
[822] | 28 | * This class implements monitoring of AWT and Swing mouse and keyboard events. Each of the events
|
---|
| 29 | * is written to an output stream.
|
---|
[271] | 30 | * </p>
|
---|
| 31 | *
|
---|
[853] | 32 | * @author Steffen Herbold
|
---|
| 33 | * @author Fabian Glaser
|
---|
[271] | 34 | * @version 1.0
|
---|
| 35 | */
|
---|
[263] | 36 | public class JFCListener implements AWTEventListener {
|
---|
| 37 |
|
---|
[822] | 38 | /**
|
---|
| 39 | * <p>
|
---|
| 40 | * Writer for logging events.
|
---|
| 41 | * </p>
|
---|
| 42 | */
|
---|
[842] | 43 | final private JFCMonitorOutputWriter outputWriter;
|
---|
[271] | 44 |
|
---|
[822] | 45 | /**
|
---|
| 46 | * <p>
|
---|
[842] | 47 | * Constructor. Creates a new JFCListener with a given {@link JFCMonitorOutputWriter}, where the
|
---|
[822] | 48 | * monitored information is logged.
|
---|
| 49 | * </p>
|
---|
| 50 | *
|
---|
| 51 | * @param outputWriter
|
---|
| 52 | * writer for the logged information
|
---|
| 53 | */
|
---|
[842] | 54 | public JFCListener(JFCMonitorOutputWriter outputWriter) {
|
---|
[822] | 55 | this.outputWriter = outputWriter;
|
---|
| 56 | }
|
---|
[271] | 57 |
|
---|
[822] | 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();
|
---|
[271] | 68 |
|
---|
[822] | 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);
|
---|
[1001] | 82 | addTimestampInfo(builder);
|
---|
[822] | 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);
|
---|
[1001] | 95 | addTimestampInfo(builder);
|
---|
[822] | 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);
|
---|
[1001] | 103 | addTimestampInfo(builder);
|
---|
[822] | 104 | builder.append("</event>" + StringTools.ENDLINE);
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | if (builder.length() > 0 && outputWriter != null) {
|
---|
[842] | 108 | outputWriter.write(builder.toString());
|
---|
[822] | 109 | }
|
---|
| 110 | }
|
---|
[271] | 111 |
|
---|
[822] | 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) {
|
---|
[932] | 123 | builder.append(" <param name=\"source\"");
|
---|
[842] | 124 | if (event.getSource() instanceof Component) {
|
---|
| 125 | Component source = (Component) event.getSource();
|
---|
[932] | 126 | builder.append(" value=\"" + Integer.toHexString(source.hashCode()) + "\"");
|
---|
[842] | 127 | }
|
---|
[932] | 128 | builder.append(" />" + StringTools.ENDLINE);
|
---|
[822] | 129 | }
|
---|
[1001] | 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 | }
|
---|
[271] | 145 |
|
---|
[822] | 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 | }
|
---|
[263] | 159 |
|
---|
| 160 | }
|
---|