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

Last change on this file since 271 was 271, checked in by sherbold, 12 years ago
  • JFCMonitor now writes to a logfile jfcmonitor.log located in the working directory.
  • logged information now includes the symbolic name of the GUI components (optional, may be null)
  • code documentation and formatting
  • Property svn:mime-type set to text/plain
File size: 5.2 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.KeyEvent;
7import java.awt.event.MouseEvent;
8import java.io.IOException;
9import java.io.OutputStreamWriter;
10import java.lang.reflect.InvocationTargetException;
11import java.lang.reflect.Method;
12
13/**
14 * <p>
15 * This class implements monitoring of AWT and Swing mouse and keyboard events.
16 * Each of the events is written to an output stream.
17 * </p>
18 *
19 * @author Steffen Herbold
20 * @version 1.0
21 */
22public class JFCListener implements AWTEventListener {
23
24        /**
25         * <p>
26         * Convenience variable.
27         * </p>
28         */
29        final static private String ENDLINE = System.getProperty("line.separator");
30
31        /**
32         * <p>
33         * Writer for logging events.
34         * </p>
35         */
36        final private OutputStreamWriter outputWriter;
37
38        public JFCListener(OutputStreamWriter outputWriter) {
39                this.outputWriter = outputWriter;
40        }
41
42        /**
43         * <p>
44         * Writes all received {@link MouseEvent}s and {@link KeyEvent}s to the
45         * {@link #outputWriter}.
46         *
47         * @see java.awt.event.AWTEventListener#eventDispatched(java.awt.AWTEvent)
48         */
49        @Override
50        public void eventDispatched(AWTEvent event) {
51                StringBuilder builder = new StringBuilder();
52
53                if (event instanceof MouseEvent) {
54                        MouseEvent mouseEvent = (MouseEvent) event;
55                        if (!isMouseMovement(event.getID())) {
56                                builder.append("<event id=\"" + event.getID() + "\">" + ENDLINE);
57                                builder.append(" <param name=\"X\" value=\""
58                                                + mouseEvent.getX() + "\" />" + ENDLINE);
59                                builder.append(" <param name=\"Y\" value=\""
60                                                + mouseEvent.getY() + "\" />" + ENDLINE);
61                                builder.append(" <param name=\"Button\" value=\""
62                                                + mouseEvent.getButton() + "\" />" + ENDLINE);
63                                builder.append(" <param name=\"Modifiers\" value=\""
64                                                + mouseEvent.getModifiers() + "\" />" + ENDLINE);
65                                addSourceInfo(builder, event);
66                                builder.append("</event>" + ENDLINE);
67                        }
68                }
69                if (event instanceof KeyEvent) {
70                        KeyEvent keyEvent = (KeyEvent) event;
71                        if (keyEvent.getID() == KeyEvent.KEY_TYPED) {
72                                builder.append("<event id=\"" + event.getID() + "\">" + ENDLINE);
73                                builder.append(" <param name=\"KeyCode\" value=\""
74                                                + keyEvent.getKeyCode() + "\" />" + ENDLINE);
75                                builder.append(" <param name=\"Modifiers\" value=\""
76                                                + keyEvent.getModifiers() + "\" />" + ENDLINE);
77                                addSourceInfo(builder, event);
78                                builder.append("</event>" + ENDLINE);
79                        }
80                }
81                if (builder.length() > 0 && outputWriter != null) {
82                        try {
83                                outputWriter.write(builder.toString());
84                                outputWriter.flush();
85                        } catch (IOException e) {
86                                System.err.println("JFCMONITOR -- Failure writing to log: "
87                                                + e.getMessage());
88                        }
89                }
90        }
91
92        /**
93         * <p>
94         * Appends information about the event to a {@link StringBuilder}.
95         * </p>
96         *
97         * @param builder
98         *            {@link StringBuilder} where the information is appended
99         * @param event
100         *            event whose information is appended
101         */
102        private void addSourceInfo(StringBuilder builder, AWTEvent event) {
103                builder.append(" <param name=\"Source\" value=\""
104                                + event.getSource().toString() + "\" />" + ENDLINE);
105                if (event.getSource() instanceof Component) {
106                        Component source = (Component) event.getSource();
107                        addComponentInfo(builder, source, "Source");
108                        addComponentInfo(builder, source.getParent(), "Parent");
109                }
110        }
111
112        /**
113         * <p>
114         * Appends information about the component to the a {@link StringBuilder}.
115         * The prefix can be used to give further information about the component.
116         * </p>
117         *
118         * @param builder
119         *            {@link StringBuilder} where the information is appended
120         * @param component
121         *            component whose information is appended
122         * @param prefix
123         *            prefix to give further information about the component
124         */
125        private void addComponentInfo(StringBuilder builder, Component component,
126                        String prefix) {
127                builder.append(" <param name=\"" + prefix + "Name\" value=\""
128                                + component.getName() + "\" />" + ENDLINE);
129                for (Method method : component.getClass().getMethods()) {
130                        try {
131                                if (method.getName() == "getText") {
132                                        String text = (String) method.invoke(component,
133                                                        new Object[] {});
134                                        builder.append(" <param name=\"" + prefix
135                                                        + "Text\" value=\"" + text + "\" />" + ENDLINE);
136                                }
137                                if (method.getName() == "getTitle") {
138                                        String title = (String) method.invoke(component,
139                                                        new Object[] {});
140                                        builder.append(" <param name=\"" + prefix
141                                                        + "Title\" value=\"" + title + "\" />" + ENDLINE);
142                                }
143                        } catch (IllegalArgumentException e) {
144                        } catch (IllegalAccessException e) {
145                        } catch (InvocationTargetException e) {
146                        }
147                }
148        }
149
150        /**
151         * <p>
152         * Checks if the Id of an {@link AWTEvent} is a mouse movement Id.
153         * </p>
154         *
155         * @param eventId
156         *            id of the {@link AWTEvent}
157         * @return true, if the event is a mouse movement event; false otherwise
158         */
159        private boolean isMouseMovement(int eventId) {
160                return eventId == MouseEvent.MOUSE_MOVED
161                                || eventId == MouseEvent.MOUSE_DRAGGED
162                                || eventId == MouseEvent.MOUSE_ENTERED
163                                || eventId == MouseEvent.MOUSE_EXITED;
164        }
165
166}
Note: See TracBrowser for help on using the repository browser.