java - 如何更改 HTMLPanel 的 html

标签 java html gwt panel

我想声明 HTMLPanel 的子类。 在它的构造函数中,我想给它一些参数来构造包含的 html。

因为我必须调用 super 构造函数作为第一个语句,所以我必须稍后在构造函数中更改 html。

我该怎么做?

public class MyHTMLPanel extends HTMLPanel
{ 
  public MyHTMLPanel(String id, int anotherParameter)
  { super("");
     String html=""
    // ... some code th construct the html
    //??? this.setHtml(html);  
  }  
}

最佳答案

您可以在下面找到我使用过并且效果很好的示例。 我不记得为什么我不将 HTMLPanel 子类化,无论是否有充分的理由。 如果您在单个页面中包含多个相同类型的对象,您会注意到一种随机化 html id 的机制。

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);
  }
}

现在如何从上述基础进行子类化:

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()
  {
  }
}

关于java - 如何更改 HTMLPanel 的 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12993095/

相关文章:

java - 通过指定其属性来打印 XML 元素

c# - 服务器-客户端推/拉连接如何工作?

javascript - 单页应用程序查看位置

html - 具有圆形底座的 CSS 选项卡(平滑底座过渡)

javascript - 通过 JavaScript 将输入值传递给 SVG 属性

java - 有关GWT,Cookie和网页定向的问题

java - 如何更新 Couchbase 系统日期?

html - 带有列表项的 GWT 无序列表

gwt - 我可以设置 EntityProxy 的默认字段值吗?

java - Spring 框架: findBy throws Illegal Argument Exception