package org.ui.editors.code;

import java.awt.Color;
import javax.swing.JTextPane;
import org.ui.syntaxes.CharAttrib;
import org.ui.syntaxes.SyntaxHighlighter;

public class JavaSyntaxHighlighter
  extends SyntaxHighlighter
{
  private static final String OPERATOR_REGEX = "[\\+\\-\\*/!&|\\^~.:?=]";
  private static final String DELEMITER_REGEX = "[\\[\\]\\(\\)\\{\\},;]";
  private static final String KEYWORD_REGEX = "\\W?(\\bextends|implements|this|super|import|package|public|private|throws|throw|protected|void|class|static|volatile|const|final|break|continue|while|for|do|switch|case|default|if|else\\b)\\W";
  private static final String DATATYPES_REGEX = "\\W(\\bbyte|boolean|short|char|int|long|float|double\\b)\\W";
  private static final String COMMENT_REGEX = "(/\\*(.)*?\\*/)|(//[^\\n]*)";
  private static final String JAVADOC_REGEX = "(/\\*\\*(.)*?\\*/)";
  private static final String ESC_SEQ_EXT_REGEX = "\\bTODO|@author|@version|@date|@serial|@apiNote|@see|@implNote|@since|@category|\\\\|\\n|\\t|\\?|\\'|\\\"|\\b|\\r|\\f\\b";
  private static final String STRING_REGEX = "\".*?\"|'.?'";

  private static Color RED = new Color(0xff604a);
  private static Color ORANGE = new Color(0xFFA84A);
  private static Color YELLOW = new Color(0xffe74a);
  private static Color GREEN = new Color(0x8fff4a);
  private static Color CYAN = new Color(0x4ae9ff);
  private static Color BLUE = new Color(0x4a62ff);

  public JavaSyntaxHighlighter(JTextPane textPane, int fontSize) {
    super(textPane, 4.0F, fontSize, textPane.getBackground());

    createHighlightPattern(OPERATOR_REGEX, new CharAttrib(false, false, RED));

    createHighlightPattern(DELEMITER_REGEX, new CharAttrib(false, false, Color.LIGHT_GRAY));

    createHighlightPattern(KEYWORD_REGEX, new CharAttrib(false, false, RED));

    createHighlightPattern(DATATYPES_REGEX, new CharAttrib(false, false, RED));

    createHighlightPattern(COMMENT_REGEX, new CharAttrib(false, true, Color.GRAY));

    createHighlightPattern(JAVADOC_REGEX, new CharAttrib(false, false, CYAN));

    createHighlightPattern(ESC_SEQ_EXT_REGEX, new CharAttrib(true, false, CYAN.darker()));

    createHighlightPattern(STRING_REGEX, new CharAttrib(false, false, ORANGE));
  }

  public final void updateSource() {
    highlight();
  }
}
