package textbender.o.rhinohide.events; // Copyright 2007, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Textbender Software"), to deal in the Textbender Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Textbender Software, and to permit persons to whom the Textbender Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Textbender Software. THE TEXTBENDER SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TEXTBENDER SOFTWARE OR THE USE OR OTHER DEALINGS IN THE TEXTBENDER SOFTWARE. import netscape.javascript.JSObject; import textbender.g.lang.ThreadSafe; import textbender.o.rhinohide.*; import textbender.o.rhinohide._.*; /** A synchronous relay, based on an intermediate JavaScript listener/relay [but with bugs] * The JavaScript side listener/relay pushes events out to the Java side. *

* Suffers same bugs as {@linkplain RelayS RelayS}. * Try {@linkplain RelaySIP RelaySIP}, instead. *

*/ public abstract class RelaySI extends Relay { /** Constructs a RelaySI. */ public RelaySI() { dispatchAdaptor = new DispatchAdaptor(); } /** Constructs a modified RelaySI. * * @param dummyFlag ignored, serves only to discriminate constructor */ RelaySI( boolean dummyFlag ) {} // ------------------------------------------------------------------------------------ @ThreadSafe JSObject createJSORelay_orNull( RhiWindow window ) { return (JSObject)window.relayFactorySI().call( "createRelay" ); } /** Called from JavaScript side when event occurs. * Wraps it as Java DOM Event, and forwards it to EventListener.this. */ Object dispatchAdaptor; // final after subclass init private boolean isInit; /** The listener/relay on the JavaScript side. * It's a free object on that side, subject to garbage collection, * so it should cleanly evaporate when we're done with it. */ Rhinohide rhiRelay; // lazy init, thence final /** Implementation of {@linkplain #rhiRelay rhiRelay} listener, * expressed as a script fragment. * [This will later be made private.] */ public static final String HANDLE_EVENT_SCRIPT = "" + " relay.handleEvent = function( e ) \n" + " { \n" // + " this.dispatchAdaptor.handleEvent( e ); \n" ////// this.dispatchAdaptor has no methods/properties. Maybe because handleEvent not called as method of an object? [That's right, it's documented that 'this' will be the event target.] Could register entire listener/relay as an object, using addEventListener(this). But that's non-standard, it's supposed to be a bare function. + " relay.dispatchAdaptor.handleEvent( e ); \n" ////// or maybe this is cleaner: // + " dispatchAdaptor.handleEvent( e ); \n" + " }; \n" ; // - E v e n t - R e l a y ------------------------------------------------------------ /** Adds a JavaScript relay to the specified target, as a listener. * The JavaScript relay pushes events out to this Java relay, * which then wraps them and passes them on to the Java listener. */ @ThreadSafe void addTo( final RhiEventTarget target, final String type, final boolean useCapture ) throws StubbornRhinoException { init: synchronized( RelaySI.this ) // atomic init { if( isInit ) break init; checkThreadSafe( RelaySI.this ); JSObject jsoRelay = createJSORelay_orNull( target.window() ); if( jsoRelay == null ) throw new StubbornRhinoException( "sorry, you probably need a faster computer (or more memory) to do this" ); rhiRelay = new Rhinohide( target.window(), jsoRelay ); rhiRelay.setMember( "dispatchAdaptor", dispatchAdaptor ); isInit = true; } rhiRelay.call( "addTo", target.jsObject(), type, useCapture ); } @ThreadSafe void removeFrom( final RhiEventTarget target, final String type, final boolean useCapture ) { synchronized( RelaySI.this ) {} // update thread's memory cache if( rhiRelay == null ) { assert false; return; } rhiRelay.call( "removeFrom", target.jsObject(), type, useCapture ); } // ==================================================================================== /** Relay adaptor that wraps each event from the JavaScript side, * as a proper Java {@linkplain org.w3c.dom.events.Event Event}. * [This is implementation, made public only for technical reasons.] */ public final class DispatchAdaptor // must not be private, or dispatch fails { public @ThreadSafe void handleEvent( JSObject eventJS ) { // System.out.println( "ERSI.DC event: " + eventJS ); // System.out.println( eventJS.getMember( "target" )); ///// eventJS.getMember("target") throws NullPointerException. RelaySI.this.handleEvent ( RhiEvent.wrapEvent( rhiRelay.window(), eventJS, /*isAsync*/false )); } } }