Learning GWT UiBinder – Part 2 (Styles and Instantiation)

2 03 2010

Part 1 of this short tutorial explained how to layout a component UI with help of UiBinder.
This part will show how to style already layouted structure.
To begin with we create a CSS file with needed styles:

@CHARSET "UTF-8";
.name{
    font-weight: bold;
}

.label{
    color: #0000FF;
    font-style: italic;
}

.bio{
    color: #666666;
}
.photo{
    margin-right: 5px;
}

Then we register this CSS in the UiBinder xml template:

Then we assign to widgets required styles with help of addStyleNames attributes.
That’s it about simple styling.

I also decided to have “bio” part of the user profile to be rendered inside a disclosure panel so the users can expand/collapse it as needed.
The first though is to do it like this:

<g:DiclosurePanel>
<g:Label ui:field="bio" addStyleNames="{style.bio}"/>
</g:DisclosurePanel>

But this way will not allow me to set the header of the disclosure panel. DisclosurePanel has method setHeader(Widget) but I don’t know how to nicely construct and reference a widget inside the xml attribute.
Also the mostly useful constructor of the DisclosurePanel class it the public DisclosurePanel(ImageResource openImage, ImageResource closedImage,
String headerText)
which allows setting of open/close icons as well as the string header. I don’t know why GWT guys have constructor with String parameter but setter only with Widget parameter, but this is the different story. Our task is to be able to instantiate the DisclosurePanel with help of that mentioned constructor.
For being able to delegate instantiation of UI components from the GWT framework to the developer’s code GWT provides @UiFactory annotation.
I just add the following method to my UserProfile.java:

@UiFactory
    protected DisclosurePanel createPanel(String label)
    {
        Resources res = GWT.create(Resources.class);
        DisclosurePanel result = new DisclosurePanel(res.getCloseIcon(), res.getOpenIcon(), label);
        return result;
    }

Now each time I will write in my .ui.xml

<g:DisclosurePanel label="Details">
            <g:Label ui:field="bio" addStyleNames="{style.bio}"/>
 </g:DisclosurePanel>

for example it will call the createPanel(String label) method to get the instance of the DisclosurePanel with specified textual header and open/close icons.
In result we will have this:

There are some more annotations available with UiBinder but this little example actually shown the essence of it.

@CHARSET “UTF-8″;
.name{
font-weight: bold;
}.label{
color: #0000FF;
font-style: italic;
}

.bio{
color: #666666;
}

.photo{
margin-right: 5px;
}

Advertisement

Actions

Information

6 responses

23 03 2010
26 03 2010
Alexander Arendar

A lot of flex weak sides mentioned there are fixed in new releases.
Also the comparison is not good at all, not consistent. If one mentions that something is bad in Flex he MUST explain how is it in GWT and vise verse. Just text layout is bad in Flex and performance is good in GWT is like comparing oranges to cows :)

23 09 2010
sekhar

Here i have a One Query .
in “UserProfile.ui.xml” i have 30 textboxes. i Wanna Create a UiHandler(onvalueChnage) on all these textboxes.

i can do that in UserProfile.java like this.
@Uihandler({“txt1″},{“txt2″},{“txt3″},”{“txt4″}…………….{“txt30″});
by this I need to specify the Name of the textBoxes Explicitly.
i ther any better way to do this.
like get all the textboxes name’s into some array. and create handlers for this texBoxes. Because my “XML” will get Structured Dynamically by background process.Hope u got my Point.

14 04 2011
sachin taware

Hi!!This was very helpful.Can you pls post something related to database connection and use using GWT for a similar application.It would be very helpful for me as I am a new bee learning GWT.
Thanks

11 07 2011
Mein Umstieg auf UiBinder (GWT)

[...] http://aarendar.wordpress.com/2010/03/02/learning-gwt-uibinder-part-2-styles-and-annotations/ Veröffentlicht in GWTÄhnliche ArtikelGWT-Beispiel in EclipseThema der Woche: Google Web Toolkit (GWT)GWT 1.5 RC2GWT 1.5 fertigGWT 1.6 Milestone 1 [...]

11 10 2011
Osho

Can you throw some light into using css styles from a CSSResource via ClientBundle? Thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s




Follow

Get every new post delivered to your Inbox.