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