001: package net.xoetrope.builder.editor.syntaxhighlight;
002:
003: /*
004: * SyntaxStyle.java - A simple text style class
005: * Copyright (C) 1999 Slava Pestov
006: *
007: * You may use and modify this package for any purpose. Redistribution is
008: * permitted, in both source and binary form, provided that this notice
009: * remains intact in all source distributions of this package.
010: */
011:
012: import java.awt.Color;
013: import java.awt.Font;
014: import java.awt.FontMetrics;
015: import java.awt.Graphics;
016: import java.awt.Toolkit;
017:
018: /**
019: * A simple text style class. It can specify the color, italic flag,
020: * and bold flag of a run of text.
021: * @author Slava Pestov
022: * @version $Id: SyntaxStyle.java,v 1.22 2005/01/05 17:20:49 luano Exp $
023: */
024: public class SyntaxStyle {
025: /**
026: * Creates a new SyntaxStyle.
027: * @param color The text color
028: * @param italic True if the text should be italics
029: * @param bold True if the text should be bold
030: */
031: public SyntaxStyle(Color color, boolean italic, boolean bold) {
032: this .color = color;
033: this .italic = italic;
034: this .bold = bold;
035: }
036:
037: /**
038: * Returns the color specified in this style.
039: */
040: public Color getColor() {
041: return color;
042: }
043:
044: /**
045: * Returns true if no font styles are enabled.
046: */
047: public boolean isPlain() {
048: return !(bold || italic);
049: }
050:
051: /**
052: * Returns true if italics is enabled for this style.
053: */
054: public boolean isItalic() {
055: return italic;
056: }
057:
058: /**
059: * Returns true if boldface is enabled for this style.
060: */
061: public boolean isBold() {
062: return bold;
063: }
064:
065: /**
066: * Returns the specified font, but with the style's bold and
067: * italic flags applied.
068: */
069: public Font getStyledFont(Font font) {
070: if (font == null)
071: throw new NullPointerException("font param must not"
072: + " be null");
073: if (font.equals(lastFont))
074: return lastStyledFont;
075: lastFont = font;
076: lastStyledFont = new Font(font.getFamily(), (bold ? Font.BOLD
077: : 0)
078: | (italic ? Font.ITALIC : 0), font.getSize());
079: return lastStyledFont;
080: }
081:
082: /**
083: * Returns the font metrics for the styled font.
084: */
085: public FontMetrics getFontMetrics(Font font) {
086: if (font == null)
087: throw new NullPointerException("font param must not"
088: + " be null");
089: if (font.equals(lastFont) && fontMetrics != null)
090: return fontMetrics;
091: lastFont = font;
092: lastStyledFont = new Font(font.getFamily(), (bold ? Font.BOLD
093: : 0)
094: | (italic ? Font.ITALIC : 0), font.getSize());
095: fontMetrics = Toolkit.getDefaultToolkit().getFontMetrics(
096: lastStyledFont);
097: return fontMetrics;
098: }
099:
100: /**
101: * Sets the foreground color and font of the specified graphics
102: * context to that specified in this style.
103: * @param gfx The graphics context
104: * @param font The font to add the styles to
105: */
106: public void setGraphicsFlags(Graphics gfx, Font font) {
107: Font _font = getStyledFont(font);
108: gfx.setFont(_font);
109: gfx.setColor(color);
110: }
111:
112: /**
113: * Returns a string representation of this object.
114: */
115: public String toString() {
116: return getClass().getName() + "[color=" + color
117: + (italic ? ",italic" : "") + (bold ? ",bold" : "")
118: + "]";
119: }
120:
121: // private members
122: private Color color;
123: private boolean italic;
124: private boolean bold;
125: private Font lastFont;
126: private Font lastStyledFont;
127: private FontMetrics fontMetrics;
128: }
|