3. StyleObject.java.

Listing 3. StyleObject.java.


package pec.style;

import java.awt.Component;
import java.awt.Color;
import java.awt.Font;
import java.util.MissingResourceException;
import java.util.Vector;
import java.util.Enumeration;

public class StyleObject implements StyleFormatter
{
	private StyleDefinition styleDef = null;
	private boolean debug = true;

	public StyleObject(StyleDefinition styleDefinition)
	{
		setStyleDefinition(styleDefinition);
	}

	public StyleDefinition getStyleDefinition()
	{
		return styleDef;
	}

	public void setStyleDefinition(StyleDefinition style)
	{
		styleDef = style;
	}


	public Component applyStyleDefinition(
	 Component component, String styleKey) throws
	 java.util.MissingResourceException, Exception
	{
		Object objs[] = getStyleDefinition().getStyle(styleKey);
		component = applyFont((Component)component, (Font)objs[0]);
		component = applyColor((Component)component,(
		 Color)objs[1], StyleDefinition.BACKGROUND);
		component = applyColor((Component)component,(
		 Color)objs[2], StyleDefinition.FOREGROUND);
		return component;
	}

	public Component[] applyStyleDefinition(
	 Component[] component, String styleKey) throws
	 MissingResourceException, Exception
	{
		for(int i=0; i < component.length; i++)
		{
			component[i] = applyStyleDefinition(
			 component[i], styleKey);
		}
		return component;
	}

	public Vector applyStyleDefinition(
	 Vector vector, String styleKey) throws
	 MissingResourceException, Exception
	{
		Vector returnVector = new Vector();
		Enumeration enumeration = vector.elements();
		while(enumeration.hasMoreElements())
		{
			returnVector.addElement(applyStyleDefinition((
			 Component)enumeration.nextElement(), styleKey));
		}
		enumeration = null;
		return returnVector;
	}

	private Component applyFont(Component component, Font font)
	{
		component.setFont(font);
		return component;
	}

	private Component applyColor(
	 Component component, Color color, int colorType) throws
	 Exception
	{
		switch(colorType)
		{
			case(StyleDefinition.BACKGROUND):
				component.setBackground(color); return component;
			case(StyleDefinition.FOREGROUND):
				component.setForeground(color); return component;
		}
		throw new Exception(
		 "Color type was neither background or foreground!");
	}
}