View Javadoc

1   /*
2    * SyntaxStyle.java - A simple text style class
3    * Copyright (C) 1999 Slava Pestov
4    *
5    * You may use and modify this package for any purpose. Redistribution is
6    * permitted, in both source and binary form, provided that this notice
7    * remains intact in all source distributions of this package.
8    */
9   package com.ochafik.swing.syntaxcoloring;
10  
11  import java.awt.Color;
12  import java.awt.Font;
13  import java.awt.FontMetrics;
14  import java.awt.Graphics;
15  import java.awt.Toolkit;
16  
17  /**
18   * A simple text style class. It can specify the color, italic flag,
19   * and bold flag of a run of text.
20   * @author Slava Pestov
21   * @version $Id: SyntaxStyle.java,v 1.6 1999/12/13 03:40:30 sp Exp $
22   */
23  public class SyntaxStyle
24  {
25  	/**
26  	 * Creates a new SyntaxStyle.
27  	 * @param color The text color
28  	 * @param italic True if the text should be italics
29  	 * @param bold True if the text should be bold
30  	 */
31  	public SyntaxStyle(Color color, boolean italic, boolean bold)
32  	{
33  		this.color = color;
34  		this.italic = italic;
35  		this.bold = bold;
36  	}
37  
38  	/**
39  	 * Returns the color specified in this style.
40  	 */
41  	public Color getColor()
42  	{
43  		return color;
44  	}
45  
46  	/**
47  	 * Returns true if no font styles are enabled.
48  	 */
49  	public boolean isPlain()
50  	{
51  		return !(bold || italic);
52  	}
53  
54  	/**
55  	 * Returns true if italics is enabled for this style.
56  	 */
57  	public boolean isItalic()
58  	{
59  		return italic;
60  	}
61  
62  	/**
63  	 * Returns true if boldface is enabled for this style.
64  	 */
65  	public boolean isBold()
66  	{
67  		return bold;
68  	}
69  
70  	/**
71  	 * Returns the specified font, but with the style's bold and
72  	 * italic flags applied.
73  	 */
74  	public Font getStyledFont(Font font)
75  	{
76  		if(font == null)
77  			throw new NullPointerException("font param must not"
78  				+ " be null");
79  		if(font.equals(lastFont))
80  			return lastStyledFont;
81  		lastFont = font;
82  		lastStyledFont = new Font(font.getFamily(),
83  			(bold ? Font.BOLD : 0)
84  			| (italic ? Font.ITALIC : 0),
85  			font.getSize());
86  		return lastStyledFont;
87  	}
88  
89  	/**
90  	 * Returns the font metrics for the styled font.
91  	 */
92  	public FontMetrics getFontMetrics(Font font)
93  	{
94  		if(font == null)
95  			throw new NullPointerException("font param must not"
96  				+ " be null");
97  		if(font.equals(lastFont) && fontMetrics != null)
98  			return fontMetrics;
99  		lastFont = font;
100 		lastStyledFont = new Font(font.getFamily(),
101 			(bold ? Font.BOLD : 0)
102 			| (italic ? Font.ITALIC : 0),
103 			font.getSize());
104 		fontMetrics = Toolkit.getDefaultToolkit().getFontMetrics(
105 			lastStyledFont);
106 		return fontMetrics;
107 	}
108 
109 	/**
110 	 * Sets the foreground color and font of the specified graphics
111 	 * context to that specified in this style.
112 	 * @param gfx The graphics context
113 	 * @param font The font to add the styles to
114 	 */
115 	public void setGraphicsFlags(Graphics gfx, Font font)
116 	{
117 		Font _font = getStyledFont(font);
118 		gfx.setFont(_font);
119 		gfx.setColor(color);
120 	}
121 
122 	/**
123 	 * Returns a string representation of this object.
124 	 */
125 	public String toString()
126 	{
127 		return getClass().getName() + "[color=" + color +
128 			(italic ? ",italic" : "") +
129 			(bold ? ",bold" : "") + "]";
130 	}
131 
132 	// private members
133 	private Color color;
134 	private boolean italic;
135 	private boolean bold;
136 	private Font lastFont;
137 	private Font lastStyledFont;
138 	private FontMetrics fontMetrics;
139 }