6

In a gwt project I have a CellTree with custom cells. For easier testing I would like to add IDs for each of the cells. I know I can make it like this :

@Override
public void render(Context context,TreeElement value, SafeHtmlBuilder sb) {             
            if (value == null) {return;}
            sb.appendHtmlConstant("<div id=\""+value.getID()+"\">" + 
                                       value.getName()) + "</div>"; 
}

But I would like to use something similar to EnsureDebugID() so I don't have to burn the IDs in the code. Is there a way to do that?

Croo
  • 1,301
  • 2
  • 13
  • 32

4 Answers4

2

I would do something between the two of the aforementioned approaches. You should definitely add a prefix to be sure you can easily identify the cells during testing, and you should also take the createUniqueId() approach rather than generating your own UUIDs which can be more troublesome.

@Override
public void render(Context context, TreeElement value, SafeHtmlBuilder sb) {             
    if (value == null) {return;}
    String id = Document.get().createUniqueId();
    sb.appendHtmlConstant("<div id=\"cell_"+id+"\">" + 
                           value.getName()) + "</div>"; 
}
Chris Cashwell
  • 22,308
  • 13
  • 63
  • 94
1

You can use

Document.get().createUniqueId();

Here the description:

/**
   * Creates an identifier guaranteed to be unique within this document.
   * 
   * This is useful for allocating element id's.
   * 
   * @return a unique identifier
   */
  public final native String createUniqueId() /*-{
    // In order to force uid's to be document-unique across multiple modules,
    // we hang a counter from the document.
    if (!this.gwt_uid) {
      this.gwt_uid = 1;
    }

    return "gwt-uid-" + this.gwt_uid++;
  }-*/;
Stefan
  • 14,826
  • 17
  • 80
  • 143
0

I wanted to set an id to a TextCell and I did like this

import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;

public class EnsuredDbgIdTextCell extends TextCell {

    private static EnsuredDbgIdTextCellTemplate template = null;

    public EnsuredDbgIdTextCell() {
        super();
        if (template == null) {
            template = GWT.create(EnsuredDbgIdTextCellTemplate.class);
        }
    }

    public interface EnsuredDbgIdTextCellTemplate extends SafeHtmlTemplates {
        @Template("<div id=\"{0}\" style=\"outline:none;\" tabindex=\"0\">{0}</div>")
        SafeHtml withValueAsDebugId(String value);
    }

    @Override
    public void render(Context context, SafeHtml value, SafeHtmlBuilder sb) {
        if (value != null) {
            sb.append(template.withValueAsDebugId(value.asString()));
        }
    }

}

I set the id equals to the text value.

Christian Achilli
  • 5,547
  • 2
  • 25
  • 24
0

Generally when I do this sort of thing I add a prefix to it. so ID="sec_22" where sec_ is the prefix. Then I know that section has something unique.

Mech
  • 2,904
  • 3
  • 24
  • 29