Peer classes for AWT/Swing Listeners/Adapters

2004-11-21
2013-04-29
  • John Swensen
    John Swensen
    2004-11-21

    I think I have come up with a decent way of handling AWT/Swing events without having to create a peer for each different handler.  This is based somewhat on a post I saw on one of these forums, but can't find again.  Here is the basic process.  I can automate it if it is OK with you.

    1) Generate Java class called Peer<name of listener class> (e.g. PeerMouseListener).  It would look like the following:
    public class PeerMouseListener implements MouseListener{
       long classPtr = -1;
       public PeerMouseListener( long ptr ) { classPtr = ptr; }

       public native void mouseClicked( long ptr, MouseEvent me );
       public void mouseClicked( MouseEvent me ) {
          mouseClicked( classPtr, me );
       }

       // Place all other interface functions here.
    }

    2) Run the PeerGenerator on the class.  Also create all the required proxies.  To the PeerMouseListener C++ class, add purely virtual functions for each of the interface functions.  Following my example:
        virtual void PeerMouseListener::mouseClicked( jace::proxy::java::awt::event::MouseEvent me ) = 0; 

    3) When implementing the native functions, cast the long to the proxy type and call the appropriate virtual function.

    4) Use these results by deriving from the proxy andimplementing the necessary purely virtual functions.  Following the example:
        class TestMouseListener : public PeerMouseListened {
            TestMouseListener() : PeerMouseListener(this) {};
            virtual void mouseClicked( MouseEvent me ) {
                // Place handler code here
            }
        }

    All of this could be completely automated, making it so the Peer<name of listener> class could simply be derived and used.

    Would you prefer to have this functionality integrated into the existing PeerGenerator and ProxyGenerator (via command line options or something similar), or do you think it should be a separate utility?  Let me know and I will start working on it.

    John Swensen

     
    • Joe T
      Joe T
      2005-08-16

      Hi,

      Suppose that a Java library provides mean to addListener() to a java class.  Would the C++ program be able to receive live event triggers from the Java class?

      Thanks,
      Joe