package textbender.o.rhinohide.events; // Copyright 2006, 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 org.w3c.dom.events.*; import textbender.g.lang.ThreadSafe; import textbender.o.rhinohide.*; import textbender.o.rhinohide.core.*; /** An event implemented as an overlay of a JavaScript event. */ abstract @ThreadSafe class RhiEvent extends Rhinohide implements Event { /** Constructs a RhiEvent. * * @param window global object * @param jsObject bridge to underlying JavaScript event, * per {@linkplain Rhinohide#jsObject() jsObject}() * @param isAsync per {@linkplain #isAsync() isAsync}() * * @return event, or null if jsObject is null * * @throws StunnedRhinoException if jsObject is non-null, * and has no event 'type' property */ public static RhiEvent wrapEvent( RhiWindow window, JSObject jsObject, boolean isAsync ) { if( jsObject == null ) return null; return wrapEvent( window, jsObject, isAsync, getType(new Rhinohide(window,jsObject)) ); } /** Constructs a RhiEvent of a specified event type. * * @param window global object * @param jsObject bridge to underlying JavaScript event, * per {@linkplain Rhinohide#jsObject() jsObject}() * @param isAsync per {@linkplain #isAsync() isAsync}() * @param type jsObject's event type */ public static RhiEvent wrapEvent( RhiWindow window, JSObject jsObject, boolean isAsync, String type ) { final RhiEvent event; if( type.equals("keypress") || type.equals("keydown") || type.equals("keyup") ) { event = new RhiKeyEvent( window, jsObject, isAsync ); } else if( type.equals("click") || type.equals("mousedown") || type.equals("mouseup") || type.equals("mouseout") || type.equals("mouseover") ) { event = new RhiMouseEvent( window, jsObject, isAsync ); } else throw new IllegalStateException( "unrecognized event type: " + type ); return event; } RhiEvent( RhiWindow window, JSObject jsObject, boolean isAsync ) { super( window, jsObject ); this.isAsync = isAsync; } // ------------------------------------------------------------------------------------ /** Returns true iff this event is to be asynchronously dispatched. * But there is no longer an async dispatcher. * This method may soon be removed. */ public final boolean isAsync() { return isAsync; } private final boolean isAsync; // - E v e n t ------------------------------------------------------------------------ /** Not yet coded. * * @throws UnsupportedOperationException */ public final boolean getBubbles() { throw new UnsupportedOperationException(); } /** Not yet coded. * * @throws UnsupportedOperationException */ public final boolean getCancelable() { throw new UnsupportedOperationException(); } /** Not yet coded. * * @throws UnsupportedOperationException */ public final EventTarget getCurrentTarget() { throw new UnsupportedOperationException(); } /** Not yet coded. * * @throws UnsupportedOperationException */ public final short getEventPhase() { throw new UnsupportedOperationException(); } public final EventTarget getTarget() { JSObject jsObjectTarget = (JSObject)getMemberV( "target" ); Double nodeTypeD = (Double)jsObjectTarget.getMember( "nodeType" ); if( nodeTypeD == null ) return RhiEventTarget.wrapEventTarget( window, jsObjectTarget ); // not a DOM node, don't know what it is // else ... More checks possible here, but what's really needed is a way to "cast-wrap" among different subtypes of rhinohide, based on underlying JS object type. Then the client can do it. else return RhiNode.wrapNode( window, jsObjectTarget, nodeTypeD.shortValue() ); } /** Not yet coded. * * @throws UnsupportedOperationException */ public final long getTimeStamp() { throw new UnsupportedOperationException(); } public final String getType() { return getType( RhiEvent.this ); } /** Returns the type of an event. * * @param rhinohide of JavaScript event * * @throws StunnedRhinoException if jsObject is non-null, * and has no event 'type' property */ public static String getType( Rhinohide rhinohide ) { Object sType = rhinohide.getMemberV( "type" ); return (String)sType; } /** Not yet coded. * * @throws UnsupportedOperationException */ public final void initEvent( String eventTypeArg, boolean canBubbleArg, boolean cancelableArg ) { throw new UnsupportedOperationException(); } /** @throws UnsupportedOperationException if this event * is {@linkplain #isAsync() asynchronously dispatched} */ public final void preventDefault() { if( isAsync ) throw new UnsupportedOperationException( "preventDefault() inapplicable to async events, per AsyncEventListener" ); else call( "preventDefault" ); } /** @throws UnsupportedOperationException if this event * is {@linkplain #isAsync() asynchronously dispatched} */ public final void stopPropagation() // never tested { if( isAsync ) throw new UnsupportedOperationException( "stopPropagation() inapplicable to async events, per AsyncEventListener" ); else call( "stopPropagation" ); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// // // // /** @throws IllegalArgumentException if jsObject is not a DOM event of type // */ // static void ensureType( Rhinohide rhinohide, String type ) // { // String jsObjectType = getType( rhinohide ); // if( jsObjectType != type ) throw new IllegalArgumentException( "wrong event type: " + jsObjectType ); // } // // // }