source: trunk/JFCMonitor/src/de/ugoe/cs/eventbench/jfcmonitor/JFCListener.java @ 371

Last change on this file since 371 was 371, checked in by sherbold, 12 years ago
  • JFCMonitor monitors KEY_PRESSED and KEY_RELEASED instead of KEY_TYPED events, because of various drawbacks of key typed (does not receive all inputs, like pressing enter and the events only offer the actual char pressed, not its virtual code, which leads to all kinds of problems, e.g., in case of backspace)
  • Property svn:mime-type set to text/plain
File size: 5.0 KB
Line 
1package de.ugoe.cs.eventbench.jfcmonitor;
2
3import java.awt.AWTEvent;
4import java.awt.Component;
5import java.awt.event.AWTEventListener;
6import java.awt.event.FocusEvent;
7import java.awt.event.KeyEvent;
8import java.awt.event.MouseEvent;
9import java.io.IOException;
10import java.io.OutputStreamWriter;
11
12import de.ugoe.cs.util.StringTools;
13
14/**
15 * <p>
16 * This class implements monitoring of AWT and Swing mouse and keyboard events.
17 * Each of the events is written to an output stream.
18 * </p>
19 *
20 * @author Steffen Herbold
21 * @version 1.0
22 */
23public class JFCListener implements AWTEventListener {
24
25        /**
26         * <p>
27         * Writer for logging events.
28         * </p>
29         */
30        final private OutputStreamWriter outputWriter;
31
32        /**
33         * <p>
34         * Constructor. Creates a new JFCListener with a given
35         * {@link OutputStreamWriter}, where the monitored information is logged.
36         * </p>
37         *
38         * @param outputWriter
39         *            writer for the logged information
40         */
41        public JFCListener(OutputStreamWriter outputWriter) {
42                this.outputWriter = outputWriter;
43                try {
44                        outputWriter.write("<?xml version=\"1.0\" encoding=\"UTF-16\"?>"
45                                        + StringTools.ENDLINE);
46                        outputWriter.write("<sessions>" + StringTools.ENDLINE);
47                } catch (IOException e) {
48                        System.err.println("JFCMONITOR -- Failure writing to log: "
49                                        + e.getMessage());
50                }
51        }
52
53        /**
54         * <p>
55         * Writes all received {@link MouseEvent}s and {@link KeyEvent}s to the
56         * {@link #outputWriter}.
57         * </p>
58         *
59         * @see java.awt.event.AWTEventListener#eventDispatched(java.awt.AWTEvent)
60         */
61        @Override
62        public void eventDispatched(AWTEvent event) {
63                StringBuilder builder = new StringBuilder();
64
65                if (event instanceof MouseEvent) {
66                        MouseEvent mouseEvent = (MouseEvent) event;
67                        if (!isMouseMovement(event.getID())) {
68                                builder.append("<event id=\"" + event.getID() + "\">"
69                                                + StringTools.ENDLINE);
70                                builder.append(" <param name=\"X\" value=\""
71                                                + mouseEvent.getX() + "\" />" + StringTools.ENDLINE);
72                                builder.append(" <param name=\"Y\" value=\""
73                                                + mouseEvent.getY() + "\" />" + StringTools.ENDLINE);
74                                builder.append(" <param name=\"Button\" value=\""
75                                                + mouseEvent.getButton() + "\" />"
76                                                + StringTools.ENDLINE);
77                                builder.append(" <param name=\"Modifiers\" value=\""
78                                                + mouseEvent.getModifiers() + "\" />"
79                                                + StringTools.ENDLINE);
80                                addSourceInfo(builder, event);
81                                builder.append("</event>" + StringTools.ENDLINE);
82                        }
83                }
84                if (event instanceof KeyEvent) {
85                        KeyEvent keyEvent = (KeyEvent) event;
86                        if (keyEvent.getID() == KeyEvent.KEY_PRESSED
87                                        || keyEvent.getID() == KeyEvent.KEY_RELEASED) {
88                                builder.append("<event id=\"" + event.getID() + "\">"
89                                                + StringTools.ENDLINE);
90                                builder.append(" <param name=\"KeyCode\" value=\""
91                                                + keyEvent.getKeyCode() + "\" />" + StringTools.ENDLINE);
92                                builder.append(" <param name=\"Modifiers\" value=\""
93                                                + keyEvent.getModifiers() + "\" />"
94                                                + StringTools.ENDLINE);
95                                addSourceInfo(builder, event);
96                                builder.append("</event>" + StringTools.ENDLINE);
97                        }
98                }
99                if (event instanceof FocusEvent) {
100                        FocusEvent focusEvent = (FocusEvent) event;
101                        if (focusEvent.getID() == FocusEvent.FOCUS_GAINED) {
102                                builder.append("<event id=\"" + event.getID() + "\">"
103                                                + StringTools.ENDLINE);
104                                addSourceInfo(builder, event);
105                                builder.append("</event>" + StringTools.ENDLINE);
106                        }
107                }
108                if (builder.length() > 0 && outputWriter != null) {
109                        try {
110                                outputWriter.write(builder.toString());
111                                outputWriter.flush();
112                        } catch (IOException e) {
113                                System.err.println("JFCMONITOR -- Failure writing to log: "
114                                                + e.getMessage());
115                        }
116                }
117        }
118
119        /**
120         * <p>
121         * Appends information about the event to a {@link StringBuilder}.
122         * </p>
123         *
124         * @param builder
125         *            {@link StringBuilder} where the information is appended
126         * @param event
127         *            event whose information is appended
128         */
129        private void addSourceInfo(StringBuilder builder, AWTEvent event) {
130                builder.append(" <source>" + StringTools.ENDLINE);
131                builder.append("  <param name=\"toString\" value=\""
132                                + StringTools
133                                                .xmlEntityReplacement(event.getSource().toString())
134                                + "\" />" + StringTools.ENDLINE);
135                if (event.getSource() instanceof Component) {
136                        Component source = (Component) event.getSource();
137                        JFCComponent jfcComponent = JFCComponent.find(source);
138                        if (jfcComponent != null) {
139                                builder.append(jfcComponent.getXML());
140                        }
141                }
142                builder.append(" </source>" + StringTools.ENDLINE);
143        }
144
145        /**
146         * <p>
147         * Checks if the Id of an {@link AWTEvent} is a mouse movement Id.
148         * </p>
149         *
150         * @param eventId
151         *            id of the {@link AWTEvent}
152         * @return true, if the event is a mouse movement event; false otherwise
153         */
154        private boolean isMouseMovement(int eventId) {
155                return eventId == MouseEvent.MOUSE_MOVED
156                                || eventId == MouseEvent.MOUSE_DRAGGED
157                                || eventId == MouseEvent.MOUSE_ENTERED
158                                || eventId == MouseEvent.MOUSE_EXITED;
159        }
160
161}
Note: See TracBrowser for help on using the repository browser.