How to properly use CSS in GWTP?


Tags: css,gwt,stylesheet,uibinder,gwtp

Problem :

it's very difficult to find a complete step-by-step guidelines of How to properly use CSS in GWTP on internet.

I am using eclipse & GWTP to build my app & I want to set some simple Css styles for my widgets (button, textbox...).

Ok, here is what I got: a TestPresenter.java, TestView.java & TestView.ui.xml

-In TestView.ui.xml:

    <ui:UiBinder .....>
    <ui:style src='myStyle.css' />
    <g:HTMLPanel>
    <g:Button  text="My But 1" addStyleNames="{style.myBut}" ui:field="myBut1"/>
    <g:Button  text="My But 2" ui:field="myBut2"/>
    </g:HTMLPanel>
    </ui:UiBinder>

myStyle.css is in the same folder that houses TestPresenter.java, TestView.java & TestView.ui.xml

-TestPresenter.java (has 2 buttons- myBut 1 & myBut 2): I added "myBut" for myBut2

    getView().getMyBut2().addStyleName("myBut");

After running, it shows 2 buttons, the first myBut1 got the correct CSS but myBut2 still show the default Css. I changed to getView().getMyBut2().setStyleName("myBut"); but it still didn't work.

So i think probably i'm missing some classes here & that is why eClipse couldn't recognize "myBut" CSS so that it can apply for myBut2.

So, How to let myBut2 show the correct Css in eClipse?



Solution :

the reason is that adding a CSS style sheet as a source to the uibinder causes the gwt compiler to generate a CssResource class for it, and therefore obfuscating the CSS class name to SHA1 hash. That mean that in the final compiled version, instead of ".myBut" you actually end up with something like ".XYZXYZ".

this is purely GWT uibinder behavior that you can read about here

Specifically for GWTP, the textbook solution is:

  1. in TestView.java add:

    public TestView extends SomeGWTPViewClass implements TestPresenter.MyView
    {
        public interface MyStyle extends CssResource
        {
           String myBut();
        }
    
        @UiField MyStyle style;
    
    
        @Override
        MyStyle getStyle()
        {
            return style;
        } 
    
        //rest of code here......
        .....
        ...
    
    
    }
    
  2. in TestView.ui.xml change ui:style to:

    <ui:style src='myStyle.css' type="fully.qualified.package.name.TestView.MyStyle"/>

  3. in TestPresenter.MyView interface add:

    MyStyle getStyle();

  4. now you can access the myBut style in TestPresenter by:

    getView().getMyBut2().addStyleName(getView().getStyle().myBut());


    CSS Howto..

    express: how to send html together with css using sendFile?

    How to get these divs side-by-side while still keeping position:relative and not using float?

    How to make CSS URL background images show up in localhost?

    How to fix angular-strap css

    How to vertically align absolutely positioned images on top of one another?

    how to change a css class onclick?

    how to remove and add css class to a specific textbox inside gridview in c# asp.net?

    How to create custom horizontal radio buttons with CSS?

    How to style (increase the height) of select box in CSS?

    how to arrange divs with css as a grid?

    How to organise my web content?

    WordPress TwentyTen menu: how to CSS-select sub-menus in a specific position?

    How to suppress specific CSS 2.0 validation errors in Visual Studio 2008?

    how to do conditional css on table row?

    How to assign inner shadow to inputs in CSS

    How to style the entire first row of a table in css

    How do I justify a horizontal list?

    How to make fonts display on web cross-platform?

    How to create the timetabling with div properly?

    How to apply a css class to master page based on the url using jquery? [closed]

    HTML: How to change the width of input field based on container size

    How to make image override parent width and stretch to browser width?

    How do I apply CSS style to ASCII character

    How do you hide an HTML element based on a series of parent elements

    how to remove the last li::after for first line in specific width of an div

    How do I get an html/css form to post to Gravity Forms?

    How to get different style in nested element depending to depth level?

    How to put html input text into an image using CSS?

    How to style form elements unobtrusively with JavaScript and CSS?

    How do I create a pattern overlay in CSS