Creating a GUI for font manipulation

To use the functionality described in the previous chapters, application SimplyHTML needs additional GUI components the first of which is class FontPanel. Class FontPanel allows to display and change most relevant font and font style attributes at once. By wrapping all related components into a panel, it is easier to use it in different places such as dialogs later.

Setting and getting attributes

FontPanel uses methods getAttributes and setAttributes to exchange font settings with other objects through an AttributeSet.

In the constructor of FontPanel all components implementing the FontComponent interface (see below) are added to Vector fontComponents . This makes it easy for methods getAttributes and setAttributes to distribute or collect the attributes to and from the various font manipulation components through an AttributeSet.

Methods getAttributes and setAttributes simply go through all objects in Vector fontComponents and call methods getValue and setValue respectively passing the AttributeSet containing the actual font settings.

Components of class FontPanel

FontPanel uses GUI components defined in different classes to set the various font attributes:

FamilyPickList, SizePickList and StylePickList are inner classes of class FontPanel and variations of a separate class TitledPickList which defines the general behaviour of a pick list typical for font dialogs having a list, a text field and a title label. All mentioned classes are described below shortly. Please consult the sources and API documents for further details.

TitledPickList

Class TitledPickList defines a pick list typically being used in font dialogs, consisting of a list title, a text field for the currently selected value and the actual pick list containing all possible values. It implements listeners for the various events produced by user settings inside its controls to synchronize selections in the text field and the pick list at all times. Then it has some getter/setter methods to programmatically get and set a selection.

It also defines a an EventListener and Event so that external components can be notified of changes in the TitledPickList . This mainly is meant to allow FontPanel to update the sample text display whenever a selection changes.

FamilyPickList, SizePickList and StylePickList

Classes FamilyPickList , SizePickList and StylePickList all are subclasses of TitledPickList. They extend TitledPickList by implementing interface FontComponent.

Interface FontComponent

Interface FontComponent is used to standardize the way attributes are set and retrieved. It defines two generic methods getValue and setValue. setValue is meant for setting a component from an AttributeSet, getValue should return the setting of a font component in the form of an AttributeSet.

Implementing the FontComponent Interface

Each component implementing the FontComponent interface can do the implementation special to the attribute or set of attributes it is meant to manipulate. FamilyPickList for instance simply reads CSS.Attribute.FONT_FAMILY, StylePickList acts on a combination of CSS.Attribute.FONT_WEIGHT and CSS.Attribute.FONT_STYLE and SizePickList uses CSS.Attribute.FONT_SIZE and adds certain handling for the 'pt' identifier.

EffectPanel

Class EffectPanel is a JPanel with a ButtonGroup of JRadioButtons allowing to select, whether or not a text portion should be underlined or striked out. With CSS.Attribute.TEXT_DECORATION, attributes underline and line-through can not be combined which is why JRadioButtons are used allowing only one of the possible selections at a time.

EffectPanel implements interface FontComponent to set and return the component's value in form of an AttributeSet.

ColorPanel

Class ColorPanel adds a JLabel, a JTextField and a JButton to a JPanel and shows a JColorChooser dialog when the JButton is pressed. Colors selected from the JColorChooser are set as the background color of the JTextField. The JTextField is not editable, it is only used to show the currently selected color as its background color.

ColorPanel implements interface FontComponent to set and return the component's value in form of an AttributeSet. In addition it defines a an EventListener and Event so that external components can be notified of changes in the ColorPanel. This mainly is meant to allow FontPanel to update the sample text display when a color is changed.