1. StyleDefinition.java.

Listing 1. StyleDefinition.java.


package pec.style;

import java.awt.Component;
import java.awt.Font;
import java.awt.Color;
import java.util.ResourceBundle;
import java.util.Locale;
import java.util.Hashtable;
import java.util.MissingResourceException;
import java.util.StringTokenizer;
import java.util.Enumeration;

public class StyleDefinition
{
	protected static final int BACKGROUND =0;
	protected static final int FOREGROUND =1;
	private static final String MFFONT = ".font.";
	private static final String SUFSIZE = "size";
	private static final String SUFFACE = "face";
	private static final String SUFSTYLE = "style";
	private static final String MFCOLOR = ".color.";
	private static final String SUFFORE = "foreground";
	private static final String SUFBACK = "background";
	private ResourceBundle rStyleSheet;
	private Hashtable hshStyles = new Hashtable();
	private Hashtable hshStylePrefix = new Hashtable();
	private Object[] objStyleTemp = new Object[3];

	public StyleDefinition(String sResourceName)
	{
		initBundle(sResourceName);
		initKeys();
		initStyles();
	}

	public StyleDefinition(String sResourceName, Locale locale)
	{
		initBundle(sResourceName, locale);
		initKeys();
		initStyles();
	}

	public StyleDefinition(ResourceBundle resources)
	{
		rStyleSheet = resources;
		initKeys();
		initStyles();
	}

	public void setStyleSheetResources(ResourceBundle resources)
	{
		rStyleSheet = resources;
		initKeys();
		initStyles();
	}

	public ResourceBundle getStyleSheetResources()
	{
		return rStyleSheet;
	}

	private void initBundle(String resourceName)
	{
		try
		{
			rStyleSheet = ResourceBundle.getBundle(resourceName);
		}
		catch(MissingResourceException e)
		{
			e.printStackTrace();
		}
	}

	private void initBundle(String resourceName, Locale locale) throws MissingResourceException
	{
			rStyleSheet = ResourceBundle.getBundle(
			 resourceName, locale);
	}

	private void initStyles()
	{
		try
		{
			Enumeration enum = hshStylePrefix.keys();
			while(enum.hasMoreElements())
			{
				String sKeyName = (String) enum.nextElement();
				Object[] objs = new Object[3];
				objs[0] = createFont(sKeyName);
				objs[1] = createColor(sKeyName, BACKGROUND);
				objs[2] = createColor(sKeyName, FOREGROUND);
				hshStyles.put(sKeyName, objs);
			}
		}
		catch(MissingResourceException exception)
		{
			exception.printStackTrace();
		}
	}
	private void initKeys()
	{
		Enumeration enumeration = rStyleSheet.getKeys();
		while(enumeration.hasMoreElements())
		{

			StringTokenizer stringTokenizer = 
			 new StringTokenizer((
			 String)enumeration.nextElement(), ".");
			String sStylePrefix = null;
			if(stringTokenizer.hasMoreTokens())
			{
				sStylePrefix = stringTokenizer.nextToken();

				if(!hshStylePrefix.containsKey(sStylePrefix))
				{
					hshStylePrefix.put(sStylePrefix, new Object());
				}
			}
		}
	}

	private Font createFont(String sKeyName) throws MissingResourceException
	{
		System.out.println(sKeyName);
		return new Font
				(
				rStyleSheet.getString(sKeyName + MFFONT + SUFFACE),
				convertFontStyle
				(
                  		new Character(((String)rStyleSheet.getObject
					(
						sKeyName + MFFONT + SUFSTYLE
					)
						).charAt(0))
				),
				Integer.parseInt
				(
					rStyleSheet.getString
					(
						sKeyName + MFFONT + SUFSIZE
							)
						)
					);
	}

	private int convertFontStyle(Character charStyle)
	{
		switch(charStyle.charValue())
		{
			case 'P' : return Font.PLAIN;
			case 'B' : return Font.BOLD;
			case 'I' : return Font.ITALIC;
			case 'G' : return (Font.BOLD + Font.ITALIC);
		}
		return Font.PLAIN;
	}


	private Color createColor(String sKeyName, int colorType) throws MissingResourceException
	{
		String[] sColorInts = null;
		int[] iColorInts = new int[3];
		StringTokenizer stringTokenizer = new StringTokenizer("");
		switch(colorType)
		{
			case BACKGROUND:
			{
				stringTokenizer = 
				new StringTokenizer(rStyleSheet.getString(
				sKeyName + MFCOLOR + SUFBACK));
				break;
			}
			case FOREGROUND:
			{
				stringTokenizer = 
				 new StringTokenizer(rStyleSheet.getString(
				 sKeyName + MFCOLOR + SUFFORE));
				break;
			}
		}
		int countthis =0;
		while(stringTokenizer.hasMoreTokens())
		{
			iColorInts[countthis] =
			Integer.parseInt(stringTokenizer.nextToken());
			countthis++;
		}
		return new Color(iColorInts[0], iColorInts[1], iColorInts[2]);
	}

	public boolean isStyleAvailable(String style)
	{
		return hshStyles.containsKey(style);
	}

	public Object[] getStyle(String styleName) throws 
	 MissingResourceException, Exception
	{
		if(isStyleAvailable(styleName))
			return (Object[]) hshStyles.get(styleName);
		else throw new Exception("Style " + styleName + 
		 " does not exist.");

	}

	

}