KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jedit > syntax > SyntaxStyle


1 package org.jedit.syntax;
2
3 /*
4  * SyntaxStyle.java - A simple text style class
5  * Copyright (C) 1999 Slava Pestov
6  *
7  * You may use and modify this package for any purpose. Redistribution is
8  * permitted, in both source and binary form, provided that this notice
9  * remains intact in all source distributions of this package.
10  */

11
12 import java.awt.*;
13 import java.util.StringTokenizer JavaDoc;
14
15 /**
16  * A simple text style class. It can specify the color, italic flag,
17  * and bold flag of a run of text.
18  * @author Slava Pestov
19  * @version $Id: SyntaxStyle.java,v 1.1 2003/12/14 16:29:49 daggerrz Exp $
20  */

21 public class SyntaxStyle
22 {
23    /**
24     * Creates a new SyntaxStyle.
25     * @param color The text color
26     * @param italic True if the text should be italics
27     * @param bold True if the text should be bold
28     */

29    public SyntaxStyle(Color color, boolean italic, boolean bold)
30    {
31       this.color = color;
32       this.italic = italic;
33       this.bold = bold;
34    }
35
36    /**
37     * Returns the color specified in this style.
38     */

39    public Color getColor()
40    {
41       return color;
42    }
43
44    /**
45     * Returns true if no font styles are enabled.
46     */

47    public boolean isPlain()
48    {
49       return !(bold || italic);
50    }
51
52    /**
53     * Returns true if italics is enabled for this style.
54     */

55    public boolean isItalic()
56    {
57       return italic;
58    }
59
60    /**
61     * Returns true if boldface is enabled for this style.
62     */

63    public boolean isBold()
64    {
65       return bold;
66    }
67
68    /**
69     * Returns the specified font, but with the style's bold and
70     * italic flags applied.
71     */

72    public Font getStyledFont(Font font)
73    {
74       if(font == null)
75          throw new NullPointerException JavaDoc("font param must not"
76             + " be null");
77       if(font.equals(lastFont))
78          return lastStyledFont;
79       lastFont = font;
80       lastStyledFont = new Font(font.getFamily(),
81          (bold ? Font.BOLD : 0)
82          | (italic ? Font.ITALIC : 0),
83          font.getSize());
84       return lastStyledFont;
85    }
86
87    /**
88     * Returns the font metrics for the styled font.
89     */

90    public FontMetrics getFontMetrics(Font font)
91    {
92       if(font == null)
93          throw new NullPointerException JavaDoc("font param must not"
94             + " be null");
95       if(font.equals(lastFont) && fontMetrics != null)
96          return fontMetrics;
97       lastFont = font;
98       lastStyledFont = new Font(font.getFamily(),
99          (bold ? Font.BOLD : 0)
100          | (italic ? Font.ITALIC : 0),
101          font.getSize());
102       fontMetrics = Toolkit.getDefaultToolkit().getFontMetrics(
103          lastStyledFont);
104       return fontMetrics;
105    }
106
107    /**
108     * Sets the foreground color and font of the specified graphics
109     * context to that specified in this style.
110     * @param gfx The graphics context
111     * @param font The font to add the styles to
112     */

113    public void setGraphicsFlags(Graphics gfx, Font font)
114    {
115       Font _font = getStyledFont(font);
116       gfx.setFont(_font);
117       gfx.setColor(color);
118    }
119
120    /**
121     * Returns a string representation of this object.
122     */

123    public String JavaDoc toString()
124    {
125       return getClass().getName() + "[color=" + color +
126          (italic ? ",italic" : "") +
127          (bold ? ",bold" : "") + "]";
128    }
129
130    // private members
131
private Color color;
132    private boolean italic;
133    private boolean bold;
134    private Font lastFont;
135    private Font lastStyledFont;
136    private FontMetrics fontMetrics;
137 }
138
Popular Tags