Skip to content Skip to sidebar Skip to footer

How To Change The Html Of A HTMLPanel

I want do declare a Subclass of an HTMLPanel. In its constructor I want to give it a few paramters to construct the containing html. Because I have to call the super-constructor as

Solution 1:

You can find below an example I used and worked well for me. I don't remember why I don't sub-class HTMLPanel, whether a good reason or not. You will notice a mechanism to randomize the html ids in case you include several objects of the same type in a single page.

public abstract class HtmlPanelBase extends Composite
{
  private String _dynPostfix = "";
  protected final String id(final String staticId) { return staticId + _dynPostfix; }
  private final String wrapId(final String id) { return "id=\"" + id + "\""; }
  private final String wrapDynId(final String refId) { return wrapId(id(refId)); }

  private String _htmlAsText = null;
  public String getHtmlAsText() { return _htmlAsText; }

  abstract protected String htmlPanelBundleHtmlText();
  abstract protected List<String> idList();

  protected HTMLPanel _holder = null;
  private HTMLPanel createHtmlPanel(final boolean defineGloballyUniqueIds)
  {
    // Referent HTML panel text containing the reference id's.
    _htmlAsText = htmlPanelBundleHtmlText();
    if (defineGloballyUniqueIds)
    {
      // List of id's in the HTML Panel reference page to replace with dynamic/unique id's.
      final List<String> refIdList = idList();
      // Replace the reference id's with dynamic/unique id's.
      for (String refId : refIdList)
        _htmlAsText = _htmlAsText.replace(wrapId(refId), wrapDynId(refId));
    }
    // Return the HTMLPanel containing the globally unique id's.
    return new HTMLPanel(_htmlAsText);
  }
  public HtmlPanelBase(final boolean defineGloballyUniqueIds)
  {
    setup(defineGloballyUniqueIds);
    initWidget(_holder);
  }

  private void setup(final boolean defineGloballyUniqueIds)
  {
    if (defineGloballyUniqueIds)
      _dynPostfix = "_" + UUID.uuid().replace("-", "_");
    _holder = createHtmlPanel(defineGloballyUniqueIds);
  }
}

And now how you could sub-class from the above base:

public class HtmlPanelTemplate extends HtmlPanelBase
{
  private final static boolean _defineGloballyUniqueIds = false;
  private final static int _numIdCapacity = 40;

  public HtmlPanelTemplate()
  {
    super(_defineGloballyUniqueIds);
    setup();
  }

  @Override
  protected String htmlPanelBundleHtmlText()
  {
    return YourClientBundle.INSTANCE.getYourFileHtml().getText();
  }

  @Override
  protected List<String> idList()
  {
    final List<String> idList = new ArrayList<String>(_numIdCapacity);
    return idList;
  }

  private void setup()
  {
  }
}

Solution 2:

You don't need to subclass HTMLPanel. You can create a simple Composite widget:

public class myPanel extends Composite {

    private HTMLPanel panel = new HTMLPanel();

    public myPanel(String id, int anotherParameter) {
        // set HTML to panel based on your parameters
        initWidget(panel);
    }
}

Solution 3:

htmlPanel.getElement().setInnerHTML(...)

Don't know whether this works in derived class' constructor. But setting up a class for specific content text isn't really a good solution.


Post a Comment for "How To Change The Html Of A HTMLPanel"