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..

    How can I get text to be vertically-aligned to the bottom and fill the empty space above it as needed?

    How to add focus on label css style

    How can I use CSS, Javascript, or a Cookie to disable animation reply on back button?

    How to mail HTML/CSS output as attachment?

    How to Set an if in css for opera browser? [duplicate]

    Why is last select option always shown, even when styled out

    How to unbind() a hover/ mouseover event?

    How to keep responsiveness while expanding div background height in CSS?

    How do I code CSS to “change” when I load/pull content using ajax?

    Button element styled with CSS is not showing the background-image in IE6

    In Wordpress how can I create a page which has 3 col-md-4 blocks in a container?

    How to show ellipsis for long cell values in kendo ui grid?

    How can I opt out of a CSS style on a case-by-case basis?

    how to position two divs next to each other, one of them is centered in the container of both divs

    How do I make a Javascript alert display only once, ever? [closed]

    How to achieve this complex layout with CSS?

    How to change hover background on links in css?

    how to set css and html in textarea [closed]

    html/css: how to on a single line center “title” and have on right buttons

    CSS preprocessors workflow - how does it really work?

    How to tooltip keep active using HTML and CSS or Jquery?

    How to position an element at the TOP LEFT corner of a page using CSS?

    How to format an arabic poem using jquery?

    How to force two elements become hovered at the same time?

    How to align two anchor tags with text horizontally next to each other?

    How do you set JavaFX TabPane CSS in code?

    How to force a hover state with jQuery?

    How to get angled corner like this in CSS, it's not round?

    HTML to PDF (by javascript) how can I add css or table?

    How to add a space between paragraphs when using css reset? [closed]