0ea0d88579929249825a8d9b13cd21c31358924a
[nikiroo-utils.git] / src / jexer / bits / CellAttributes.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * License: LGPLv3 or later
5 *
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
9 *
10 * Copyright (C) 2015 Kevin Lamonte
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 * 02110-1301 USA
27 *
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 * @version 1
30 */
31 package jexer.bits;
32
33 /**
34 * The attributes used by a Cell: color, bold, blink, etc.
35 */
36 public class CellAttributes {
37
38 /**
39 * Bold attribute.
40 */
41 private boolean bold;
42
43 /**
44 * Getter for bold.
45 *
46 * @return bold value
47 */
48 public final boolean isBold() {
49 return bold;
50 }
51
52 /**
53 * Setter for bold.
54 *
55 * @param bold new bold value
56 */
57 public final void setBold(final boolean bold) {
58 this.bold = bold;
59 }
60
61 /**
62 * Blink attribute.
63 */
64 private boolean blink;
65
66 /**
67 * Getter for blink.
68 *
69 * @return blink value
70 */
71 public final boolean isBlink() {
72 return blink;
73 }
74
75 /**
76 * Setter for blink.
77 *
78 * @param blink new blink value
79 */
80 public final void setBlink(final boolean blink) {
81 this.blink = blink;
82 }
83
84 /**
85 * Reverse attribute.
86 */
87 private boolean reverse;
88
89 /**
90 * Getter for reverse.
91 *
92 * @return reverse value
93 */
94 public final boolean isReverse() {
95 return reverse;
96 }
97
98 /**
99 * Setter for reverse.
100 *
101 * @param reverse new reverse value
102 */
103 public final void setReverse(final boolean reverse) {
104 this.reverse = reverse;
105 }
106
107 /**
108 * Underline attribute.
109 */
110 private boolean underline;
111
112 /**
113 * Getter for underline.
114 *
115 * @return underline value
116 */
117 public final boolean isUnderline() {
118 return underline;
119 }
120
121 /**
122 * Setter for underline.
123 *
124 * @param underline new underline value
125 */
126 public final void setUnderline(final boolean underline) {
127 this.underline = underline;
128 }
129
130 /**
131 * Protected attribute.
132 */
133 private boolean protect;
134
135 /**
136 * Getter for protect.
137 *
138 * @return protect value
139 */
140 public final boolean isProtect() {
141 return protect;
142 }
143
144 /**
145 * Setter for protect.
146 *
147 * @param protect new protect value
148 */
149 public final void setProtect(final boolean protect) {
150 this.protect = protect;
151 }
152
153 /**
154 * Foreground color. Color.WHITE, Color.RED, etc.
155 */
156 private Color foreColor;
157
158 /**
159 * Getter for foreColor.
160 *
161 * @return foreColor value
162 */
163 public final Color getForeColor() {
164 return foreColor;
165 }
166
167 /**
168 * Setter for foreColor.
169 *
170 * @param foreColor new foreColor value
171 */
172 public final void setForeColor(final Color foreColor) {
173 this.foreColor = foreColor;
174 }
175
176 /**
177 * Background color. Color.WHITE, Color.RED, etc.
178 */
179 private Color backColor;
180
181 /**
182 * Getter for backColor.
183 *
184 * @return backColor value
185 */
186 public final Color getBackColor() {
187 return backColor;
188 }
189
190 /**
191 * Setter for backColor.
192 *
193 * @param backColor new backColor value
194 */
195 public final void setBackColor(final Color backColor) {
196 this.backColor = backColor;
197 }
198
199 /**
200 * Set to default: white foreground on black background, no
201 * bold/underline/blink/rever/protect.
202 */
203 public void reset() {
204 bold = false;
205 blink = false;
206 reverse = false;
207 underline = false;
208 protect = false;
209 foreColor = Color.WHITE;
210 backColor = Color.BLACK;
211 }
212
213 /**
214 * Public constructor sets default values of the cell to white-on-black,
215 * no bold/blink/reverse/underline/protect.
216 *
217 * @see #reset()
218 */
219 public CellAttributes() {
220 reset();
221 }
222
223 /**
224 * Comparison check. All fields must match to return true.
225 *
226 * @param rhs another CellAttributes instance
227 * @return true if all fields are equal
228 */
229 @Override
230 public boolean equals(final Object rhs) {
231 if (!(rhs instanceof CellAttributes)) {
232 return false;
233 }
234
235 CellAttributes that = (CellAttributes) rhs;
236 return ((bold == that.bold)
237 && (blink == that.blink)
238 && (reverse == that.reverse)
239 && (underline == that.underline)
240 && (protect == that.protect)
241 && (foreColor == that.foreColor)
242 && (backColor == that.backColor));
243 }
244
245 /**
246 * Hashcode uses all fields in equals().
247 *
248 * @return the hash
249 */
250 @Override
251 public int hashCode() {
252 int A = 13;
253 int B = 23;
254 int hash = A;
255 hash = (B * hash) + (bold ? 1 : 0);
256 hash = (B * hash) + (blink ? 1 : 0);
257 hash = (B * hash) + (underline ? 1 : 0);
258 hash = (B * hash) + (reverse ? 1 : 0);
259 hash = (B * hash) + (protect ? 1 : 0);
260 hash = (B * hash) + foreColor.hashCode();
261 hash = (B * hash) + backColor.hashCode();
262 return hash;
263 }
264
265 /**
266 * Set my field values to that's field.
267 *
268 * @param rhs another CellAttributes instance
269 */
270 public void setTo(final Object rhs) {
271 CellAttributes that = (CellAttributes) rhs;
272
273 this.bold = that.bold;
274 this.blink = that.blink;
275 this.reverse = that.reverse;
276 this.underline = that.underline;
277 this.protect = that.protect;
278 this.foreColor = that.foreColor;
279 this.backColor = that.backColor;
280 }
281
282 /**
283 * Make human-readable description of this CellAttributes.
284 *
285 * @return displayable String
286 */
287 @Override
288 public String toString() {
289 if (bold) {
290 return String.format("bold %s on %s",
291 foreColor, backColor);
292 } else {
293 return String.format("%s on %s", foreColor, backColor);
294 }
295 }
296
297 }