package com.dbxml.db.admin.swing;

/*
 * dbXML - Native XML Database
 * Copyright (C) 1999-2004  The dbXML Group, L.L.C.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: SyntaxTextArea.java,v 1.3 2004/02/12 00:17:55 bradford Exp $
 */

import java.awt.*;
import org.jedit.syntax.*;

import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.border.AbstractBorder;

/**
 * SyntaxTextArea
 */

public final class SyntaxTextArea extends JEditTextArea {
   public static final String LANG_BATCHFILE = "BatchFile";
   public static final String LANG_CPP = "C++";
   public static final String LANG_C = "C";
   public static final String LANG_EIFFEL = "Eiffel";
   public static final String LANG_IDL = "IDL";
   public static final String LANG_HTML = "HTML";
   public static final String LANG_JAVASCRIPT = "JavaScript";
   public static final String LANG_JAVA = "Java";
   public static final String LANG_MAKEFILE = "Makefile";
   public static final String LANG_PATCH = "Patch";
   public static final String LANG_PERL = "Perl";
   public static final String LANG_PHP = "PHP";
   public static final String LANG_PROPERTIES = "Properties";
   public static final String LANG_PYTHON = "Python";
   public static final String LANG_SHELLSCRIPT = "Shell";
   public static final String LANG_SQL = "SQL";
   public static final String LANG_TEX = "TeX";
   public static final String LANG_TSQL = "TransactSQL";
   public static final String LANG_XML = "XML";
   public static final String LANG_XPATH = "XPath";
   public static final String LANG_TEXT = "Text";

   private boolean highlighting = true;
   private String language = LANG_XML;

   public SyntaxTextArea() {
      super();
      setFont(new Font("Monospaced", 0, 12));

      setElectricScroll(0);
      setMinimumSize(new Dimension(0, 50));
      setPreferredSize(new Dimension(0, 50));

      setBorder(new SyntaxTextAreaBorder());

      NumberedTextPainter painter = new NumberedTextPainter(this, TextAreaDefaults.getDefaults());
      setPainter(painter);

      setDefaults(TextAreaDefaults.getDefaults());

      painter.setMarginColor(new Color(192, 192, 192));
      painter.setInvalidLinesPainted(false);
      painter.setLineHighlightEnabled(false);
      painter.setEOLMarkersPainted(false);
      painter.setCaretColor(Color.black);

      SyntaxStyle[] styles = painter.getStyles();
      styles[Token.COMMENT1] = new SyntaxStyle(Color.gray, false, false);
      styles[Token.COMMENT2] = new SyntaxStyle(Color.lightGray, false, true);
      styles[Token.KEYWORD1] = new SyntaxStyle(Color.blue, false, true);
      styles[Token.KEYWORD2] = new SyntaxStyle(Color.magenta, false, false);
      styles[Token.KEYWORD3] = new SyntaxStyle(Color.darkGray, false, true);
      styles[Token.LITERAL1] = new SyntaxStyle(Color.red, false, false);
      styles[Token.LITERAL2] = new SyntaxStyle(Color.pink, false, false);
      styles[Token.OPERATOR] = new SyntaxStyle(Color.darkGray, false, false);
      styles[Token.LABEL] = new SyntaxStyle(Color.orange, false, false);
      painter.setStyles(styles);

      setDocument(new SyntaxDocument());
      setLanguage(language);
   }

   public void setEnabled(boolean enabled) {
      setEditable(enabled);
   }

   public boolean isEnabled() {
      return isEditable();
   }

   public void setHighlighting(boolean highlighting) {
      this.highlighting = highlighting;
      if ( highlighting )
         setLanguage(language);
      else
         setTokenMarker(null);
   }

   public boolean isHighlighting() {
      return highlighting;
   }

   public void setLanguage(String language) {
      this.language = language;
      if ( language.equals(LANG_BATCHFILE) )
         setTokenMarker(new BatchFileTokenMarker());
      else if ( language.equals(LANG_CPP) )
         setTokenMarker(new CCTokenMarker());
      else if ( language.equals(LANG_C) )
         setTokenMarker(new CTokenMarker());
      else if ( language.equals(LANG_EIFFEL) )
         setTokenMarker(new EiffelTokenMarker());
      else if ( language.equals(LANG_IDL) )
         setTokenMarker(new IDLTokenMarker());
      else if ( language.equals(LANG_HTML) )
         setTokenMarker(new HTMLTokenMarker());
      else if ( language.equals(LANG_JAVASCRIPT) )
         setTokenMarker(new JavaScriptTokenMarker());
      else if ( language.equals(LANG_JAVA) )
         setTokenMarker(new JavaTokenMarker());
      else if ( language.equals(LANG_MAKEFILE) )
         setTokenMarker(new MakefileTokenMarker());
      else if ( language.equals(LANG_PATCH) )
         setTokenMarker(new PatchTokenMarker());
      else if ( language.equals(LANG_PERL) )
         setTokenMarker(new PerlTokenMarker());
      else if ( language.equals(LANG_PHP) )
         setTokenMarker(new PHPTokenMarker());
      else if ( language.equals(LANG_PROPERTIES) )
         setTokenMarker(new PropsTokenMarker());
      else if ( language.equals(LANG_PYTHON) )
         setTokenMarker(new PythonTokenMarker());
      else if ( language.equals(LANG_SHELLSCRIPT) )
         setTokenMarker(new ShellScriptTokenMarker());
      else if ( language.equals(LANG_SQL) )
         setTokenMarker(new TSQLTokenMarker());
      else if ( language.equals(LANG_TEX) )
         setTokenMarker(new TeXTokenMarker());
      else if ( language.equals(LANG_TSQL) )
         setTokenMarker(new TSQLTokenMarker());
      else if ( language.equals(LANG_XML) )
         setTokenMarker(new XMLTokenMarker());
      else if ( language.equals(LANG_XPATH) )
         setTokenMarker(new XPathTokenMarker());
      else
         setTokenMarker(null);
   }

   public String getLanguage() {
      return language;
   }

   private static class SyntaxTextAreaBorder extends AbstractBorder {
      private static Insets insets = new Insets(1, 1, 2, 2);

      public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
         g.translate(x, y);

         LookAndFeel lf = UIManager.getLookAndFeel();
         //g.setColor(lf.getDefaults().getColor("primaryControlShadow"));
         g.setColor(Color.gray);
         g.drawRect(0, 0, w - 2, h - 2);
         g.setColor(lf.getDefaults().getColor("controlHighlight"));

         g.drawLine(w - 1, 1, w - 1, h - 1);
         g.drawLine(1, h - 1, w - 1, h - 1);

         g.setColor(lf.getDefaults().getColor("control"));
         g.drawLine(w - 2, 2, w - 2, 2);
         g.drawLine(1, h - 2, 1, h - 2);

         g.translate(-x, -y);
      }

      public Insets getBorderInsets(Component c) {
         return insets;
      }
   }

}
