source: trunk/autoquest-jfcmonitor/src/main/java/de/ugoe/cs/autoquest/jfcmonitor/JFCListener.java @ 927

Last change on this file since 927 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
  • Property svn:mime-type set to text/plain
File size: 5.6 KB
Line 
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
15package de.ugoe.cs.autoquest.jfcmonitor;
16
17import java.awt.AWTEvent;
18import java.awt.Component;
19import java.awt.event.AWTEventListener;
20import java.awt.event.FocusEvent;
21import java.awt.event.KeyEvent;
22import java.awt.event.MouseEvent;
23
24import 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 */
36public 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                builder.append("</event>" + StringTools.ENDLINE);
83            }
84        }
85        else if (event instanceof KeyEvent) {
86            if (event.getID() == KeyEvent.KEY_PRESSED || event.getID() == KeyEvent.KEY_RELEASED) {
87                KeyEvent keyEvent = (KeyEvent) event;
88                builder.append("<event id=\"" + event.getID() + "\">" + StringTools.ENDLINE);
89                builder.append(" <param name=\"KeyCode\" value=\"" + keyEvent.getKeyCode() +
90                    "\" />" + StringTools.ENDLINE);
91                builder.append(" <param name=\"Modifiers\" value=\"" + keyEvent.getModifiers() +
92                    "\" />" + StringTools.ENDLINE);
93                addSourceInfo(builder, event);
94                builder.append("</event>" + StringTools.ENDLINE);
95            }
96        }
97        else if (event instanceof FocusEvent) {
98            if (event.getID() == FocusEvent.FOCUS_GAINED) {
99                builder.append("<event id=\"" + event.getID() + "\">" + StringTools.ENDLINE);
100                addSourceInfo(builder, event);
101                builder.append("</event>" + StringTools.ENDLINE);
102            }
103        }
104        if (builder.length() > 0 && outputWriter != null) {
105            outputWriter.write(builder.toString());
106        }
107    }
108
109    /**
110     * <p>
111     * Appends information about the event to a {@link StringBuilder}.
112     * </p>
113     *
114     * @param builder
115     *            {@link StringBuilder} where the information is appended
116     * @param event
117     *            event whose information is appended
118     */
119    private void addSourceInfo(StringBuilder builder, AWTEvent event) {
120        builder.append(" <source");
121        if (event.getSource() instanceof Component) {
122            Component source = (Component) event.getSource();
123            builder.append(" hash=\"" + Integer.toHexString(source.hashCode()) + "\"");
124        } 
125        builder.append(">" + StringTools.ENDLINE);
126        builder.append("  <param name=\"toString\" value=\"" +
127            StringTools.xmlEntityReplacement(event.getSource().toString()) + "\" />" +
128            StringTools.ENDLINE);
129        builder.append(" </source>" + StringTools.ENDLINE);
130    }
131
132    /**
133     * <p>
134     * Checks if the Id of an {@link AWTEvent} is a mouse movement Id.
135     * </p>
136     *
137     * @param eventId
138     *            id of the {@link AWTEvent}
139     * @return true, if the event is a mouse movement event; false otherwise
140     */
141    private boolean isMouseMovement(int eventId) {
142        return eventId == MouseEvent.MOUSE_MOVED || eventId == MouseEvent.MOUSE_DRAGGED ||
143            eventId == MouseEvent.MOUSE_ENTERED || eventId == MouseEvent.MOUSE_EXITED;
144    }
145
146}
Note: See TracBrowser for help on using the repository browser.