Wednesday, December 26, 2012

Re: Simple GWT Deferred Binding Example

hello Sir

I read yours eg and try to implement
but i got fail
can u give me this complete eg as a eclipse project





On Friday, March 28, 2008 10:11:50 PM UTC+5:30, neonleon wrote:
Hello,

I wanted to post a very simple example of GWT deferred binding as I
had trouble initially grasping it.

What this example will do is alert a message to the screen based on a
meta property set in the html page.


Here are the steps:

1.  I am going to create java classes that will alert a message to the
screen.  Each one of these classes will implement an Alerter
interface:

        package com.zedak.alerter.gwt.client.impl;
        ...

        public interface Alerter {
                    public void alert();
        }

2.  Here are my implementations of the above Alerter interface:

        package com.zedak.alerter.gwt.client.impl;
        ...

        public class HelloAlerterImpl implements Alerter{
                    public void alert() {
                                Window.alert("Hello!");
                    }
        }

        public class GoodbyeAlerterImpl implements Alerter{
                    public void alert() {
                                Window.alert("Goodbye!");
                    }
        }

        public class DefaultAlerterImpl implements Alerter{
                    public void alert() {
                                Window.alert("Default!");
                    }
        }

3.  I am going to create a property which will be utilized by GWT to
determine which implementation my Alerter will use.  I am going to
define my properties in a seperate XML file than my main module to
reduce clutter...

        Contents of Alerter-properties.gwt.xml:

        <module>
                    <define-property name="alerter" values="hello,goodbye,default" /
>

                    <property-provider name="alerter">
                               <![CDATA[
                                           var alerter = __gwt_getMetaProperty("alerter");
                                           if (alerter == null){
                                                alerter = "default";
                                          }
                                           return alerter;
                               ]]>
                    </property-provider>
        </module>

The HTML will contain a meta property that will define which alerter
to use.  If no meta property exists, it will use the default.

4.  I now will inherit the above module in my main module and define
which Alerter implementation to use for each possible property.

        Contents of Alerter.gwt.xml:

        <module>
                ....

                    <!-- Inherit the above module -->
                <inherits name="com.zedak.alerter.gwt.Alerter-properties" />

                    <replace-with
class="com.zedak.alerter.gwt.client.impl.HelloAlerterImpl">
                                <when-type-is
class="com.zedak.alerter.gwt.client.impl.AlerterImpl"/>
                                <when-property-is name="alerter" value="hello"/>
                    </replace-with>

                    <replace-with
class="com.zedak.alerter.gwt.client.impl.GoodbyeAlerterImpl">
                                <when-type-is
class="com.zedak.alerter.gwt.client.impl.AlerterImpl"/>
                                <when-property-is name="alerter" value="goodbye"/>
                    </replace-with>

                    <replace-with
class="com.zedak.alerter.gwt.client.impl.DefaultAlerterImpl">
                                <when-type-is
class="com.zedak.alerter.gwt.client.impl.AlerterImpl"/>
                                <when-property-is name="alerter" value="default"/>
                   </replace-with>

                <entry-point class='com.zedak.alerter.gwt.client.AlerterEntryPoint'/
>
                ...
        </module>

5.  Now lets create the EntryPoint class defined in the above XML,
which will call the Alerter implementation based on the property
value...

        package com.zedak.alerter.gwt.client;
        ...

        public class AlerterEntryPoint implements EntryPoint {
                public void onModuleLoad() {
                                Alerter alerter = (Alerter) GWT.create(Alerter.class);
                                alerter.alert();
                }
        }

All this module will do is instantiate the appropriate Alerter and
alert the message to the screen.  You must use the GWT.create method
to instatiate the Alerter class.

6.  Finally, lets add the appropriate code to our Alerter.html page...

        <html>
                <head>
                            <meta name='gwt:module' content='js/
gwt=com.zedak.alerter.gwt.Alerter'/>
                            <meta name='gwt:property' content='alerter=hello'/>
                </head>
                <body>
                        <script language="javascript" src="/js/gwt/
com.zedak.alerter.gwt.Alerter.nocache.js"/>
                </body>
        </html>



When you load the Alerter.html file, you should see an alert dialog
that states 'hello'.

If you switch the property to this...

        <meta name='gwt:property' content='alerter=goodbye'/>

You should see an alert dialog that states 'goodbye'.

Removing the property entirely should display an alert dialog that
states 'default'.



I thought a super-simple example might be helpful for some people out
there, and I hope that it is.


-Leon

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/XAD9I3TrZyIJ.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment