Following code is used to set attribute style to body element and register cut/paste code to it, but failed. Strangely, when debugging, variable body
is not null and attribute "style" exists, but frame hasn't this child. Running work doesn't work as expected,and NPE got thrown out when call setBodyContent()
.
public class TextAreaExt extends RichTextArea
{
private BodyElement body;
@Override
protected void onLoad()
{
super.onLoad();
body = ((FrameElement) getElement().cast())
.getContentDocument().getBody();
body.setAttribute("style", "color:gray;");
registerOnCut(body);
}
public void setBodyContent(String text)
{
body.setInnerText(text);
}
private native void registerOnCut(BodyElement element) /*-{
var that = this;
console.log("registerOnCut");
element.oncut = $entry(function(event) {
//invoke method to adjust height based on content
return false;
});
element.onpaste = $entry(function(event) {
//invoke method to adjust height based on content
return false;
});
}-*/;
}
Don't invoke the onLoad
method to initialize the body, initialize it by making use of the addInitializeHandler
.
this.addInitializeHandler(new InitializeHandler() {
@Override
public void onInitialize(InitializeEvent event) {
body = ((FrameElement)getElement().cast()).getContentDocument().getBody();
registerOnCut(body);
}
});