Search code examples
gwtuibinder

Change span element style for GWT Cell using UiRenderer


How do I change the style for a Span HTML element when using UiRenderer with GWT 2.5? I have setup a simple cell to be used in a CellTable. The ui.xml looks like this :

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder">
<ui:with field='stkval' type='java.lang.String'/>
<ui:with field='stkchg' type='java.lang.String'/>
<ui:with field='res' type='com.mycompanyclient.client.Enres'/>

<div id="parent">
    <span><ui:text from='{stkval}'/></span>.
    [<span class="{res.newstyle.positive}" ui:field="signSpan">
        <ui:text from='{stkchg}'/>
    </span>]
</div>
</ui:UiBinder>

Now when this cell is instantiated by the CellTable, I expect to change the class name of the signSpan to be changed based on the value passed into the render function. My java code looks something like this:

public class IndCell extends AbstractCell<QuoteProxy>  {

@UiField
SpanElement signSpan;
@UiField(provided=true)
Enres res = Enres.INSTANCE;

interface MyUiRenderer extends UiRenderer {
    SpanElement getSignSpan(Element parent);
    void render(SafeHtmlBuilder sb, String stkval,String stkchg);
}
private static MyUiRenderer renderer = GWT.create(MyUiRenderer.class);

public IndCell() {
    res.newstyle().ensureInjected();
}


@Override
public void render(com.google.gwt.cell.client.Cell.Context context,
        QuoteProxy value, SafeHtmlBuilder sb) {
            if (value.getChangeSign().contentequals('d')) {
    renderer.getSignSpan(/* ?? */).removeClassName(res.newstyle().negative());
            renderer.getSignSpan(/* ?? */).addClassName(res.newstyle().positive());
            }
    renderer.render(sb, value.getAmount(),value.getChange());
}

If I try to use the UiField directly it is set to Null. That makes sense because I am not calling the createandbindui function like I would for UiBinder. The renderer.getSignSpan looks promising but I dont know what to pass for parent. All the example I could find use a event to identify the parent. But I dont want to click the cell generated.

Is there a way of changing style in the render method?


Solution

  • Because the class of the element is not a constant, you'll want to pass it as an argument to the render method so the cell's render reads:

    public void render(Cell.Context context, QuoteProxy value, SafeHtmlBuilder sb) {
      renderer.render(sb, value.getAmount(), value.getChange(),
          value.getChangeSign().contentequals('d') ? res.newstyle.positive() : res.newstyle.negative());
    }