Search code examples
javagwtuibinder

GWT: How to use Constants in UiBinder Template


I have a question related to UiBinder.

I have the following UiBinder file:

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
             xmlns:g="urn:import:com.google.gwt.user.client.ui">
    <g:HTMLPanel>
        <div>
            <g:VerticalPanel>
                <g:Label>Please enter your password:</g:Label>
                <g:FlowPanel>
                    <g:PasswordTextBox ui:field="textbox"/>
                    <g:Button ui:field="button" text="Login" styleName="?????"/>
                </g:FlowPanel>
            </g:VerticalPanel>
        </div>
    </g:HTMLPanel>
</ui:UiBinder>

If I put the style name in the ???, it works fine.

However, We have a constant file (not i18n Constants) which contains all the css names as constants. Like:

public class CSSConstants {
    public static final String CSS_TITLE = "title";

    public static final String CSS_TEXT_NORMAL = "text_normal";
    public static final String CSS_TEXT_ERROR = "text_error";
    public static final String CSS_TEXT_ERROR = "button blue";
    .......
}

I want to know how I can refer to this constant file in the UiBinder Template?

Many thanks


Solution

  • I hope this will work for you: 1) In your Java class, define a static getter for your style-name:

    public static String getSomeStyle(){}
    

    2) Access your style name as following:

     <ui:with type="package.of.your.class.ClassName" field="yourClass"></ui:with>
    
    <g:Button ui:field="button" text="Login" styleName="{yourClass.getSomeStyle}"/>
    

    This works fine when accessing targetHistoryToken values, and I hope it will work in your case.

    Of course you can do that easily (and elegantly) using interfaces that extend ClientBundle, but I don't think that's what you want (correct me if I misunderstood your need, so I can give more hints).

    (Sorry I don't have a dev environment to test it before posting my answer)