1. A sample attribute editor

In this tutorial, we'll only explain how to write the second kind of extension: an editor displaying a dialog box letting the user set or change the value of the XHTML bgcolor attribute. This attribute is defined by the XHTML 1.0 Transitional DTD for the following elements: body, table, tr, th, td. The modal dialog box displayed by this attribute editor is a standard javax.swing.JColorChooser.

A custom attribute editor implements interface SetAttribute.EditorFactory which is a factory class creating modal dialog boxes.

Excerpts from HexColorChooser.java:

public class HexColorChooser implements SetAttribute.EditorFactory {
    public SetAttribute.Editor createEditor(Component parentComponent, 
                                            Element element, Name attributeName,
                                            DataType attributeType) {
        return new Chooser(parentComponent,
                           dialogTitle(element, attributeName));
    }

The above method is passed information identifying the attribute to be edited: element, attributeName, attributeType.

The modal dialog box created and returned by the above methods must implement interface SetAttribute.Editor.

private static class Chooser implements SetAttribute.Editor {
    public final Component parentComponent;
    public final String title;

    public Chooser(Component parentComponent, String title) {
        this.parentComponent = parentComponent;
        this.title = title;
    }

    public String editAttributeValue(String attributeValue) {1
        Color color = fromHexString(attributeValue.trim());
        if (color == null) {
            color = Color.black;
        }

        color = JColorChooser.showDialog(parentComponent, title, color);
        if (color == null) {
            return null;
        }

        return toHexString(color);
    }
}

1

The SetAttribute.Editor specifies a single method: editAttributeValue. This method is passed the current value of the attribute. It must display the modal dialog box. If the user clicks Cancel, this method returns null. If the user clicks OK, this method returns a new value of the attribute being edited.