package net.sf.hibern8ide.highlighter;
/*
=====================================================================

  RETypes.java
  
  Created by Claude Duguay
  Copyright (c) 2002
  
=====================================================================
*/

import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class RETypes {
	protected List typeList;
	protected List wordstyleList;

	public RETypes() {
		typeList = new ArrayList();
		wordstyleList = new ArrayList();
	}

	public void addTokenType(String name, String expr, Color color, int fontstyle) {
		typeList.add(new Type(name, expr, color, fontstyle));
	}

	public void addWordStyle(String name, Color color, int fontstyle) {
		wordstyleList.add(new WordStyle(name, color, fontstyle));
	}

	protected Type getType(int index) {
		return (Type) typeList.get(index);
	}


	public String getExpr(int index) {
		return getType(index).expr;
	}

	public void setStyles(StyledDocument doc) {
		for (int i = 0; i < typeList.size(); i++) {
			Type type = getType(i);
			String name = type.name;
			Color color = type.color;
			int fontStyle = type.fontstyle;
            setStyle(doc, name, color, fontStyle);
		}

		for (int i = 0; i < wordstyleList.size(); i++) {
			WordStyle wst = (WordStyle) wordstyleList.get(i);
			String name = wst.name;
			Color color = wst.color;
			int fontStyle = wst.fontstyle;
            setStyle(doc, name, color, fontStyle);
		}

	}

	private void setStyle(StyledDocument doc, String name, Color color, int fontStyle) {
		if (color != null) {
			Style style = doc.addStyle(name, null);
			StyleConstants.setForeground(style, color);
			if (fontStyle == Font.BOLD)
				StyleConstants.setBold(style, true);
			if (fontStyle == Font.ITALIC)
				StyleConstants.setItalic(style, true);
		}
	}

	public String getExpression() {
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < getTypeCount(); i++) {
			if (i > 0)
				buffer.append('|');
			buffer.append('(');
			buffer.append(getExpr(i));
			buffer.append(')');
		}
		return buffer.toString();
	}

	public Pattern getPattern() {
		String expression = getExpression();
		return Pattern.compile(expression, Pattern.DOTALL);
	}

	public Matcher getMatcher(String text) {
		return getPattern().matcher(text);
	}

	public static class WordStyle {
		public WordStyle(String name2, Color color2, int fontstyle2) {
			name = name2;
			color = color2;
			fontstyle = fontstyle2;
		}

		protected String name;
		protected Color color;
		protected int fontstyle;
	}

	public static class Type {
		protected String name;
		protected String expr;
		protected Color color;
		protected int fontstyle;

		public Type(String name, String expr, Color color, int fontstyle) {
			this.name = name;
			this.expr = expr;
			this.color = color;
			this.fontstyle = fontstyle;
		}

		public String toString() {
			return "REType:" + name + ", " + expr + ", " + color + ", " + fontstyle;
		}
	}

	Map word2style = new HashMap();
	public String getStyleName(String word) {
		return (String) word2style.get(word);
	}

    public String getName(int index) {
        return ((Type)typeList.get(index)).name;
    }
    
	public int getTypeCount() {
		return typeList.size();		
	}

	public Object getColor(int index) {
        return ((Type)typeList.get(index)).color;
	}
}
