Articles Projects Tips Downloads Contacts About

EditorKit and content dependent Reader/Writer.
By Stanislav Lapitsky

Table of content of the tutorial

Different types of EditorKits are created to work with specific types of content. E.g. RTFEditorKit is created to work with RTF and HTMLEditorKit supports HTML content. Type of content is defined by getContentType() method. Overriding the method we can define own type of content our EditorKit supports.

EditorKit provides 4 methods to read and write our specific content.

public void write(OutputStream out, Document doc, int pos, int)  throws IOException, BadLocationException
public void write(Writer out, Document doc, int pos, int len) throws IOException, BadLocationException

 
public void read(InputStream in, Document doc, int pos) throws IOException, BadLocationException
public void read(Reader in, Document doc, int pos) throws IOException, BadLocationException 

When user performs Copy action or uses getText() method of JEditorPane with the kit installed one of the write(0 methods is called to store selected fragment of the passed Document in the stream or writer accordingly. Replacing the methods with own writer calls will generate desired format of stored content. E.g.

    public void write(Writer out, Document doc, int pos, int len)
        throws IOException, BadLocationException { 
		
        MyWriter.write(doc, pos, len, out);
    }

Accordingly when paste action of setText() is called the formatted content will be passed in one of the read() methods and parsed/processed with desired parser. In fact we can create just one method and use something like this to convert reader to InputStream.

    public void read(InputStream in, Document doc, int pos)
	    throws IOException, BadLocationException {
 
        MyReader.read(doc, pos, in);
    }
    
    public void read(Reader in, Document doc, int pos) throws IOException, BadLocationException {
        BufferedReader br=new BufferedReader(in);
        String s=br.readLine();
        StringBuffer buff=new StringBuffer();
        while (s!=null) {

            buff.append(s);
            s=br.readLine();
        }
 
        MyReader.read(doc, pos, new ByteArrayInputStream(buff.toString().getBytes()));
    }

A note about Paste action. When paste is called clipboard is checked whether it contains appropriate content type. In fact it checks type of content and compares with result of getContentType() method. If clipboard has specified type of content the read() method of kit is called.

The example writer produce an xml representing paragraphs and texts with their attributes. Looks like this:

<doc>
  <par align="0" first="0.0" above="0.0" below="0.0" left="0.0" right="0.0" line_spacing="0.0">
    <text font_size="12" font_family="Monospaced" bold="false" italic="false" underline="false">
    This is 
    </text>
    <text font_size="12" font_family="Monospaced" bold="false" italic="false" underline="false" j_underline="true">
    jagged
    </text>
    <text font_size="12" font_family="Monospaced" bold="false" italic="false" underline="false">
     underline example.
    </text>
  </par>
  <par align="0" first="0.0" above="0.0" below="0.0" left="0.0" right="0.0" line_spacing="0.0">
    <text font_size="12" font_family="Monospaced" bold="false" italic="false" underline="false">
    Paragraph 2
    </text>
  </par>
</doc>

See the full code of the Writer and Reader on the example page.

Next article will explain how EditorKit uses actions and how the actions work inside.

Back to Table of Content