Changeset 271


Ignore:
Timestamp:
12/05/11 14:49:53 (12 years ago)
Author:
sherbold
Message:
  • 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
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  
    22 
    33import java.awt.AWTEvent; 
     4import java.awt.Component; 
    45import java.awt.event.AWTEventListener; 
    56import java.awt.event.KeyEvent; 
     
    1011import java.lang.reflect.Method; 
    1112 
    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 */ 
    1422public class JFCListener implements AWTEventListener { 
    1523 
    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 
    2038        public JFCListener(OutputStreamWriter outputWriter) { 
    2139                this.outputWriter = outputWriter; 
    2240        } 
    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         */ 
    2449        @Override 
    2550        public void eventDispatched(AWTEvent event) { 
    2651                StringBuilder builder = new StringBuilder(); 
    27                                  
    28                 if( event instanceof MouseEvent ) { 
     52 
     53                if (event instanceof MouseEvent) { 
    2954                        MouseEvent mouseEvent = (MouseEvent) event; 
    30                         if( !isMouseMovement(event.getID()) ) { 
     55                        if (!isMouseMovement(event.getID())) { 
    3156                                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); 
    3665                                addSourceInfo(builder, event); 
    3766                                builder.append("</event>" + ENDLINE); 
    3867                        } 
    3968                } 
    40                 if( event instanceof KeyEvent ) { 
     69                if (event instanceof KeyEvent) { 
    4170                        KeyEvent keyEvent = (KeyEvent) event; 
    42                         if( keyEvent.getID()==KeyEvent.KEY_TYPED ) { 
     71                        if (keyEvent.getID() == KeyEvent.KEY_TYPED) { 
    4372                                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); 
    4677                                addSourceInfo(builder, event); 
    4778                                builder.append("</event>" + ENDLINE); 
    4879                        } 
    4980                } 
    50                 if( builder.length()>0 && outputWriter!=null ) { 
     81                if (builder.length() > 0 && outputWriter != null) { 
    5182                        try { 
    5283                                outputWriter.write(builder.toString()); 
    5384                                outputWriter.flush(); 
    5485                        } catch (IOException e) { 
    55                                 // TODO Auto-generated catch block 
    56                                 e.printStackTrace(); 
     86                                System.err.println("JFCMONITOR -- Failure writing to log: " 
     87                                                + e.getMessage()); 
    5788                        } 
    5889                } 
    5990        } 
    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         */ 
    61102        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"); 
    85109                } 
    86110        } 
    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         */ 
    88159        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; 
    90164        } 
    91165 
  • trunk/JFCMonitor/src/de/ugoe/cs/eventbench/jfcmonitor/Runner.java

    r268 r271  
    11package de.ugoe.cs.eventbench.jfcmonitor; 
     2 
    23import java.awt.AWTEvent; 
    34import java.awt.Toolkit; 
    45import java.awt.event.AWTEventListener; 
     6import java.io.FileWriter; 
     7import java.io.IOException; 
    58import java.io.OutputStreamWriter; 
    69import java.util.Arrays; 
    710 
    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 */ 
    922public class Runner { 
    1023 
    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, 
    1564                                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)); 
    1979                launcher.exec(); 
    2080        } 
Note: See TracChangeset for help on using the changeset viewer.