- Timestamp:
- 12/05/11 14:49:53 (13 years ago)
- Location:
- trunk/JFCMonitor/src/de/ugoe/cs/eventbench/jfcmonitor
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JFCMonitor/src/de/ugoe/cs/eventbench/jfcmonitor/JFCListener.java
r268 r271 2 2 3 3 import java.awt.AWTEvent; 4 import java.awt.Component; 4 5 import java.awt.event.AWTEventListener; 5 6 import java.awt.event.KeyEvent; … … 10 11 import java.lang.reflect.Method; 11 12 12 import javax.swing.JComponent; 13 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 */ 14 22 public class JFCListener implements AWTEventListener { 15 23 16 final static String ENDLINE = System.getProperty("line.separator"); 17 18 final OutputStreamWriter outputWriter; 19 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 20 38 public JFCListener(OutputStreamWriter outputWriter) { 21 39 this.outputWriter = outputWriter; 22 40 } 23 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 */ 24 49 @Override 25 50 public void eventDispatched(AWTEvent event) { 26 51 StringBuilder builder = new StringBuilder(); 27 28 if ( event instanceof MouseEvent) {52 53 if (event instanceof MouseEvent) { 29 54 MouseEvent mouseEvent = (MouseEvent) event; 30 if ( !isMouseMovement(event.getID())) {55 if (!isMouseMovement(event.getID())) { 31 56 builder.append("<event id=\"" + event.getID() + "\">" + ENDLINE); 32 builder.append(" <param name=\"X\" value=\"" + mouseEvent.getX() + "\" />" + ENDLINE); 33 builder.append(" <param name=\"Y\" value=\"" + mouseEvent.getY() + "\" />" + ENDLINE); 34 builder.append(" <param name=\"Button\" value=\"" + mouseEvent.getButton() + "\" />" + ENDLINE); 35 builder.append(" <param name=\"Modifiers\" value=\"" + mouseEvent.getModifiers() + "\" />" + 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); 36 65 addSourceInfo(builder, event); 37 66 builder.append("</event>" + ENDLINE); 38 67 } 39 68 } 40 if ( event instanceof KeyEvent) {69 if (event instanceof KeyEvent) { 41 70 KeyEvent keyEvent = (KeyEvent) event; 42 if ( keyEvent.getID()==KeyEvent.KEY_TYPED) {71 if (keyEvent.getID() == KeyEvent.KEY_TYPED) { 43 72 builder.append("<event id=\"" + event.getID() + "\">" + ENDLINE); 44 builder.append(" <param name=\"KeyCode\" value=\"" + keyEvent.getKeyCode() + "\" />" + ENDLINE); 45 builder.append(" <param name=\"Modifiers\" value=\"" + keyEvent.getModifiers() + "\" />" + ENDLINE); 73 builder.append(" <param name=\"KeyCode\" value=\"" 74 + keyEvent.getKeyCode() + "\" />" + ENDLINE); 75 builder.append(" <param name=\"Modifiers\" value=\"" 76 + keyEvent.getModifiers() + "\" />" + ENDLINE); 46 77 addSourceInfo(builder, event); 47 78 builder.append("</event>" + ENDLINE); 48 79 } 49 80 } 50 if ( builder.length()>0 && outputWriter!=null) {81 if (builder.length() > 0 && outputWriter != null) { 51 82 try { 52 83 outputWriter.write(builder.toString()); 53 84 outputWriter.flush(); 54 85 } catch (IOException e) { 55 // TODO Auto-generated catch block56 e.printStackTrace();86 System.err.println("JFCMONITOR -- Failure writing to log: " 87 + e.getMessage()); 57 88 } 58 89 } 59 90 } 60 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 */ 61 102 private void addSourceInfo(StringBuilder builder, AWTEvent event) { 62 builder.append(" <param name=\"Source\" value=\"" + event.getSource().toString() + "\" />" + ENDLINE); 63 if( event.getSource() instanceof JComponent ) { 64 JComponent source = (JComponent) event.getSource(); 65 builder.append(" <param name=\"SourceName\" value=\"" + source.getName() + "\" />" + ENDLINE); 66 for(Method method : source.getClass().getMethods() ) { 67 try { 68 if( method.getName()=="getText" ) { 69 String text = (String) method.invoke(source, new Object[]{}); 70 builder.append(" <param name=\"SourceText\" value=\"" + text + "\" />" + ENDLINE); 71 } 72 if( method.getName()=="getTitle" ) { 73 String title = (String) method.invoke(source, new Object[]{}); 74 builder.append(" <param name=\"SourceTitle\" value=\"" + title + "\" />" + ENDLINE); 75 } 76 } catch (IllegalArgumentException e) { 77 } catch (IllegalAccessException e) { 78 } catch (InvocationTargetException e) { 79 } 80 } 81 /* TODO should include info about parent 82 builder.append(" <param name=\"Parent\" value=\"" + source.getParent().toString() + "\" />" + ENDLINE); 83 builder.append(" <param name=\"ParentName\" value=\"" + source.getParent().getName() + "\" />" + ENDLINE); 84 */ 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"); 85 109 } 86 110 } 87 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 */ 88 159 private boolean isMouseMovement(int eventId) { 89 return eventId==MouseEvent.MOUSE_MOVED || eventId==MouseEvent.MOUSE_DRAGGED || eventId==MouseEvent.MOUSE_ENTERED || eventId==MouseEvent.MOUSE_EXITED; 160 return eventId == MouseEvent.MOUSE_MOVED 161 || eventId == MouseEvent.MOUSE_DRAGGED 162 || eventId == MouseEvent.MOUSE_ENTERED 163 || eventId == MouseEvent.MOUSE_EXITED; 90 164 } 91 165 -
trunk/JFCMonitor/src/de/ugoe/cs/eventbench/jfcmonitor/Runner.java
r268 r271 1 1 package de.ugoe.cs.eventbench.jfcmonitor; 2 2 3 import java.awt.AWTEvent; 3 4 import java.awt.Toolkit; 4 5 import java.awt.event.AWTEventListener; 6 import java.io.FileWriter; 7 import java.io.IOException; 5 8 import java.io.OutputStreamWriter; 6 9 import java.util.Arrays; 7 10 8 11 /** 12 * <p> 13 * Start-up class of the application. 14 * </p> 15 * <p> 16 * Launches the application under test in the same JVM. 17 * </p> 18 * 19 * @author Steffen Herbold 20 * @version 1.0 21 */ 9 22 public class Runner { 10 23 11 public static void main(String[] args) throws Exception { 12 AWTEventListener listener = new JFCListener(new OutputStreamWriter(System.out)); 13 14 Toolkit.getDefaultToolkit().addAWTEventListener(listener, 24 /** 25 * <p> 26 * Name of the log file. 27 * </p> 28 */ 29 private final static String logfileName = "jfcmonitor.log"; 30 31 /** 32 * <p> 33 * Debugging variable. If set to true, the logging is also written to the 34 * console. 35 * </p> 36 */ 37 private final static boolean stdOutputWrite = true; 38 39 /** 40 * <p> 41 * Main method of the application. 42 * </p> 43 * 44 * @param args 45 * the first parameter defines the Jar file that contains the 46 * start-up information of the application under test. The 47 * remaining parameters are passed on the toe application under 48 * test. 49 */ 50 public static void main(String[] args) { 51 FileWriter writer; 52 try { 53 // the writer is not closed explicitly! 54 writer = new FileWriter(logfileName, true); 55 writer.write("<newsession />"); 56 } catch (IOException e) { 57 System.err.println("JFCMONITOR -- failure opening logfile: " 58 + e.getMessage()); 59 return; 60 } 61 62 AWTEventListener listenerFile = new JFCListener(writer); 63 Toolkit.getDefaultToolkit().addAWTEventListener(listenerFile, 15 64 AWTEvent.KEY_EVENT_MASK); 16 Toolkit.getDefaultToolkit().addAWTEventListener(listener, AWTEvent.MOUSE_EVENT_MASK); 17 18 JarLauncher launcher = new JarLauncher(args[0], Arrays.copyOfRange(args, 1, args.length)); 65 Toolkit.getDefaultToolkit().addAWTEventListener(listenerFile, 66 AWTEvent.MOUSE_EVENT_MASK); 67 68 if (stdOutputWrite) { 69 AWTEventListener listenerStdOut = new JFCListener( 70 new OutputStreamWriter(System.out)); 71 Toolkit.getDefaultToolkit().addAWTEventListener(listenerStdOut, 72 AWTEvent.KEY_EVENT_MASK); 73 Toolkit.getDefaultToolkit().addAWTEventListener(listenerStdOut, 74 AWTEvent.MOUSE_EVENT_MASK); 75 } 76 77 JarLauncher launcher = new JarLauncher(args[0], Arrays.copyOfRange( 78 args, 1, args.length)); 19 79 launcher.exec(); 20 80 }
Note: See TracChangeset
for help on using the changeset viewer.