Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Custom tag in JEditorPane

Hi All
I need to create custom tag like <media> that JEditorPane should understand. The requirement is that i have a button on click of which <media></media> tag should be generated in backgound and some UI should be displayed in JEditorPane for eg: A Button. I am totally confused with all this. I have writtern the following code on click of button

String text="<media name=media>test</media>";
htmlkit.insertHTML(htmldoc, jEditorPane.getCaretPosition(), "temp", 0,0,XeHTML.XeHTMLTag.MEDIA);

here htmlkit is custom EditorKit that i have created. The code is attached below

public class MyEditorKit extends HTMLEditorKit {

public ViewFactory getViewFactory() {
return new XeHTMLFactory();
}

public Document createDefaultDocument()
{
StyleSheet styles = getStyleSheet();
StyleSheet ss = new StyleSheet();
ss.addStyleSheet(styles);
XeDocument doc = new XeDocument(ss);
doc.setParser(getParser());
doc.setAsynchronousLoadPriority(4);
doc.setTokenThreshold(100);
return doc;
}

public static class XeHTMLFactory extends HTMLEditorKit.HTMLFactory implements ViewFactory{
/*public XeHTMLFactory(){
super();
}*/

public View create(Element ele){
ele.getAttributes().getAttribute(StyleConstants.NameAttribute);
if (attr instanceof XeHTML.XeHTMLTag | attr instanceof HTML.Tag){
//Start processing for the custom tag
if(ele.getName().equals("media")){
return new ComponentView(ele){
protected Component createComponent(){
String text ="### how to get the text between the button tags?? ###";
JButton button = new JButton("Button : " + text);
button.setBackground(Color.RED);
return button;
}
};
}else
{
System.out.println("Inside else");
return super.create(ele);
}

}else{
return null;
}
}
}//end inner static class
}

my custom made XeHTML file has this code

public class XeHTML extends HTML {
public static class XeHTMLTag extends Tag{
protected XeHTMLTag(String id){
super(id);
}
protected XeHTMLTag(String id,boolean causesbreak,boolean isblock){
super(id,causesbreak,isblock);
}


public static final Tag MEDIA=new UnknownTag("media");

}


}

Please help since i am totally confused as to how to proceed ahead. I think i am making some mistake in registering the tag. I have attached my entire source code below
[2900 byte] By [pankajtiwari] at [2007-11-11 10:11:27]