Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
624ce48e KL |
2 | * Jexer - Java Text User Interface |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
624ce48e | 5 | * |
a2018e99 | 6 | * Copyright (C) 2017 Kevin Lamonte |
624ce48e | 7 | * |
e16dda65 KL |
8 | * Permission is hereby granted, free of charge, to any person obtaining a |
9 | * copy of this software and associated documentation files (the "Software"), | |
10 | * to deal in the Software without restriction, including without limitation | |
11 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
12 | * and/or sell copies of the Software, and to permit persons to whom the | |
13 | * Software is furnished to do so, subject to the following conditions: | |
624ce48e | 14 | * |
e16dda65 KL |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
624ce48e | 17 | * |
e16dda65 KL |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
21 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
23 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
24 | * DEALINGS IN THE SOFTWARE. | |
7b5261bc KL |
25 | * |
26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
27 | * @version 1 | |
624ce48e KL |
28 | */ |
29 | package jexer.bits; | |
30 | ||
31 | /** | |
32 | * The attributes used by a Cell: color, bold, blink, etc. | |
33 | */ | |
34 | public class CellAttributes { | |
35 | ||
d36057df KL |
36 | // ------------------------------------------------------------------------ |
37 | // Variables -------------------------------------------------------------- | |
38 | // ------------------------------------------------------------------------ | |
39 | ||
624ce48e | 40 | /** |
7b5261bc KL |
41 | * Bold attribute. |
42 | */ | |
43 | private boolean bold; | |
44 | ||
d36057df KL |
45 | /** |
46 | * Blink attribute. | |
47 | */ | |
48 | private boolean blink; | |
49 | ||
50 | /** | |
51 | * Reverse attribute. | |
52 | */ | |
53 | private boolean reverse; | |
54 | ||
55 | /** | |
56 | * Underline attribute. | |
57 | */ | |
58 | private boolean underline; | |
59 | ||
60 | /** | |
61 | * Protected attribute. | |
62 | */ | |
63 | private boolean protect; | |
64 | ||
65 | /** | |
66 | * Foreground color. Color.WHITE, Color.RED, etc. | |
67 | */ | |
68 | private Color foreColor; | |
69 | ||
70 | /** | |
71 | * Background color. Color.WHITE, Color.RED, etc. | |
72 | */ | |
73 | private Color backColor; | |
74 | ||
75 | // ------------------------------------------------------------------------ | |
76 | // Constructors ----------------------------------------------------------- | |
77 | // ------------------------------------------------------------------------ | |
78 | ||
79 | /** | |
80 | * Public constructor sets default values of the cell to white-on-black, | |
81 | * no bold/blink/reverse/underline/protect. | |
82 | * | |
83 | * @see #reset() | |
84 | */ | |
85 | public CellAttributes() { | |
86 | reset(); | |
87 | } | |
88 | ||
89 | /** | |
90 | * Public constructor makes a copy from another instance. | |
91 | * | |
92 | * @param that another CellAttributes instance | |
93 | * @see #reset() | |
94 | */ | |
95 | public CellAttributes(final CellAttributes that) { | |
96 | setTo(that); | |
97 | } | |
98 | ||
99 | // ------------------------------------------------------------------------ | |
100 | // CellAttributes --------------------------------------------------------- | |
101 | // ------------------------------------------------------------------------ | |
102 | ||
7b5261bc KL |
103 | /** |
104 | * Getter for bold. | |
105 | * | |
106 | * @return bold value | |
107 | */ | |
7c870d89 | 108 | public final boolean isBold() { |
7b5261bc KL |
109 | return bold; |
110 | } | |
111 | ||
112 | /** | |
113 | * Setter for bold. | |
114 | * | |
115 | * @param bold new bold value | |
116 | */ | |
117 | public final void setBold(final boolean bold) { | |
118 | this.bold = bold; | |
119 | } | |
120 | ||
7b5261bc KL |
121 | /** |
122 | * Getter for blink. | |
123 | * | |
124 | * @return blink value | |
125 | */ | |
7c870d89 | 126 | public final boolean isBlink() { |
7b5261bc KL |
127 | return blink; |
128 | } | |
129 | ||
130 | /** | |
131 | * Setter for blink. | |
132 | * | |
133 | * @param blink new blink value | |
134 | */ | |
135 | public final void setBlink(final boolean blink) { | |
136 | this.blink = blink; | |
137 | } | |
138 | ||
7b5261bc KL |
139 | /** |
140 | * Getter for reverse. | |
141 | * | |
142 | * @return reverse value | |
143 | */ | |
7c870d89 | 144 | public final boolean isReverse() { |
7b5261bc KL |
145 | return reverse; |
146 | } | |
147 | ||
148 | /** | |
149 | * Setter for reverse. | |
150 | * | |
151 | * @param reverse new reverse value | |
152 | */ | |
153 | public final void setReverse(final boolean reverse) { | |
154 | this.reverse = reverse; | |
155 | } | |
156 | ||
624ce48e | 157 | /** |
7b5261bc KL |
158 | * Getter for underline. |
159 | * | |
160 | * @return underline value | |
624ce48e | 161 | */ |
7c870d89 | 162 | public final boolean isUnderline() { |
7b5261bc KL |
163 | return underline; |
164 | } | |
624ce48e KL |
165 | |
166 | /** | |
7b5261bc KL |
167 | * Setter for underline. |
168 | * | |
169 | * @param underline new underline value | |
624ce48e | 170 | */ |
7b5261bc KL |
171 | public final void setUnderline(final boolean underline) { |
172 | this.underline = underline; | |
173 | } | |
624ce48e | 174 | |
624ce48e | 175 | /** |
7b5261bc KL |
176 | * Getter for protect. |
177 | * | |
178 | * @return protect value | |
624ce48e | 179 | */ |
7c870d89 | 180 | public final boolean isProtect() { |
7b5261bc KL |
181 | return protect; |
182 | } | |
183 | ||
184 | /** | |
185 | * Setter for protect. | |
186 | * | |
187 | * @param protect new protect value | |
188 | */ | |
189 | public final void setProtect(final boolean protect) { | |
190 | this.protect = protect; | |
191 | } | |
624ce48e | 192 | |
7b5261bc KL |
193 | /** |
194 | * Getter for foreColor. | |
195 | * | |
196 | * @return foreColor value | |
197 | */ | |
198 | public final Color getForeColor() { | |
199 | return foreColor; | |
200 | } | |
201 | ||
202 | /** | |
203 | * Setter for foreColor. | |
204 | * | |
205 | * @param foreColor new foreColor value | |
206 | */ | |
207 | public final void setForeColor(final Color foreColor) { | |
208 | this.foreColor = foreColor; | |
209 | } | |
624ce48e | 210 | |
7b5261bc KL |
211 | /** |
212 | * Getter for backColor. | |
213 | * | |
214 | * @return backColor value | |
215 | */ | |
216 | public final Color getBackColor() { | |
217 | return backColor; | |
218 | } | |
219 | ||
220 | /** | |
221 | * Setter for backColor. | |
222 | * | |
223 | * @param backColor new backColor value | |
224 | */ | |
225 | public final void setBackColor(final Color backColor) { | |
226 | this.backColor = backColor; | |
227 | } | |
624ce48e KL |
228 | |
229 | /** | |
7b5261bc KL |
230 | * Set to default: white foreground on black background, no |
231 | * bold/underline/blink/rever/protect. | |
624ce48e KL |
232 | */ |
233 | public void reset() { | |
7b5261bc KL |
234 | bold = false; |
235 | blink = false; | |
236 | reverse = false; | |
237 | underline = false; | |
238 | protect = false; | |
239 | foreColor = Color.WHITE; | |
240 | backColor = Color.BLACK; | |
624ce48e | 241 | } |
15ea4d73 | 242 | |
624ce48e | 243 | /** |
7b5261bc KL |
244 | * Comparison check. All fields must match to return true. |
245 | * | |
246 | * @param rhs another CellAttributes instance | |
247 | * @return true if all fields are equal | |
624ce48e KL |
248 | */ |
249 | @Override | |
7b5261bc KL |
250 | public boolean equals(final Object rhs) { |
251 | if (!(rhs instanceof CellAttributes)) { | |
252 | return false; | |
253 | } | |
624ce48e | 254 | |
7b5261bc | 255 | CellAttributes that = (CellAttributes) rhs; |
6358f6e5 KL |
256 | return ((foreColor == that.foreColor) |
257 | && (backColor == that.backColor) | |
258 | && (bold == that.bold) | |
7b5261bc KL |
259 | && (reverse == that.reverse) |
260 | && (underline == that.underline) | |
6358f6e5 KL |
261 | && (blink == that.blink) |
262 | && (protect == that.protect)); | |
624ce48e KL |
263 | } |
264 | ||
e826b451 KL |
265 | /** |
266 | * Hashcode uses all fields in equals(). | |
267 | * | |
268 | * @return the hash | |
269 | */ | |
270 | @Override | |
271 | public int hashCode() { | |
272 | int A = 13; | |
273 | int B = 23; | |
274 | int hash = A; | |
275 | hash = (B * hash) + (bold ? 1 : 0); | |
276 | hash = (B * hash) + (blink ? 1 : 0); | |
277 | hash = (B * hash) + (underline ? 1 : 0); | |
278 | hash = (B * hash) + (reverse ? 1 : 0); | |
279 | hash = (B * hash) + (protect ? 1 : 0); | |
280 | hash = (B * hash) + foreColor.hashCode(); | |
281 | hash = (B * hash) + backColor.hashCode(); | |
282 | return hash; | |
283 | } | |
284 | ||
624ce48e | 285 | /** |
7b5261bc KL |
286 | * Set my field values to that's field. |
287 | * | |
288 | * @param rhs another CellAttributes instance | |
624ce48e | 289 | */ |
e826b451 | 290 | public void setTo(final Object rhs) { |
7b5261bc KL |
291 | CellAttributes that = (CellAttributes) rhs; |
292 | ||
293 | this.bold = that.bold; | |
294 | this.blink = that.blink; | |
295 | this.reverse = that.reverse; | |
296 | this.underline = that.underline; | |
297 | this.protect = that.protect; | |
298 | this.foreColor = that.foreColor; | |
299 | this.backColor = that.backColor; | |
300 | } | |
301 | ||
302 | /** | |
303 | * Make human-readable description of this CellAttributes. | |
304 | * | |
305 | * @return displayable String | |
624ce48e KL |
306 | */ |
307 | @Override | |
308 | public String toString() { | |
a0d734e6 KL |
309 | return String.format("%s%s%s on %s", (bold == true ? "bold " : ""), |
310 | (blink == true ? "blink " : ""), foreColor, backColor); | |
624ce48e KL |
311 | } |
312 | ||
313 | } |