Changeset 268
- Timestamp:
- 12/02/11 14:35:09 (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
r263 r268 5 5 import java.awt.event.KeyEvent; 6 6 import java.awt.event.MouseEvent; 7 import java.io.IOException; 8 import java.io.OutputStreamWriter; 9 import java.lang.reflect.InvocationTargetException; 10 import java.lang.reflect.Method; 11 12 import javax.swing.JComponent; 7 13 8 14 public class JFCListener implements AWTEventListener { 9 15 16 final static String ENDLINE = System.getProperty("line.separator"); 17 18 final OutputStreamWriter outputWriter; 19 20 public JFCListener(OutputStreamWriter outputWriter) { 21 this.outputWriter = outputWriter; 22 } 23 10 24 @Override 11 25 public void eventDispatched(AWTEvent event) { 12 // TODO implement event handler in a way that monitors the click instead of just writing strings to the console 26 StringBuilder builder = new StringBuilder(); 27 13 28 if( event instanceof MouseEvent ) { 14 29 MouseEvent mouseEvent = (MouseEvent) event; 15 if( mouseEvent.getID()==MouseEvent.MOUSE_CLICKED ) { 16 System.out.println(event.toString()); 30 if( !isMouseMovement(event.getID()) ) { 31 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); 36 addSourceInfo(builder, event); 37 builder.append("</event>" + ENDLINE); 17 38 } 18 39 } … … 20 41 KeyEvent keyEvent = (KeyEvent) event; 21 42 if( keyEvent.getID()==KeyEvent.KEY_TYPED ) { 22 System.out.println(event.toString()); 43 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); 46 addSourceInfo(builder, event); 47 builder.append("</event>" + ENDLINE); 48 } 49 } 50 if( builder.length()>0 && outputWriter!=null ) { 51 try { 52 outputWriter.write(builder.toString()); 53 outputWriter.flush(); 54 } catch (IOException e) { 55 // TODO Auto-generated catch block 56 e.printStackTrace(); 23 57 } 24 58 } 25 59 } 60 61 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 */ 85 } 86 } 87 88 private boolean isMouseMovement(int eventId) { 89 return eventId==MouseEvent.MOUSE_MOVED || eventId==MouseEvent.MOUSE_DRAGGED || eventId==MouseEvent.MOUSE_ENTERED || eventId==MouseEvent.MOUSE_EXITED; 90 } 26 91 27 92 } -
trunk/JFCMonitor/src/de/ugoe/cs/eventbench/jfcmonitor/Runner.java
r263 r268 3 3 import java.awt.Toolkit; 4 4 import java.awt.event.AWTEventListener; 5 import java.io.OutputStreamWriter; 5 6 import java.util.Arrays; 6 7 … … 9 10 10 11 public static void main(String[] args) throws Exception { 11 AWTEventListener listener = new JFCListener( );12 AWTEventListener listener = new JFCListener(new OutputStreamWriter(System.out)); 12 13 13 14 Toolkit.getDefaultToolkit().addAWTEventListener(listener,
Note: See TracChangeset
for help on using the changeset viewer.