0

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

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

我怎样才能做到这一点?

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

3 回答 3

3

您可以在下面找到我使用的示例,并且对我来说效果很好。我不记得为什么我不继承 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()
  {
  }
}
于 2012-10-20T22:42:05.303 回答
1

您不需要继承 HTMLPanel。您可以创建一个简单的 Composite 小部件:

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);
    }
}
于 2012-10-20T23:20:39.637 回答
1
htmlPanel.getElement().setInnerHTML(...)

不知道这是否适用于派生类的构造函数。但是为特定的内容文本设置一个类并不是一个好的解决方案。

于 2015-03-26T10:28:39.463 回答