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.RhiWindow; /** A basic synchronous relay [but with bugs]. It takes care of wrapping each event, * as a proper Java {@linkplain org.w3c.dom.events.Event Event}. * *

BUG: null-pointer-member

*
* http://reluk.ca/var/cache/textbender-javadoc/textbender/o/rhinohide/RelayS.html#null-pointer-member *
*

* E.g. on * system/host/obsidian/linux.xht#firefox. * JSObject.getMember() throws NullPointerException * for objects pushed out (JavaScript to Java). *

* *

BUG: no-QueryInterface

*
* http://reluk.ca/var/cache/textbender-javadoc/textbender/o/rhinohide/RelayS.html#no-QueryInterface *
*

* E.g. on * system/host/obsidian/linux.xht#firefox. * As events occur, browser fills logs with * Error: Java class textbender.o.rhinohide.events.RelayS$DispatchAdaptor has no public field or method named "QueryInterface". *

*/ public abstract class RelayS extends Relay { private final DispatchAdaptor dispatchAdaptor = new DispatchAdaptor(); private boolean isInit; private RhiWindow window; // - E v e n t - R e l a y ------------------------------------------------------------ /** Adds this relay directly to the specified target, as a listener. */ @ThreadSafe void addTo( final RhiEventTarget target, final String type, final boolean useCapture ) { textbender.g.util.logging.LoggerX.i(getClass()).warning( "never tested that it prevents duplicate registrations" ); init: synchronized( RelayS.this ) // atomic test/set, and per below (2) { if( isInit ) break init; isInit = true; checkThreadSafe( RelayS.this ); window = target.window(); } target.call( "addEventListener", type, dispatchAdaptor, useCapture ); // 2. thread var cache refreshed by above sync } @ThreadSafe void removeFrom( final RhiEventTarget target, final String type, final boolean useCapture ) { textbender.g.util.logging.LoggerX.i(getClass()).warning( "never tested that it actually removes" ); synchronized( RelayS.this ) {} // thread var cache refreshed target.call( "removeEventListener", type, dispatchAdaptor, 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( "ERS.DC event: " + eventJS ); // System.out.println( eventJS.getMember( "target" )); ///// eventJS.getMember("target") throws NullPointerException. RelayS.this.handleEvent( RhiEvent.wrapEvent( window, eventJS, /*isAsync*/false )); } } }