Showing posts with label CSS. Show all posts
Showing posts with label CSS. Show all posts

Wednesday, September 21, 2011

UIBinder, CssResource and CSS (GWT)

Few month ago I blogged about ClientBundle, UIBinder and CSS (GWT). I just realized that an important use case was missing. And here it is.

4) Using CssResource for CSS expressed in the UIBinder. When creating a UI with the UIBinder it might happen to include some CSS rules in the ui.xml file.

<ui:UiBinder
  xmlns:ui='urn:ui:com.google.gwt.uibinder'
  xmlns:g='urn:import:com.google.gwt.user.client.ui'>

    <ui:style>
       .outer {
          width: 100%;
       }
    </ui:style>
    <g:SimplePanel ui:field='sideBar'>
    </g:SimplePanel>
</ui:UiBinder>

It is possible to refer to those CSS rules from the GWT code. For doing so we have to declare a 'type' for ui:style:

<ui:UiBinder
  xmlns:ui='urn:ui:com.google.gwt.uibinder'
  xmlns:g='urn:import:com.google.gwt.user.client.ui'>

    <ui:style type='org.example.Example.ExStyle'>
       .outer {
          width: 100%;
       }
    </ui:style>
   
    <g:SimplePanel ui:field='sideBar'>
    </g:SimplePanel>
</ui:UiBinder>


Now, in the class  org.example.Example, we can reference the CSS rules and use them:

public class Example extends Composite {

    interface Binder extends UiBinder { }    
    private static final Binder binder = GWT.create(Binder.class);
   
    @UiField SimplePanel sideBar;
    @UiField ExStyle style;

    interface ExStyle extends CssResource {
        String outer();
    }

    public Example() {
        initWidget(binder.createAndBindUi(this)); 
        sideBar.setStyleName(style.outer()); 
    }
   ...

Sunday, February 27, 2011

ClientBundle, UIBinder and CSS (GWT)

In a previous post I was discussing all the possible ways for using image resources with GWT. Another thing you might want to do is dealing with CSS.

1) Standard use of CSS. While using GWT you can certainly use the CSS how you would use them for any other application, simply declaring CSS classes in a *.css file and importing it in the webpage of interest. With GWT widgets you will then simply set the style name as follows:
widget.setStyleName("cssClassName");
This approach works, however, if the CSS declaration is missing no exceptions are raised. Also, if you use multiple CSS files as I do, it is always annoying to find the declarations when you need to.

2) Using the UIBinder. If you are already using UIBinder, the easiest way to include CSS declarations is to add them to the binder. It is easy and it is safe as the Eclipse plugin is helping you out in finding missing declarations. I always use Eclipse but if you don't I am assuming you'll still find the problems at compile time.
<ui:UiBinder
  xmlns:ui='urn:ui:com.google.gwt.uibinder'
  xmlns:g='urn:import:com.google.gwt.user.client.ui'>

    <ui:style>
       .outer {
          width: 100%;
       }
    </ui:style>

    <g:VerticalPanel styleName='{style.outer}'>
    </g:VerticalPanel>
</ui:UiBinder>
The downside of this approach is redundancy. Sometimes I want to use CSS declarations multiple times and, with this approach, I have to repeat them for each single Binder.

3) Using CssResource. Another alternative consists in doing something similar to what you can do for icons with ImageResource. First I declare the set of declarations of the stylesheet in a file that I name Commons.css:
.smallIcon {
    height: 16px;
    width: 16px;
}

Then, linking the CSS file, I declare the use of the stylesheet as a resource for the application:
public class Example implements EntryPoint {

  public interface Resources extends ClientBundle
  {
     public static final Resources INSTANCE =  GWT.create(Resources.class);
 
     public interface Resources extends ClientBundle { 
        @Source("org/example/application/client/Commons.css")
        CommonsCss commonsCss();
     }
     
