Search code examples
gwtuibindergwtp

How to use interface MyCssResource in Utility class (GWT/ GWTP)?


I have

public interface MyResource extends ClientBundle{
    @NotStrict
    @Source("/myResource.css")
    MyCssResource css();
}
public interface MyCssResource extends CssResource {
      String gridEvenRow();
      String gridOddRow();
      .... more styling here....
}

in TestView.java

@UiField MyResource res;
@Inject
    public TestView(final Binder binder) {
        widget = binder.createAndBindUi(this);
        res.css().ensureInjected();
    }

In TestPresenter.java, I can style Grid without any problem.

for (int i = 1; i < myGrid.getRowCount(); i++) {
    if((i%2) == 0){
          myGrid.getRowFormatter().addStyleName(i, getView().getRes().css().gridEvenRow());
    }
    else{
          myGrid.getRowFormatter().addStyleName(i, getView().getRes().css().gridOddRow());
    }
}

But I don't want to repeat this code every time I initialize a Grid. So I want to put this code into a Utility class so that I can use it by just 1 line of code. Utility.formatGridOddEvenRow(myGrid);

Here is code in Utility

public class Utility {

    public static MyResource res;
    public Utility(){
         res.css().ensureInjected();
    }

    public static void formatGridOddEvenRow(Grid grid){
        for (int i = 1; i < grid.getRowCount(); i++) {
            if((i%2) == 0){
                grid.getRowFormatter().addStyleName(i, res.css().gridEvenRow());
            }
            else{
                grid.getRowFormatter().addStyleName(i, res.css().gridOddRow());
            }
        }
    }
}

However, it got run-time error [ERROR] - Uncaught exception escaped ? or some kind of error i don't know.

so, How to use interface MyCssResource in Utility class (GWT/ GWTP)?


Solution

  • public static final MyResource res=GWT.create(MyResource.class)
    static{res.css().ensureInjected()}
    

    add this code anywhere and use it. if you give all of your code, may be more helpfull.