001: /*
002: * The contents of this file are subject to the Mozilla Public License
003: * Version 1.1 (the "License"); you may not use this file except in
004: * compliance with the License. You may obtain a copy of the License at
005: * http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
009: * License for the specific language governing rights and limitations
010: * under the License.
011: *
012: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013: *
014: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
015: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
016: *
017: * Contributor(s):
018: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
019: *
020: * If you didn't download this code from the following link, you should check
021: * if you aren't using an obsolete version: http://www.isqlviewer.com
022: */
023: package org.isqlviewer.swing.text;
024:
025: import java.awt.Color;
026: import java.awt.Font;
027: import java.util.Hashtable;
028: import java.util.prefs.PreferenceChangeEvent;
029: import java.util.prefs.PreferenceChangeListener;
030:
031: import javax.swing.JTextPane;
032: import javax.swing.text.Style;
033: import javax.swing.text.StyleConstants;
034:
035: import org.isqlviewer.sql.processor.TokenType;
036:
037: /**
038: * @author Mark A. Kobold <mkobold at isqlviewer dot com>
039: * @version 1.0
040: */
041: public class SyntaxStylizer implements PreferenceChangeListener {
042:
043: private Hashtable<TokenType, Style> styleMap = new Hashtable<TokenType, Style>();
044: private JTextPane componentOwner = null;
045:
046: public SyntaxStylizer(JTextPane componentOwner) {
047:
048: this .componentOwner = componentOwner;
049: initializeStyles();
050: }
051:
052: public Style styleForWord(TokenType tokenType) {
053:
054: Style style = styleMap.get(tokenType);
055: if (style == null) {
056: return styleMap.get(TokenType.WHITESPACE);
057: }
058: return style;
059: }
060:
061: public void preferenceChange(PreferenceChangeEvent event) {
062:
063: }
064:
065: public void changeStyle(TokenType type, Color color) {
066:
067: Style style = componentOwner.addStyle(type.toString(), null);
068: StyleConstants.setForeground(style, color);
069: styleMap.put(type, style);
070: }
071:
072: public void changeStyle(TokenType type, Color color, int fontStyle) {
073:
074: Style style = componentOwner.addStyle(type.toString(), null);
075: StyleConstants.setForeground(style, color);
076: if ((fontStyle & Font.BOLD) != 0)
077: StyleConstants.setBold(style, true);
078: if ((fontStyle & Font.ITALIC) != 0)
079: StyleConstants.setItalic(style, true);
080: styleMap.put(type, style);
081: }
082:
083: private void initializeStyles() {
084:
085: changeStyle(TokenType.UNRECOGNIZED, Color.RED);
086: changeStyle(TokenType.WHITESPACE, Color.BLACK);
087: changeStyle(TokenType.WORD, Color.BLACK);
088:
089: changeStyle(TokenType.COMMENT, Color.decode("#3F7F5F"),
090: Font.ITALIC);
091: changeStyle(TokenType.START_COMMENT, Color.decode("#3F7F5F"),
092: Font.ITALIC);
093: changeStyle(TokenType.MID_COMMENT, Color.decode("#3F7F5F"),
094: Font.ITALIC);
095: changeStyle(TokenType.END_COMMENT, Color.decode("#3F7F5F"),
096: Font.ITALIC);
097:
098: changeStyle(TokenType.VARIABLE, Color.decode("#003e85"),
099: Font.ITALIC | Font.BOLD);
100:
101: changeStyle(TokenType.KEYWORD, Color.decode("#7F0069"),
102: Font.BOLD);
103: changeStyle(TokenType.STRING, Color.BLUE);
104: changeStyle(TokenType.CHARACTER, Color.decode("#008400"));
105: changeStyle(TokenType.FUNCTION, Color.BLUE);
106:
107: changeStyle(TokenType.TABLE_NAME, Color.decode("#003e85"));
108:
109: Style style = componentOwner.addStyle(TokenType.TABLE_NAME
110: .name(), null);
111: StyleConstants.setForeground(style, Color.decode("#3f7f5f"));
112: StyleConstants.setUnderline(style, true);
113: style.addAttribute("hyperlinked", Boolean.TRUE);
114:
115: styleMap.put(TokenType.TABLE_NAME, style);
116: }
117:
118: }
|