Form factor support using GIN

302 views
Skip to first unread message

Rob

unread,
Nov 5, 2011, 7:21:15 PM11/5/11
to Google Web Toolkit
Hi,

I would like to add form factor support (desktop, tablet, mobile) to
an existing application (GWT 2.3, GWTP 0.6, GIN, GUICE, ...)

I had a quick look at the MobileWebApplication sample and then tried
the following:

- added FormFactor.gwt.xml to the project
- inherited the form factor module in the application's .gwt.xml
- added a replace-with to the application's .gwt.xml:

->
..

<replace-with
class="com.gwtcx.sample.serendipity.client.gin.TabletViewFactory">
<when-type-is
class="com.gwtcx.sample.serendipity.client.gin.ViewFactory"/>
<when-property-is name="formfactor" value="tablet"/>
</replace-with>

..
->

- defined a simple ViewFactory class (that returns the desktop view):

->

public class ViewFactory {

public Class<? extends SignInPagePresenter.MyView>
getSignInPageView() {

return SignInPageView.class;
}
}

->

- defined a simple TabletViewFactory class (that returns the tablet
view):

->

public class TabletViewFactory {

public Class<? extends SignInPagePresenter.MyView>
getSignInPageView() {

return SignInPageTabletView.class;
}
}

- updated the ClientModule to use the a ViewFactory:

->

public class ClientModule extends AbstractPresenterModule {

private final ViewFactory viewFactory = new ViewFactory();

@Override
protected void configure() {

..

bindPresenter(SerendipitySignInPagePresenter.class,
SerendipitySignInPagePresenter.MyView.class,
viewFactory.getSignInPageView(),
SerendipitySignInPagePresenter.MyProxy.class);

..

}
}

The formfactor property is being set correctly, however, ViewFactory
is not "replaced with" TabletViewFactory?

Any suggestions much appreciated.

Cheers
Rob

-> Also posted to gwt-platform discussion group

Thomas Broyer

unread,
Nov 6, 2011, 9:58:07 AM11/6/11
to google-we...@googlegroups.com
Deferred binding only occurs when you use GWT.create(). If you "new ViewFactory()", there's no reason you'll be given a TableViewFactory. GWT doesn't change Java's semantics.

What you have to do is to use a distinct Ginjector depending on the form factor, and you can re-use the same GinModule(s) for all the shared binding configurations, and add a specific GinModule per form factor. Unfortunately, you cannot use both a generate-with (GIN's Ginjector) and replace-with (chose the right Ginjector depending on form factor), so you'll have to use a replace-with rule on a "provider class" for you Ginjector:

interface MyGinjector extends Ginjector {
   // all your accessors
}
@GinModules(SharedGinModule.class, DesktopGinModule.class)
interface DesktopGinjector extends Ginjector {
   // nothing special here; it's only used for the @GinModules annotation
}
@GinModules(SharedGinModule.class, TabletGinModule.class)
interface TabletGinjector extends Ginjector { }
@GinModules(SharedGinModule.class, PhoneGinModule.class)
interface PhoneGinjector extends Ginjector { }

interface GinjectorProvider {
   MyGinjector get();
}
class DesktopGinjectorProvider implements GinjectorProvider {
   public MyGinjector get() { return GWT.create(DesktopGinjector.class); }
}
class TabletGinjectorProvider implements GinjectorProvider {
   public MyGinjector get() { return GWT.create(TabletGinjector.class); }
}
class PhoneGinjectorProvider implements GinjectorProvider {
   public MyGinjector get() { return GWT.create(PhoneGinjector.class); }
}

<replace-with class="...DesktopGinjectorProvider">
   <when-type-is class="...GinjectorProvider" />
   <when-property-is name="formfactor" value="desktop" />
</replace-with>

Rob

unread,
Nov 7, 2011, 1:06:02 AM11/7/11
to Google Web Toolkit
Hi Thomas,

Thanks.

Was also referred to -> https://groups.google.com/d/topic/gwt-platform/V_Qqn9j6hj4/discussion

Cheers
Rob

Jens

unread,
Nov 14, 2011, 11:12:18 AM11/14/11
to google-we...@googlegroups.com
Hi,

I have pretty much the same code in my project for a long time but I had disabled everything but the desktop replace-with default rule. Today I have included the rules for tablets and mobiles again and deferred binding doesn't work correctly. GWT always uses the DesktopGinjectorProvider. Even if I disable all rules again and directly tell GWT to replace DesktopGinjectorProvider with TabletGinjectorProvider GWT uses the DesktopGinjectorProvider:

<replace-with class="<package>.TabletGinjectorProvider">
   <when-type-is class="<package>.DesktopGinjectorProvider" />
</replace-with>


So I turned on GWT debug messages and the rebinding log says:

- Rebinding <package>.DesktopGinjectorProvider
- - Checking rule <replace-with class='<package>.TabletGinjectorProvider'/>
- - - Checking if all subconditions are true (<all>)
- - - - <when-type-is class="<package>.DesktopGinjectorProvider"/>
- - - - - Not an exact match
- - - - No: One or more subconditions was false



So GWT sees my rule that should replace the DesktopGinjectorProvider with the TabletGinjectorProvider but doesnt find an "exact match" and thus uses the DesktopGinjectorProvider as its a concrete class.

So how can a fully qualified class name "not exactly match"? Any ideas? 


-- J.

Jens

unread,
Nov 14, 2011, 12:10:17 PM11/14/11
to google-we...@googlegroups.com
Sigh....the root of all evil are one letter typos...

It works now.

-- J.
Message has been deleted

Nava

unread,
Feb 14, 2014, 3:45:29 PM2/14/14
to google-we...@googlegroups.com
Hi,

I have followed your approach but I have got error that MyGinjector should be annotated with GinModule. In your example you have not annotated MyGinjector.
Can you please help providing sample code fragment which works. I am developing an web application for Desktop, Tablet, Mobile platforms, I am kind of stuck.

Kind regards
Nava

On Monday, 7 January 2013 05:15:43 UTC, Alexandre Senecal wrote:
Thank you for this example, it was quite helpful! 
One small correction, the form factor specific Ginjectors should extend the MyGinjector interface as follows:


 
@GinModules(SharedGinModule.class, DesktopGinModule.class)
interface DesktopGinjector extends MyGinjector {
   // nothing special here; it's only used for the @GinModules annotation
}
@GinModules(SharedGinModule.class, TabletGinModule.class)
interface TabletGinjector extends MyGinjector { }
@GinModules(SharedGinModule.class, PhoneGinModule.class)
interface PhoneGinjector extends MyGinjector { }

Jens

unread,
Feb 14, 2014, 3:51:38 PM2/14/14
to google-we...@googlegroups.com
Do your Desktop/Tablet/MobileGinjectors extend from MyGinjector?

-- J.

Kadaici Tamilan

unread,
Feb 15, 2014, 5:29:13 AM2/15/14
to google-we...@googlegroups.com
Yes It worked. Thanks.
Reply all
Reply to author
Forward
0 new messages