EventTracerTest.java Source code

Java tutorial

Introduction

Here is the source code for EventTracerTest.java

Source

/*
   This program is a part of the companion code for Core Java 8th ed.
   (http://horstmann.com/corejava)
    
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
    
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
    
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.EventQueue;
import java.beans.BeanInfo;
import java.beans.EventSetDescriptor;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JSlider;

/**
 * @version 1.13 2007-06-12
 * @author Cay Horstmann
 */
public class EventTracerTest {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new EventTracerFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}

class EventTracerFrame extends JFrame {
    public EventTracerFrame() {
        setTitle("EventTracerTest");
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

        // add a slider and a button
        add(new JSlider(), BorderLayout.NORTH);
        add(new JButton("Test"), BorderLayout.SOUTH);

        // trap all events of components inside the frame
        EventTracer tracer = new EventTracer();
        tracer.add(this);
    }

    public static final int DEFAULT_WIDTH = 400;
    public static final int DEFAULT_HEIGHT = 400;
}

/*
   This program is a part of the companion code for Core Java 8th ed.
   (http://horstmann.com/corejava)
    
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
    
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
    
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @version 1.31 2004-05-10
 * @author Cay Horstmann
 */
class EventTracer {
    public EventTracer() {
        // the handler for all event proxies
        handler = new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) {
                System.out.println(method + ":" + args[0]);
                return null;
            }
        };
    }

    /**
     * Adds event tracers for all events to which this component and its children can listen
     * @param c a component
     */
    public void add(Component c) {
        try {
            // get all events to which this component can listen
            BeanInfo info = Introspector.getBeanInfo(c.getClass());

            EventSetDescriptor[] eventSets = info.getEventSetDescriptors();
            for (EventSetDescriptor eventSet : eventSets)
                addListener(c, eventSet);
        } catch (IntrospectionException e) {
        }
        // ok not to add listeners if exception is thrown

        if (c instanceof Container) {
            // get all children and call add recursively
            for (Component comp : ((Container) c).getComponents())
                add(comp);
        }
    }

    /**
     * Add a listener to the given event set
     * @param c a component
     * @param eventSet a descriptor of a listener interface
     */
    public void addListener(Component c, EventSetDescriptor eventSet) {
        // make proxy object for this listener type and route all calls to the handler
        Object proxy = Proxy.newProxyInstance(null, new Class[] { eventSet.getListenerType() }, handler);

        // add the proxy as a listener to the component
        Method addListenerMethod = eventSet.getAddListenerMethod();
        try {
            addListenerMethod.invoke(c, proxy);
        } catch (InvocationTargetException e) {
        } catch (IllegalAccessException e) {
        }
        // ok not to add listener if exception is thrown
    }

    private InvocationHandler handler;
}