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

Last change on this file since 296 was 296, checked in by sherbold, 12 years ago
  • added null-checks to remove useless information from the logs, e.g., widget names that are "null"
  • Property svn:mime-type set to text/plain
File size: 5.4 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         * </p>
47         *
48         * @see java.awt.event.AWTEventListener#eventDispatched(java.awt.AWTEvent)
49         */
50        @Override
51        public void eventDispatched(AWTEvent event) {
52                StringBuilder builder = new StringBuilder();
53
54                if (event instanceof MouseEvent) {
55                        MouseEvent mouseEvent = (MouseEvent) event;
56                        if (!isMouseMovement(event.getID())) {
57                                builder.append("<event id=\"" + event.getID() + "\">" + ENDLINE);
58                                builder.append(" <param name=\"X\" value=\""
59                                                + mouseEvent.getX() + "\" />" + ENDLINE);
60                                builder.append(" <param name=\"Y\" value=\""
61                                                + mouseEvent.getY() + "\" />" + ENDLINE);
62                                builder.append(" <param name=\"Button\" value=\""
63                                                + mouseEvent.getButton() + "\" />" + ENDLINE);
64                                builder.append(" <param name=\"Modifiers\" value=\""
65                                                + mouseEvent.getModifiers() + "\" />" + ENDLINE);
66                                addSourceInfo(builder, event);
67                                builder.append("</event>" + ENDLINE);
68                        }
69                }
70                if (event instanceof KeyEvent) {
71                        KeyEvent keyEvent = (KeyEvent) event;
72                        if (keyEvent.getID() == KeyEvent.KEY_TYPED) {
73                                builder.append("<event id=\"" + event.getID() + "\">" + ENDLINE);
74                                builder.append(" <param name=\"KeyCode\" value=\""
75                                                + keyEvent.getKeyCode() + "\" />" + ENDLINE);
76                                builder.append(" <param name=\"Modifiers\" value=\""
77                                                + keyEvent.getModifiers() + "\" />" + ENDLINE);
78                                addSourceInfo(builder, event);
79                                builder.append("</event>" + ENDLINE);
80                        }
81                }
82                if (builder.length() > 0 && outputWriter != null) {
83                        try {
84                                outputWriter.write(builder.toString());
85                                outputWriter.flush();
86                        } catch (IOException e) {
87                                System.err.println("JFCMONITOR -- Failure writing to log: "
88                                                + e.getMessage());
89                        }
90                }
91        }
92
93        /**
94         * <p>
95         * Appends information about the event to a {@link StringBuilder}.
96         * </p>
97         *
98         * @param builder
99         *            {@link StringBuilder} where the information is appended
100         * @param event
101         *            event whose information is appended
102         */
103        private void addSourceInfo(StringBuilder builder, AWTEvent event) {
104                builder.append(" <source>" + ENDLINE);
105                builder.append("  <param name=\"toString\" value=\""
106                                + event.getSource().toString() + "\" />" + ENDLINE);
107                if (event.getSource() instanceof Component) {
108                        Component source = (Component) event.getSource();
109                        addComponentInfo(builder, source);
110                        builder.append(" <parent>" + ENDLINE);
111                        addComponentInfo(builder, source.getParent());
112                        builder.append(" </parent>" + ENDLINE);
113                }
114                builder.append(" </source>" + ENDLINE);
115        }
116
117        /**
118         * <p>
119         * Appends information about the component to the a {@link StringBuilder}.
120         * The prefix can be used to give further information about the component.
121         * </p>
122         *
123         * @param builder
124         *            {@link StringBuilder} where the information is appended
125         * @param component
126         *            component whose information is appended
127         */
128        private void addComponentInfo(StringBuilder builder, Component component) {
129                builder.append("  <param name=\"getName\" value=\""
130                                + component.getName() + "\" />" + ENDLINE);
131                for (Method method : component.getClass().getMethods()) {
132                        try {
133                                if (method.getName() == "getText") {
134                                        String text = (String) method.invoke(component,
135                                                        new Object[] {});
136                                        if( text!=null ) {
137                                                builder.append("  <param name=\"getText\" value=\"" + text + "\" />" + ENDLINE);
138                                        }
139                                }
140                                if (method.getName() == "getTitle") {
141                                        String title = (String) method.invoke(component,
142                                                        new Object[] {});
143                                        if( title!=null ) {
144                                                builder.append("  <param name=\"getTitle\" value=\"" + title + "\" />" + ENDLINE);
145                                        }
146                                }
147                        } catch (IllegalArgumentException e) {
148                        } catch (IllegalAccessException e) {
149                        } catch (InvocationTargetException e) {
150                                System.err.println("Found method with name " + method.getName() + " but could not access it.");
151                        }
152                }
153        }
154
155        /**
156         * <p>
157         * Checks if the Id of an {@link AWTEvent} is a mouse movement Id.
158         * </p>
159         *
160         * @param eventId
161         *            id of the {@link AWTEvent}
162         * @return true, if the event is a mouse movement event; false otherwise
163         */
164        private boolean isMouseMovement(int eventId) {
165                return eventId == MouseEvent.MOUSE_MOVED
166                                || eventId == MouseEvent.MOUSE_DRAGGED
167                                || eventId == MouseEvent.MOUSE_ENTERED
168                                || eventId == MouseEvent.MOUSE_EXITED;
169        }
170
171}
Note: See TracBrowser for help on using the repository browser.