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