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.
publicabstractclassHtmlPanelBaseextendsComposite
{
privateString_dynPostfix="";
protectedfinal String id(final String staticId) { return staticId + _dynPostfix; }
privatefinal String wrapId(final String id) { return"id=\"" + id + "\""; }
privatefinal String wrapDynId(final String refId) { return wrapId(id(refId)); }
privateString_htmlAsText=null;
public String getHtmlAsText() { return _htmlAsText; }
abstractprotected String htmlPanelBundleHtmlText();
abstractprotected List<String> idList();
protectedHTMLPanel_holder=null;
private HTMLPanel createHtmlPanel(finalboolean 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.returnnewHTMLPanel(_htmlAsText);
}
publicHtmlPanelBase(finalboolean defineGloballyUniqueIds)
{
setup(defineGloballyUniqueIds);
initWidget(_holder);
}
privatevoidsetup(finalboolean defineGloballyUniqueIds)
{
if (defineGloballyUniqueIds)
_dynPostfix = "_" + UUID.uuid().replace("-", "_");
_holder = createHtmlPanel(defineGloballyUniqueIds);
}
}
And now how you could sub-class from the above base:
publicclassHtmlPanelTemplateextendsHtmlPanelBase
{
private final staticboolean _defineGloballyUniqueIds = false;
private final static int _numIdCapacity = 40;
publicHtmlPanelTemplate()
{
super(_defineGloballyUniqueIds);
setup();
}
@OverrideprotectedStringhtmlPanelBundleHtmlText()
{
returnYourClientBundle.INSTANCE.getYourFileHtml().getText();
}
@OverrideprotectedList<String> idList()
{
final List<String> idList = newArrayList<String>(_numIdCapacity);
return idList;
}
privatevoidsetup()
{
}
}
Solution 2:
You don't need to subclass HTMLPanel. You can create a simple Composite widget:
publicclassmyPanelextendsComposite {
privateHTMLPanelpanel=newHTMLPanel();
publicmyPanel(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"