views:

236

answers:

1

Hi all, is there a way to tell GWT to compile different Java code per target browser?

GWT today creates a different script per target browser, all generated from the same source file. However, when working with non-standard features in different browsers (for example, file drag and drop into the browser), the support is quite different between different browsers, requiring to write different code.

Is there something like

// if IE 
.. some Java code to compile into the IE script
// else if chrome
.. some Java code to compile into the chrome script

etc.

+9  A: 

Yes, off course. The thing is called deferred binding. Check out http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsDeferred.html

Here's an excerpt

<module>

  <!--  ... other configuration omitted ... -->

  <!-- Fall through to this rule is the browser isn't IE or Mozilla -->
  <replace-with class="com.google.gwt.user.client.ui.impl.PopupImpl">
    <when-type-is class="com.google.gwt.user.client.ui.impl.PopupImpl"/>
  </replace-with>

  <!-- Mozilla needs a different implementation due to issue #410 -->
  <replace-with class="com.google.gwt.user.client.ui.impl.PopupImplMozilla">
    <when-type-is class="com.google.gwt.user.client.ui.impl.PopupImpl" />
    <any>
      <when-property-is name="user.agent" value="gecko"/>
      <when-property-is name="user.agent" value="gecko1_8" />
    </any>
  </replace-with>

  <!-- IE has a completely different popup implementation -->
  <replace-with class="com.google.gwt.user.client.ui.impl.PopupImplIE6">
    <when-type-is class="com.google.gwt.user.client.ui.impl.PopupImpl"/>
    <when-property-is name="user.agent" value="ie6" />
  </replace-with>
</module>

For the other browsers, I believe it would work without the fall through rule. I think the fall through rule is here just to speed up the things. Don't take this for granted, as I am not 100% sure.

This is from the official GWT documentation.

markovuksanovic