Merge branch 'master' of https://github.com/klamonte/jexer
[nikiroo-utils.git] / src / jexer / bits / CellAttributes.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2017 Kevin Lamonte
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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.
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29 package jexer.bits;
30
31 /**
32 * The attributes used by a Cell: color, bold, blink, etc.
33 */
34 public class CellAttributes {
35
36 // ------------------------------------------------------------------------
37 // Variables --------------------------------------------------------------
38 // ------------------------------------------------------------------------
39
40 /**
41 * Bold attribute.
42 */
43 private boolean bold;
44
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
103 /**
104 * Getter for bold.
105 *
106 * @return bold value
107 */
108 public final boolean isBold() {
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
121 /**
122 * Getter for blink.
123 *
124 * @return blink value
125 */
126 public final boolean isBlink() {
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
139 /**
140 * Getter for reverse.
141 *
142 * @return reverse value
143 */
144 public final boolean isReverse() {
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
157 /**
158 * Getter for underline.
159 *
160 * @return underline value
161 */
162 public final boolean isUnderline() {
163 return underline;
164 }
165
166 /**
167 * Setter for underline.
168 *
169 * @param underline new underline value
170 */
171 public final void setUnderline(final boolean underline) {
172 this.underline = underline;
173 }
174
175 /**
176 * Getter for protect.
177 *
178 * @return protect value
179 */
180 public final boolean isProtect() {
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 }
192
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 }
210
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 }
228
229 /**
230 * Set to default: white foreground on black background, no
231 * bold/underline/blink/rever/protect.
232 */
233 public void reset() {
234 bold = false;
235 blink = false;
236 reverse = false;
237 underline = false;
238 protect = false;
239 foreColor = Color.WHITE;
240 backColor = Color.BLACK;
241 }
242
243 /**
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
248 */
249 @Override
250 public boolean equals(final Object rhs) {
251 if (!(rhs instanceof CellAttributes)) {
252 return false;
253 }
254
255 CellAttributes that = (CellAttributes) rhs;
256 return ((foreColor == that.foreColor)
257 && (backColor == that.backColor)
258 && (bold == that.bold)
259 && (reverse == that.reverse)
260 && (underline == that.underline)
261 && (blink == that.blink)
262 && (protect == that.protect));
263 }
264
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
285 /**
286 * Set my field values to that's field.
287 *
288 * @param rhs another CellAttributes instance
289 */
290 public void setTo(final Object rhs) {
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
306 */
307 @Override
308 public String toString() {
309 return String.format("%s%s%s on %s", (bold == true ? "bold " : ""),
310 (blink == true ? "blink " : ""), foreColor, backColor);
311 }
312
313 }