How cut and paste work in SimplyHTML

To allow cut and paste for content including styles and all HTML specific parts SimplyHTML extends the cut and paste mechanism of Java. It defines two classes to enable the transport of HTML data and extends JEditorPane to use SimplyHTML's classes instead of the standard ones.

Class HTMLText

Class HTMLText represents a portion of HTML content. It has field htmlText for the HTML code representing the content and field plainText to represent the content as plain text. With methods copyHTML and pasteHTML it enables transport of HTML data into and out of HTMLText objects.

Transport mechanism

To transport HTML text methods copyHTML and pasteHTML use HTMLEditorKit's read and write methods, which allow to read or write a portion of a Document's content as HTML code using a Writer. By taking a StringWriter, data can be transferred into a String for temporary storage inside a HTMLText object.

Different mechanism within one paragraph

When copying and pasting text within one paragraph, i.e. without paragraph breaks, method HTMLEditorKit.read makes an own paragraph of the pasted text. HTMLText avoids this behaviour by implementing an alternate copy and paste mechanism.

When not copying multiple paragraphs the selection is split into text chunks. For each chunk of text it's attributes. Each chunk of text is inserted together with its attributes when it is pasted to another place in a document.

Class HTMLTextSelection

To transfer data in cut and paste operation a Transferable object is needed. A Transferable object wraps a data object into a common format describing the contained data to transfer operations. Class HTMLTextSelection is a Transferable for HTMLText objects. Whenever HTMLText is to be transported it is wrapped into an HTMLTextSelection object and passed to any transfer operation such as copy or paste.

Extending JEditorPane

JEditorPane is a subclass of JTextComponent. JTextComponent has methods cut, copy and paste to implement cut and paste operations which are inherited by JEditorPane. To actually use HTMLText and HTMLTextSelection in an editor, JEditorPane has to be extended by an own class named SHTMLEditorPane in SimplyHTML. SHTMLEditorPane overrides methods cut, copy and paste and uses HTMLText and HTMLTextSelection accordingly.