Actions and components to switch single font attributes

On top of being able to change most relevant font settings at once using class FontPanel, a couple of actions and components are needed to allow users to toggle or switch single font attributes quickly. In stage 3 of SimplyHTML this is done by adding some inner classes to FrmMain implementing respective parts:

- FontFamilyPicker - a JComboBox dedicated to font family changes
- FontSizePicker - a JComboBox dedicated to font size changes
- FontFamilyAction - an action to change the font family
- FontSizeAction - an action to change the font size
- ToggleFontAction - an action to toggle a single font setting on or off

FontFamilyPicker and FontSizePicker

The easiest way to act on a certain font setting probably would be an action bound to a JButton . For font properties family and size however, a possible setting is not just 'on' or 'off', for both attributes there a is a certain list of possible selections instead. For this type of setting a JComboBox is the GUI component of choice.

Extending JComboBox

To make such JComboBoxes easier to handle, two inner classes FontFamilyPicker and FontSizePicker extend class JComboBox with functions special to the purpose of changing respective font settings.

Customized content and common interface

FontFamilyPicker adds all font family names found on the particular system to its combo box using method getAvailableFontFamilyNames of class GraphicsEnvironment in its constructor. FontSizePicker adds a fixed list of point sizes instead. Both classes implement interface FontComponent for standardized access to their selected value.

FontFamilyAction and FontSizeAction

Both actions implement interface SHTMLAction so that common handling of setting action properties from our resource bundle and common updating can be used. In their actionPerformed method they apply the attribute represented by their associated picker component (family or size) to the editor.

ToggleFontAction

ToggleFontAction allows to switch a single font setting on or off in a generic way. It extends AbstractAction by defining some private fields reflecting the font attribute this instance of ToggleFontAction represents as well as the value for 'on' and 'off' for that particular font attribute. In the constructor, those fields are initialized from respective arguments passed to the constructor.

Shifting state

Method actionPerformed applies the font attribute resulting from the current state (on or off) and then toggles the action's state using method putValue. By passing either value FrmMain.ACTION_SELECTED or FrmMain.ACTION_UNSELECTED with key FrmMain.ACTION_SELECTED_KEY to method putValue, respective value is stored in the action's poperties table causing a PropertyChangeEvent being fired. Any listener to such events can then update its state accordingly.

Interfaces

ToggleFontAction implements interface FontComponent so that its value can be changed in a standard way from other objects through methods getValue and setValue . To always reflect proper state to components bound to FontAction, it implements interface SHTMLAction with method update.

Integration to FrmMain

Method initActions of class FrmMain initializes three instances of ToggleFontAction to the central commands Hashtable , one for CSS.Attribute.FONT_WEIGHT, one for CSS.Attribute.FONT_STYLE and one for switching CSS.Attribute.TEXT_DECORATION between normal and underline . For each of the three instances a separate action command is created in the constants list of class FrmMain for proper handling in dynamic menu and tool bar creation.