1 | /*
|
---|
2 | * 08/06/2004
|
---|
3 | *
|
---|
4 | * Style.java - A set of traits for a particular token type to use while
|
---|
5 | * painting.
|
---|
6 | *
|
---|
7 | * This library is distributed under a modified BSD license. See the included
|
---|
8 | * RSyntaxTextArea.License.txt file for details.
|
---|
9 | */
|
---|
10 | package org.fife.ui.rsyntaxtextarea;
|
---|
11 |
|
---|
12 | import java.awt.Color;
|
---|
13 | import java.awt.Font;
|
---|
14 | import java.awt.FontMetrics;
|
---|
15 | import javax.swing.JPanel;
|
---|
16 |
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * The color and style information for a token type. Each token type in an
|
---|
20 | * <code>RSyntaxTextArea</code> has a corresponding <code>Style</code>; this
|
---|
21 | * <code>Style</code> tells us the following things:
|
---|
22 | *
|
---|
23 | * <ul>
|
---|
24 | * <li>What foreground color to use for tokens of this type.</li>
|
---|
25 | * <li>What background color to use.</li>
|
---|
26 | * <li>The font to use.</li>
|
---|
27 | * <li>Whether the token should be underlined.</li>
|
---|
28 | * </ul>
|
---|
29 | *
|
---|
30 | * @author Robert Futrell
|
---|
31 | * @version 0.6
|
---|
32 | */
|
---|
33 | public class Style implements Cloneable {
|
---|
34 |
|
---|
35 | public static final Color DEFAULT_FOREGROUND = Color.BLACK;
|
---|
36 | public static final Color DEFAULT_BACKGROUND = null;
|
---|
37 | public static final Font DEFAULT_FONT = null;
|
---|
38 |
|
---|
39 | public Color foreground;
|
---|
40 | public Color background;
|
---|
41 | public boolean underline;
|
---|
42 | public Font font;
|
---|
43 |
|
---|
44 | FontMetrics fontMetrics;
|
---|
45 |
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Creates a new style defaulting to black foreground, no
|
---|
49 | * background, and no styling.
|
---|
50 | */
|
---|
51 | public Style() {
|
---|
52 | this(DEFAULT_FOREGROUND);
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Creates a new style with the specified foreground and no styling.
|
---|
58 | *
|
---|
59 | * @param fg The foreground color to use.
|
---|
60 | */
|
---|
61 | public Style(Color fg) {
|
---|
62 | this(fg, DEFAULT_BACKGROUND);
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Creates a new style with the specified colors and no styling.
|
---|
68 | *
|
---|
69 | * @param fg The foreground color to use.
|
---|
70 | * @param bg The background color to use.
|
---|
71 | */
|
---|
72 | public Style(Color fg, Color bg) {
|
---|
73 | this(fg, bg, DEFAULT_FONT);
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Creates a new style.
|
---|
79 | *
|
---|
80 | * @param fg The foreground color to use.
|
---|
81 | * @param bg The background color to use.
|
---|
82 | * @param font The font for this syntax scheme.
|
---|
83 | */
|
---|
84 | public Style(Color fg, Color bg, Font font) {
|
---|
85 | this(fg, bg, font, false);
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Creates a new style.
|
---|
91 | *
|
---|
92 | * @param fg The foreground color to use.
|
---|
93 | * @param bg The background color to use.
|
---|
94 | * @param font The font for this syntax scheme.
|
---|
95 | * @param underline Whether or not to underline tokens with this style.
|
---|
96 | */
|
---|
97 | public Style(Color fg, Color bg, Font font, boolean underline) {
|
---|
98 | foreground = fg;
|
---|
99 | background = bg;
|
---|
100 | this.font = font;
|
---|
101 | this.underline = underline;
|
---|
102 | this.fontMetrics = font==null ? null :
|
---|
103 | new JPanel().getFontMetrics(font); // Default, no rendering hints!
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Returns whether or not two (possibly <code>null</code>) objects are
|
---|
109 | * equal.
|
---|
110 | */
|
---|
111 | private boolean areEqual(Object o1, Object o2) {
|
---|
112 | return (o1==null && o2==null) || (o1!=null && o1.equals(o2));
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Returns a deep copy of this object.
|
---|
118 | *
|
---|
119 | * @return The copy.
|
---|
120 | */
|
---|
121 | public Object clone() {
|
---|
122 | Style clone = null;
|
---|
123 | try {
|
---|
124 | clone = (Style)super.clone();
|
---|
125 | } catch (CloneNotSupportedException cnse) { // Never happens
|
---|
126 | cnse.printStackTrace();
|
---|
127 | return null;
|
---|
128 | }
|
---|
129 | clone.foreground = foreground;
|
---|
130 | clone.background = background;
|
---|
131 | clone.font = font;
|
---|
132 | clone.underline = underline;
|
---|
133 | clone.fontMetrics = fontMetrics;
|
---|
134 | return clone;
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Returns whether or not two syntax schemes are equal.
|
---|
140 | *
|
---|
141 | * @param o2 The object with which to compare this syntax scheme.
|
---|
142 | * @return Whether or not these two syntax schemes represent the same
|
---|
143 | * scheme.
|
---|
144 | */
|
---|
145 | public boolean equals(Object o2) {
|
---|
146 | if (o2 instanceof Style) {
|
---|
147 | Style ss2 = (Style)o2;
|
---|
148 | if (this.underline==ss2.underline &&
|
---|
149 | areEqual(foreground, ss2.foreground) &&
|
---|
150 | areEqual(background, ss2.background) &&
|
---|
151 | areEqual(font, ss2.font) &&
|
---|
152 | areEqual(fontMetrics, ss2.fontMetrics))
|
---|
153 | return true;
|
---|
154 | }
|
---|
155 | return false;
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Computes the hash code to use when adding this syntax scheme to
|
---|
161 | * hash tables.<p>
|
---|
162 | *
|
---|
163 | * This method is implemented, since {@link #equals(Object)} is implemented,
|
---|
164 | * to keep FindBugs happy.
|
---|
165 | *
|
---|
166 | * @return The hash code.
|
---|
167 | */
|
---|
168 | public int hashCode() {
|
---|
169 | int hashCode = underline ? 1 : 0;
|
---|
170 | if (foreground!=null) {
|
---|
171 | hashCode ^= foreground.hashCode();
|
---|
172 | }
|
---|
173 | if (background!=null) {
|
---|
174 | hashCode ^= background.hashCode();
|
---|
175 | }
|
---|
176 | return hashCode;
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Returns a string representation of this style.
|
---|
182 | *
|
---|
183 | * @return A string representation of this style.
|
---|
184 | */
|
---|
185 | public String toString() {
|
---|
186 | return "[Style: foreground: " + foreground +
|
---|
187 | ", background: " + background + ", underline: " +
|
---|
188 | underline + ", font: " + font + "]";
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | } |
---|