KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * SyntaxStyle.java
28  *
29  */

30
31 package org.syntax.jedit;
32
33 import java.awt.*;
34 import java.util.StringTokenizer JavaDoc;
35
36 /**
37  * A simple text style class. It can specify the color, italic flag,
38  * and bold flag of a run of text.
39  * @author Slava Pestov
40  * @version $Id: SyntaxStyle.java 932 2006-10-20 09:32:45Z gtoffoli $
41  */

42 public class SyntaxStyle
43 {
44     /**
45      * Creates a new SyntaxStyle.
46      * @param color The text color
47      * @param italic True if the text should be italics
48      * @param bold True if the text should be bold
49      */

50     public SyntaxStyle(Color color, boolean italic, boolean bold)
51     {
52         this.color = color;
53         this.italic = italic;
54         this.bold = bold;
55     }
56
57     /**
58      * Returns the color specified in this style.
59      */

60     public Color getColor()
61     {
62         return color;
63     }
64
65     /**
66      * Returns true if no font styles are enabled.
67      */

68     public boolean isPlain()
69     {
70         return !(bold || italic);
71     }
72
73     /**
74      * Returns true if italics is enabled for this style.
75      */

76     public boolean isItalic()
77     {
78         return italic;
79     }
80
81     /**
82      * Returns true if boldface is enabled for this style.
83      */

84     public boolean isBold()
85     {
86         return bold;
87     }
88
89     /**
90      * Returns the specified font, but with the style's bold and
91      * italic flags applied.
92      */

93     public Font getStyledFont(Font font)
94     {
95         if(font == null)
96             throw new NullPointerException JavaDoc("font param must not"
97                 + " be null");
98         if(font.equals(lastFont))
99             return lastStyledFont;
100         lastFont = font;
101         lastStyledFont = new Font(font.getFamily(),
102             (bold ? Font.BOLD : 0)
103             | (italic ? Font.ITALIC : 0),
104             font.getSize());
105         return lastStyledFont;
106     }
107
108     /**
109      * Returns the font metrics for the styled font.
110      */

111     public FontMetrics getFontMetrics(Font font)
112     {
113         if(font == null)
114             throw new NullPointerException JavaDoc("font param must not"
115                 + " be null");
116         if(font.equals(lastFont) && fontMetrics != null)
117             return fontMetrics;
118         lastFont = font;
119         lastStyledFont = new Font(font.getFamily(),
120             (bold ? Font.BOLD : 0)
121             | (italic ? Font.ITALIC : 0),
122             font.getSize());
123         fontMetrics = Toolkit.getDefaultToolkit().getFontMetrics(
124             lastStyledFont);
125         return fontMetrics;
126     }
127
128     /**
129      * Sets the foreground color and font of the specified graphics
130      * context to that specified in this style.
131      * @param gfx The graphics context
132      * @param font The font to add the styles to
133      */

134     public void setGraphicsFlags(Graphics gfx, Font font)
135     {
136         Font _font = getStyledFont(font);
137         gfx.setFont(_font);
138         gfx.setColor(color);
139     }
140
141     /**
142      * Returns a string representation of this object.
143      */

144     public String JavaDoc toString()
145     {
146         return getClass().getName() + "[color=" + color +
147             (italic ? ",italic" : "") +
148             (bold ? ",bold" : "") + "]";
149     }
150
151     // private members
152
private Color color;
153     private boolean italic;
154     private boolean bold;
155     private Font lastFont;
156     private Font lastStyledFont;
157     private FontMetrics fontMetrics;
158 }
159
Popular Tags