23.7 Editing HTML


Generally, editor kits serve as a collection point for everything you would need to read, write, edit, and display some type of content. The HTMLEditorKit in particular serves as just such a collection point for HTML content. In addition to the HTMLEditorKit class proper, several supporting classes aid in the process of reading HTML, displaying it in the framework of JEditorPane, and writing it to a stream. Specifically, we look at the javax.swing.text.html.parser package and the HTMLWriter class. The APIs for this section remain a bit opaque, but as we mentioned earlier, each new release of the SDK comes with increased support, functionality, and openness.

If you're not interested in the hows and wheres of reading and writing HTML, but still want to be able to edit HTML documents, go ahead and dive into this example. We extend the SimpleEditor from earlier in this chapter to support two things:

  • More HTML actions, including horizontal rules, images, and hyperlinks

  • A Save menu item to write the document as HTML

To get started, Figure 23-8 is a screenshot of our editor in action. The document you see was created from scratch using the editor.

Figure 23-8. The SimpleEditor extended to support HTML
figs/swng2.2308.gif

Here's the HTML generated when you save the document:

<html>   <head>   </head>   <body>     <p>       This is some simple text.     </p>     <hr>     And <a href="http://www.ora.com">this is a link</a>.     <p>       Here's a picture:        <img src="/books/2/715/1/html/2/http://www.ora.com/animals/javasoap_red_firefish_flip.gif">     </p>   </body> </html>

Not bad, eh? Not perfect, but certainly passable for prototypes and internal applications.

Let's look at the classes involved in this application, which is primarily the same as the StyledEditor. The primary change comes from the new actions we support. Rather than repeat the entire code for the editor (HTMLEditor.java in the source archive), we can concentrate on three specific actions: ImageAction, SaveAction, and TagAction.

23.7.1 Hyperlink Actions

The TagAction class supports the most interesting of the editor actions. You can create actions for any HTML tag that uses one (primary) attribute. In our simple editor, the only such tag of interest is the <a> tag, but this is still a popular tag. We create two separate instances of TagAction for the <a> tag: one for the href attribute version, and one for the name attribute. Here's the code for creating the actions:

a = new TagAction(HTML.Tag.A, "URL", HTML.Attribute.HREF); a.putValue(Action.SMALL_ICON, new ImageIcon("icons/link.gif")); a.putValue(Action.NAME, "Anchor Link"); a = new TagAction(HTML.Tag.A, "Name", HTML.Attribute.NAME); a.putValue(Action.SMALL_ICON, new ImageIcon("icons/anchor.gif")); a.putValue(Action.NAME, "Anchor Name");

These actions can then be stuffed back into the action hashtable we use for all the predefined text actions found in the StyledEditorKit.

The code for the TagAction class itself is fairly straightforward because we rely primarily on StyledEditorKit.StyledTextAction as a starting point:

public class TagAction extends StyledEditorKit.StyledTextAction {   private HTML.Tag tag;   private HTML.Attribute tagAttr;   private String tagName;   public TagAction(HTML.Tag t, String s, HTML.Attribute a) {     super(s);     tag = t;     tagName = s;     tagAttr = a;   }   public void actionPerformed(ActionEvent e) {     JEditorPane editor = getEditor(e);     if (editor != null) {       String value = JOptionPane.showInputDialog(HTMLEditor.this,          "Enter " + tagName +":");       StyledEditorKit kit = getStyledEditorKit(editor);       MutableAttributeSet attr = kit.getInputAttributes( );       boolean anchor = attr.isDefined(tag);       if (anchor) {         attr.removeAttribute(tag);       }       else {         SimpleAttributeSet as = new SimpleAttributeSet( );         as.addAttribute(tagAttr, value);         attr.addAttribute(tag, as);       }       setCharacterAttributes(editor, attr, false);     }   } }

This action starts by retrieving the editor (an instance of JEditorPane) associated with the event. The editor "associated with the event" is the editor pane with focus when the action occurred. There is no specific code required to attach an Action in a menu or toolbar to an editor. Once we have a valid editor, we use it to alter the selected text.

Anchor tags require at least one attribute, so we prompt the user for the proper information. With the attribute information in hand, we use the editor's StyledEditorKit to make a proper AttributeSet for our tag. We use that AttributeSet in turn to apply our tag to the text in the editor.

23.7.2 Inserting Images

The ImageAction class is similar to the TagAction class. We need to prompt the user for the image's source URL, but we don't link the image to any selected text. We simply insert it at the current cursor location. Here's the code for the ImageAction class:

public class ImageAction extends StyledEditorKit.StyledTextAction {   public ImageAction( ) {     super("InsertIMG");   }   public void actionPerformed(ActionEvent ae) {     JEditorPane editor = getEditor(ae);     HTMLEditorKit kit = (HTMLEditorKit)editor.getEditorKit( );     HTMLDocument doc = (HTMLDocument)editor.getDocument( );     String value = JOptionPane.showInputDialog(HTMLEditor.this, "Image file:");     try {       kit.insertHTML(doc, editor.getCaretPosition( ),         "<img src=\"" + value + "\">", 0, 0, HTML.Tag.IMG);     }     catch (Exception e) {       JOptionPane.showMessageDialog(HTMLEditor.this,       "Image Not Loaded", "ERROR", JOptionPane.ERROR_MESSAGE);       e.printStackTrace( );     }   } }

Unlike the TagAction class, ImageAction requires the editor kit to be an HTMLEditorKit because HTMLEditorKit has a convenience routine to insert a chunk of HTML as plain text. No building of tags with attribute sets here. We just insert an <img> tag that refers to the user's chosen file, and we're done.

23.7.3 Saving as HTML

OK, so we can insert HTML into our editor. What we're really here for is reading and writing HTML. The power of the editor kits really starts to shine now. We don't have to write new SaveAction or OpenAction classes the ones written for our SimpleEditor work just fine. SimpleEditor relies on the text component's write( ) method, which in turn queries the editor kit. The HTMLEditorKit supplies an instance of the HTMLWriter class to get the job done. This means that no extra code is required on our part, which is just the way we like it.



Java Swing
Graphic Java 2: Mastering the Jfc, By Geary, 3Rd Edition, Volume 2: Swing
ISBN: 0130796670
EAN: 2147483647
Year: 2001
Pages: 289
Authors: David Geary

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net