// Copyright 2012 Georg-August-Universität Göttingen, Germany // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package de.ugoe.cs.autoquest.jfcmonitor; import java.awt.AWTEvent; import java.awt.Component; import java.awt.event.AWTEventListener; import java.awt.event.FocusEvent; import java.awt.event.KeyEvent; import java.awt.event.MouseEvent; import de.ugoe.cs.util.StringTools; /** *

* This class implements monitoring of AWT and Swing mouse and keyboard events. Each of the events * is written to an output stream. *

* * @author Steffen Herbold * @author Fabian Glaser * @version 1.0 */ public class JFCListener implements AWTEventListener { /** *

* Writer for logging events. *

*/ final private JFCMonitorOutputWriter outputWriter; /** *

* Constructor. Creates a new JFCListener with a given {@link JFCMonitorOutputWriter}, where the * monitored information is logged. *

* * @param outputWriter * writer for the logged information */ public JFCListener(JFCMonitorOutputWriter outputWriter) { this.outputWriter = outputWriter; } /** *

* Writes all received {@link MouseEvent}s and {@link KeyEvent}s to the {@link #outputWriter}. *

* * @see java.awt.event.AWTEventListener#eventDispatched(java.awt.AWTEvent) */ @Override public void eventDispatched(AWTEvent event) { StringBuilder builder = new StringBuilder(); if (event instanceof MouseEvent) { if (!isMouseMovement(event.getID())) { MouseEvent mouseEvent = (MouseEvent) event; builder.append("" + StringTools.ENDLINE); builder.append(" " + StringTools.ENDLINE); builder.append(" " + StringTools.ENDLINE); builder.append(" " + StringTools.ENDLINE); builder.append(" " + StringTools.ENDLINE); addSourceInfo(builder, event); addTimestampInfo(builder); builder.append("" + StringTools.ENDLINE); } } else if (event instanceof KeyEvent) { if (event.getID() == KeyEvent.KEY_PRESSED || event.getID() == KeyEvent.KEY_RELEASED) { KeyEvent keyEvent = (KeyEvent) event; builder.append("" + StringTools.ENDLINE); builder.append(" " + StringTools.ENDLINE); builder.append(" " + StringTools.ENDLINE); addSourceInfo(builder, event); addTimestampInfo(builder); builder.append("" + StringTools.ENDLINE); } } else if (event instanceof FocusEvent) { if (event.getID() == FocusEvent.FOCUS_GAINED) { builder.append("" + StringTools.ENDLINE); addSourceInfo(builder, event); addTimestampInfo(builder); builder.append("" + StringTools.ENDLINE); } } if (builder.length() > 0 && outputWriter != null) { outputWriter.write(builder.toString()); } } /** *

* Appends information about the event to a {@link StringBuilder}. *

* * @param builder * {@link StringBuilder} where the information is appended * @param event * event whose information is appended */ private void addSourceInfo(StringBuilder builder, AWTEvent event) { builder.append(" " + StringTools.ENDLINE); } /** *

* Appends timestamp information of the event to a {@link StringBuilder}. *

* * @param builder * {@link StringBuilder} where the information is appended */ private void addTimestampInfo(StringBuilder builder){ long timestamp = System.currentTimeMillis(); builder.append(" "); builder.append(StringTools.ENDLINE); } /** *

* Checks if the Id of an {@link AWTEvent} is a mouse movement Id. *

* * @param eventId * id of the {@link AWTEvent} * @return true, if the event is a mouse movement event; false otherwise */ private boolean isMouseMovement(int eventId) { return eventId == MouseEvent.MOUSE_MOVED || eventId == MouseEvent.MOUSE_DRAGGED || eventId == MouseEvent.MOUSE_ENTERED || eventId == MouseEvent.MOUSE_EXITED; } }