0

I'm looking to supply my own implementation of CanvasElement class. This class is instantiated in HTMLGraphicsCanvas class in the PlayN stack.

What is the ideal way of doing this? There are 2 ways that I've tried but failed:

A) Subclassing HTMLGraphics and HTMLPlatform? --this doesn't seem to be possible.

B) The GWT module mechanism seems to require that my custom implementation of a GWT-supplied class be of the same package. So take for example this XML snippet:

<replace-with class="com.google.gwt.dom.client.DOMImplOpera">
    <when-type-is class="com.google.gwt.dom.client.DOMImpl"/>
    <when-property-is name="user.agent" value="opera"/>
</replace-with>

If I do something like:

<replace-with class="com.domain.example.CustomCanvasElement">
    <when-type-is class="com.google.gwt.dom.client.CanvasElement"/>
    <when-property-is name="user.agent" value="opera"/>
</replace-with>

...it does not seem to work because the packages are different. At least that's what I think is happening.

Any other ideas? Much appreciated.

1 Answers1

0

Ok so I can answer the B) part of my own question. Although I would really appreciate an answer to my overall question about how to supply my own implementation.

So my guess wasn't entirely correct. According to this section of Deferred Binding, basically you have to first create a superclass object that Browser-specific objects will subclass. So for instance:

GenericBrowserObject is the superclass and might have common implementations of certain methods between Browser-specific objects or might be your "catch-all" implementation for all other (non-specific) Browsers.

So for instance, let's say you only want to have specific implementations for IE and Gecko-based browsers.

GeckoBrowserObject extends GenericBrowserObject

IEBrowserObject extends GenericBrowserObject

Let's say that there is a common method called setStyle in GenericBrowserObject but they have differing implementations between GeckoBrowserObject and IEBrowserObject, then you would override setStyle in each of those classes and supply their own implementations.

The class that manipulates these objects needs to instantiate GenericBrowserObject superclass in this way:

    GenericBrowserObject obj = GWT.create(GenericBrowserObject.class);
    obj.setStyle("whatever");

Then in your module file (something.gwt.xml), you would have something like this:

    <replace-with class="com.domain.blah.GenericBrowserObject">
            <when-type-is class="com.domain.blah.GenericBrowserObject"/>
    </replace-with>

    <replace-with class="com.domain.blah.IEBrowserObject">
            <when-type-is class="com.domain.blah.GenericBrowserObject"/>
            <any>
                    <when-property-is name="user.agent" value="ie6"/>
                    <when-property-is name="user.agent" value="ie8"/>
            </any>
    </replace-with>

    <replace-with class="com.domain.blah.GeckoBrowserObject">
            <when-type-is class="com.domain.blah.GenericBrowserObject"/>
                    <any>
                            <when-property-is name="user.agent" value="gecko"/>
                            <when-property-is name="user.agent" value="gecko1_8"/>
                    </any>
    </replace-with>

When GWT compiles this, it will generate the correct objects based on the user agent string via GWT.create.