Tutorial 3: Setters



Support This Project


stats counter


Logo SourceForge.net Logo IntelliJ IDEA


In the previous tutorial, we were able to convert a string into a KeyStroke object so that the accelerator key can be set on the menu. This tutorial teaches how to create a setter.

For the illustration purpose, assuming that we want provide the option in XML to adjust the text alignment. We can do this through the setter.

<?xml version="1.0"?>
	<menu text="File">
		<menu text="Open">
			<menuitem text="XML"/>
			<menuitem text="Java"/>
		<menuitem text="Exit" accelerator="control Q"/>
	<menu text="Edit">
		<menuitem text="Cut" horizontalalignment="center"/>
		<menuitem text="Copy" horizontalalignment="right"/>
		<menuitem text="Paste" horizontalalignment="left"/>
	<menu text="Help">
		<menuitem text="About" accelerator="F1"/>

In this case, it is probably not the best idea to use a converter that convert "center" to the corresponding SwingConstants.CENTER, since later on we may need to have a different conversion for a different field. In this case, a setter can be used for the specific attribute setting.

Step 1: Writing an Alignment Setter

A setter is also simple. The obj is the current object that is being processed and value is the attribute value to be set for the object. Note that value is an object rather than string. This is because there are other sources that could generate the attribute value that may not be a string.

In general, exceptions generated will be automatically wrapped and handled by CookXml's DecodeEngine, so it is not that necessary to explicitly deal with runtime exceptions.

import javax.swing.*;

import cookxml.core.DecodeEngine;
import cookxml.core.exception.SetterException;
import cookxml.core.interfaces.Setter;

public class AlignmentSetter implements Setter
	public void setAttribute (String tagNS, String tag, String attrNS, String attr, Object obj, Object value, DecodeEngine decodeEngine) throws SetterException
		String attrValue = (String)value;
		int alignment;
		if ("center".equalsIgnoreCase (attrValue))
			alignment = SwingConstants.CENTER;
		else if ("left".equalsIgnoreCase (attrValue))
			alignment = SwingConstants.LEFT;
		else if ("RIGHT".equalsIgnoreCase (attrValue))
			alignment = SwingConstants.RIGHT;
			throw new SetterException (decodeEngine, null, this, tagNS, tag, attrNS, attr, obj, value);
		((JMenuItem)obj).setHorizontalAlignment (alignment);

Step 2: Installing the Setter

Installing the setter involves telling the tag library to execute the setter when the specific combination of tag and attribute is encountered.

tagLibrary.setSetter ("menuitem", "horizontalalignment", new AlignmentSetter ());

While in this case, a setter is installed for a specific tag and element, as mentioned in tutorial 1, one can install a generic setter as well, by passing null as the parameter in the corresponding tag library setting.

CookXml also provide some commonly used setters. ConstantSetter uses Java reflection to lookup constants inside a Java class to reduce the need to write a custom setter. CallFunctionSetter is another setter that one can use to specify the function call with the given parameters classes.


(c) Copyright 2004-2007 Heng Yuan. All rights reserved.

Valid XHTML 1.0! Valid CSS!