     ...
  }
  
}
Now, where the dots in the above snippet are, I can declare the stylesheet declarations I want to expose to the application:
public interface CommonsCss extends CssResource {
     String smallIcon();
}
As the CSS class is named as the method everything works fine. However, sometimes you might want to change the name of the method. Using Java annotations you address that issue as well:
public interface CommonsCss extends CssResource {
     @ClassName("smallIcon")
     String smallIconClass();
}
Now, we can write something like:
Resources resources = Resources.INSTANCE.factory().create();
Image img = new Image();
img.setStyleName(resource.commonsCss.smallIcon());
...
This approach allows you to collect in one single place CSS declarations you need to use in multiple packages in your application. Also you can leverage a good amount of validation in your Java code. You might argue the process can be a bit tedious but I can assure you, for a big GWT application, it can help you saving lots of time later on especially when refactoring the code.

There are other interesting things to know about the ClientBundles, but for now I'll stop here.

Tuesday, November 09, 2010

UIBinder, CSS and Images (GWT)

I am working with GWT (Google Web Toolkit) since a while now. One of the things I am regularly doing (since version 2.0) is creating UIs through Declarative Layouts with UiBinder. While creating the ui.xml files it is normal to have the need of displaying images or icons.

1) Integrating background images with the ui:style section: for a long time have been using simple colors to create my look and feel but lately I started to feel the need to give the UI a better look using images as background. One way of getting this done with GWT is to include in the ui.xml file something like this - where the image is in the same folder of the ui.xml file:
<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
xmlns:g='urn:import:com.google.gwt.user.client.ui'>

  <ui:style>
    @url fileUrl bckImage;
    .titleBar { background: fileUrl repeat 0 0; }
  </ui:style>

  <ui:data field='bckImage' src='image.gif'/>

  <g:HorizontalPanel styleName='{style.titleBar}'>
  ...
  </g:HorizontalPanel>

</ui:UiBinder>

An alternative way:
<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
xmlns:g='urn:import:com.google.gwt.user.client.ui'>

  <ui:image field='gradient' src='image.gif' />

  <ui:style>
    @sprite .header {
      gwt-image: 'gradient';
      width: 100%;
      height: 18px;
    }
  </ui:style>

  <g:VerticalPanel>
    <g:HorizontalPanel ui:field='header' styleName='{style.header}'/>
  </g:VerticalPanel>

</ui:UiBinder>

2) Getting the images from the ClientBundle: in GWT it is possible to declare resources with something like the following:
package com.commonsemantics.example;

public class Example implements EntryPoint {

  public interface Resources extends ClientBundle
  {
     public static final Resources INSTANCE =  GWT.create(Resources.class);

     @Source("images/myIcon.jpg")
     ImageResource myIcon();
  }

  ...
}

Once the ImageResource is declared it is possible to use it in the ui.xml file:
<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
xmlns:g='urn:import:com.google.gwt.user.client.ui'>

  <ui:style>
    // This does not have any effect as the images are translated by GWT 
    // into background. In this configuration, in order to have the image 
    // of the right size, it has to be manually resized.
    .myImage { width: 18px; height: 18px; }
  </ui:style>

  <ui:with field='resources'
    type='com.commonsemantics.example.Example.Resources' /> 

  <g:HorizontalPanel styleName='{style.titleBar}'>
    <g:Image ui:field='rightSide' resource='{resources.myIcon}'  
      styleName='{style.myImage}'></g:Image>
  </g:HorizontalPanel>

  </ui:UiBinder>

3) Getting the images from the same package. When making use of images it is also possible to copy them in the same package of the class using them and refer to them as follow:
<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
xmlns:g='urn:import:com.google.gwt.user.client.ui'>

  <ui:image field='myImage' src='myImage.gif' />

  <g:HorizontalPanel>
    <g:Image ui:field='rightSide' resource='{myImage}'></g:Image>
  </g:HorizontalPanel>

</ui:UiBinder>