1
2
3
4
5
6
7
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
19
20
21
22
23 public class SyntaxStyle
24 {
25
26
27
28
29
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
40
41 public Color getColor()
42 {
43 return color;
44 }
45
46
47
48
49 public boolean isPlain()
50 {
51 return !(bold || italic);
52 }
53
54
55
56
57 public boolean isItalic()
58 {
59 return italic;
60 }
61
62
63
64
65 public boolean isBold()
66 {
67 return bold;
68 }
69
70
71
72
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
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
111
112
113
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
124
125 public String toString()
126 {
127 return getClass().getName() + "[color=" + color +
128 (italic ? ",italic" : "") +
129 (bold ? ",bold" : "") + "]";
130 }
131
132
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 }