Creating new documents
To create a new document basically two steps are necessary
With method DocumentPane(URL url) class DocumentPane has a constructor especially for that purpose. When a DocumentPane is created by calling this constructor with null as the url parameter, the DocumentPane is told to create a new HTMLDocument after creating the basic DocumentPane. The constructor calls this() to create a basic DocumentPane and then it calls method createNewDocument for creating the new document.
Method createNewDocument
As with creating a new editor pane, it is not enough to simply create a new HTMLDocument object and send it to the editor pane. We need additional steps inorder to adjust the HTMLDocument to the needs of application SimplyHTML which is why it makes sense to put these steps into an own method.
createNewDocument first gets the SHTMLEditorKit which was attached to the editor pane upon construction of the DocumentPane. A new HTMLDocument is created by calling method createDefaultDocument of the editor kit. createDefaultDocument ensures that we get a new HTMLDocument properly initialized and with our default style sheet attached to it.
The HTMLDocument then gets inserted a link reference to the style sheet file. Because we create a new HTMLDocument , this link refers to the name of SimplyHTML's default style sheet. The link reference is necessary because once we save the HTMLDocument, it can be used with other applications too. For an application other than SimplyHTML, the only way to determine which style sheet to use when the HTMLDocument is loaded is this particular style sheet reference (see 'Style sheets and HTML documents'). To insert the style sheet reference to the document, method insertStyleRef is used (see below).
The DocumentPane is registered as DocumentListener with the new document, causing the document to notify its DocumentPane about all changes. Finally the new HTMLDocument is assigned to the editor pane.
Method insertStyleRef
Method insertStyleRef inserts a reference link to the style sheet file to be associated with the HTML document having this reference link. The reference link has a syntax such as <link rel=stylesheet type="text/css" href="style.css"> and is to be placed in the <head> tag of a HTML document (see ' Style sheets and HTML documents').
To insert the reference, method insertStyleRef 'walks' through the element structure of a HTMLDocument and looks for its <head> and <body> tags. If a <head> tag is found, it is assumed that it already has the appropriate reference and the method does nothing.
Otherwise, it uses method insertBeforeStart of class HTMLDocument to insert a new <head> tag before the start of the <body> tag. The <head> tag is inserted along with the reference link to the style sheet by passing a respective HTML string to method insertBeforeStart.