001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * SyntaxStyle.java
028: *
029: */
030:
031: package org.syntax.jedit;
032:
033: import java.awt.*;
034: import java.util.StringTokenizer;
035:
036: /**
037: * A simple text style class. It can specify the color, italic flag,
038: * and bold flag of a run of text.
039: * @author Slava Pestov
040: * @version $Id: SyntaxStyle.java 1167 2008-01-15 18:49:05Z gtoffoli $
041: */
042: public class SyntaxStyle {
043: /**
044: * Creates a new SyntaxStyle.
045: * @param color The text color
046: * @param italic True if the text should be italics
047: * @param bold True if the text should be bold
048: */
049: public SyntaxStyle(Color color, boolean italic, boolean bold) {
050: this .color = color;
051: this .italic = italic;
052: this .bold = bold;
053: }
054:
055: /**
056: * Returns the color specified in this style.
057: */
058: public Color getColor() {
059: return color;
060: }
061:
062: /**
063: * Returns true if no font styles are enabled.
064: */
065: public boolean isPlain() {
066: return !(bold || italic);
067: }
068:
069: /**
070: * Returns true if italics is enabled for this style.
071: */
072: public boolean isItalic() {
073: return italic;
074: }
075:
076: /**
077: * Returns true if boldface is enabled for this style.
078: */
079: public boolean isBold() {
080: return bold;
081: }
082:
083: /**
084: * Returns the specified font, but with the style's bold and
085: * italic flags applied.
086: */
087: public Font getStyledFont(Font font) {
088: if (font == null)
089: throw new NullPointerException("font param must not"
090: + " be null");
091: if (font.equals(lastFont))
092: return lastStyledFont;
093: lastFont = font;
094: lastStyledFont = new Font(font.getFamily(), (bold ? Font.BOLD
095: : 0)
096: | (italic ? Font.ITALIC : 0), font.getSize());
097: return lastStyledFont;
098: }
099:
100: /**
101: * Returns the font metrics for the styled font.
102: */
103: public FontMetrics getFontMetrics(Font font) {
104: if (font == null)
105: throw new NullPointerException("font param must not"
106: + " be null");
107: if (font.equals(lastFont) && fontMetrics != null)
108: return fontMetrics;
109: lastFont = font;
110: lastStyledFont = new Font(font.getFamily(), (bold ? Font.BOLD
111: : 0)
112: | (italic ? Font.ITALIC : 0), font.getSize());
113: fontMetrics = Toolkit.getDefaultToolkit().getFontMetrics(
114: lastStyledFont);
115: return fontMetrics;
116: }
117:
118: /**
119: * Sets the foreground color and font of the specified graphics
120: * context to that specified in this style.
121: * @param gfx The graphics context
122: * @param font The font to add the styles to
123: */
124: public void setGraphicsFlags(Graphics gfx, Font font) {
125: Font _font = getStyledFont(font);
126: gfx.setFont(_font);
127: gfx.setColor(color);
128: }
129:
130: /**
131: * Returns a string representation of this object.
132: */
133: public String toString() {
134: return getClass().getName() + "[color=" + color
135: + (italic ? ",italic" : "") + (bold ? ",bold" : "")
136: + "]";
137: }
138:
139: // private members
140: private Color color;
141: private boolean italic;
142: private boolean bold;
143: private Font lastFont;
144: private Font lastStyledFont;
145: private FontMetrics fontMetrics;
146: }
|