LICENSE CHANGED TO MIT
[nikiroo-utils.git] / src / jexer / bits / CellAttributes.java
CommitLineData
daa4106c 1/*
624ce48e
KL
2 * Jexer - Java Text User Interface
3 *
e16dda65 4 * The MIT License (MIT)
624ce48e 5 *
e16dda65 6 * Copyright (C) 2016 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 */
29package jexer.bits;
30
31/**
32 * The attributes used by a Cell: color, bold, blink, etc.
33 */
34public class CellAttributes {
35
36 /**
7b5261bc
KL
37 * Bold attribute.
38 */
39 private boolean bold;
40
41 /**
42 * Getter for bold.
43 *
44 * @return bold value
45 */
7c870d89 46 public final boolean isBold() {
7b5261bc
KL
47 return bold;
48 }
49
50 /**
51 * Setter for bold.
52 *
53 * @param bold new bold value
54 */
55 public final void setBold(final boolean bold) {
56 this.bold = bold;
57 }
58
59 /**
60 * Blink attribute.
61 */
62 private boolean blink;
63
64 /**
65 * Getter for blink.
66 *
67 * @return blink value
68 */
7c870d89 69 public final boolean isBlink() {
7b5261bc
KL
70 return blink;
71 }
72
73 /**
74 * Setter for blink.
75 *
76 * @param blink new blink value
77 */
78 public final void setBlink(final boolean blink) {
79 this.blink = blink;
80 }
81
82 /**
83 * Reverse attribute.
84 */
85 private boolean reverse;
86
87 /**
88 * Getter for reverse.
89 *
90 * @return reverse value
91 */
7c870d89 92 public final boolean isReverse() {
7b5261bc
KL
93 return reverse;
94 }
95
96 /**
97 * Setter for reverse.
98 *
99 * @param reverse new reverse value
100 */
101 public final void setReverse(final boolean reverse) {
102 this.reverse = reverse;
103 }
104
105 /**
106 * Underline attribute.
624ce48e 107 */
7b5261bc 108 private boolean underline;
624ce48e
KL
109
110 /**
7b5261bc
KL
111 * Getter for underline.
112 *
113 * @return underline value
624ce48e 114 */
7c870d89 115 public final boolean isUnderline() {
7b5261bc
KL
116 return underline;
117 }
624ce48e
KL
118
119 /**
7b5261bc
KL
120 * Setter for underline.
121 *
122 * @param underline new underline value
624ce48e 123 */
7b5261bc
KL
124 public final void setUnderline(final boolean underline) {
125 this.underline = underline;
126 }
624ce48e
KL
127
128 /**
7b5261bc 129 * Protected attribute.
624ce48e 130 */
7b5261bc 131 private boolean protect;
624ce48e
KL
132
133 /**
7b5261bc
KL
134 * Getter for protect.
135 *
136 * @return protect value
624ce48e 137 */
7c870d89 138 public final boolean isProtect() {
7b5261bc
KL
139 return protect;
140 }
141
142 /**
143 * Setter for protect.
144 *
145 * @param protect new protect value
146 */
147 public final void setProtect(final boolean protect) {
148 this.protect = protect;
149 }
624ce48e
KL
150
151 /**
152 * Foreground color. Color.WHITE, Color.RED, etc.
153 */
7b5261bc
KL
154 private Color foreColor;
155
156 /**
157 * Getter for foreColor.
158 *
159 * @return foreColor value
160 */
161 public final Color getForeColor() {
162 return foreColor;
163 }
164
165 /**
166 * Setter for foreColor.
167 *
168 * @param foreColor new foreColor value
169 */
170 public final void setForeColor(final Color foreColor) {
171 this.foreColor = foreColor;
172 }
624ce48e
KL
173
174 /**
175 * Background color. Color.WHITE, Color.RED, etc.
176 */
7b5261bc
KL
177 private Color backColor;
178
179 /**
180 * Getter for backColor.
181 *
182 * @return backColor value
183 */
184 public final Color getBackColor() {
185 return backColor;
186 }
187
188 /**
189 * Setter for backColor.
190 *
191 * @param backColor new backColor value
192 */
193 public final void setBackColor(final Color backColor) {
194 this.backColor = backColor;
195 }
624ce48e
KL
196
197 /**
7b5261bc
KL
198 * Set to default: white foreground on black background, no
199 * bold/underline/blink/rever/protect.
624ce48e
KL
200 */
201 public void reset() {
7b5261bc
KL
202 bold = false;
203 blink = false;
204 reverse = false;
205 underline = false;
206 protect = false;
207 foreColor = Color.WHITE;
208 backColor = Color.BLACK;
624ce48e
KL
209 }
210
211 /**
7b5261bc
KL
212 * Public constructor sets default values of the cell to white-on-black,
213 * no bold/blink/reverse/underline/protect.
214 *
215 * @see #reset()
624ce48e
KL
216 */
217 public CellAttributes() {
7b5261bc 218 reset();
624ce48e
KL
219 }
220
221 /**
7b5261bc
KL
222 * Comparison check. All fields must match to return true.
223 *
224 * @param rhs another CellAttributes instance
225 * @return true if all fields are equal
624ce48e
KL
226 */
227 @Override
7b5261bc
KL
228 public boolean equals(final Object rhs) {
229 if (!(rhs instanceof CellAttributes)) {
230 return false;
231 }
624ce48e 232
7b5261bc
KL
233 CellAttributes that = (CellAttributes) rhs;
234 return ((bold == that.bold)
235 && (blink == that.blink)
236 && (reverse == that.reverse)
237 && (underline == that.underline)
238 && (protect == that.protect)
239 && (foreColor == that.foreColor)
240 && (backColor == that.backColor));
624ce48e
KL
241 }
242
e826b451
KL
243 /**
244 * Hashcode uses all fields in equals().
245 *
246 * @return the hash
247 */
248 @Override
249 public int hashCode() {
250 int A = 13;
251 int B = 23;
252 int hash = A;
253 hash = (B * hash) + (bold ? 1 : 0);
254 hash = (B * hash) + (blink ? 1 : 0);
255 hash = (B * hash) + (underline ? 1 : 0);
256 hash = (B * hash) + (reverse ? 1 : 0);
257 hash = (B * hash) + (protect ? 1 : 0);
258 hash = (B * hash) + foreColor.hashCode();
259 hash = (B * hash) + backColor.hashCode();
260 return hash;
261 }
262
624ce48e 263 /**
7b5261bc
KL
264 * Set my field values to that's field.
265 *
266 * @param rhs another CellAttributes instance
624ce48e 267 */
e826b451 268 public void setTo(final Object rhs) {
7b5261bc
KL
269 CellAttributes that = (CellAttributes) rhs;
270
271 this.bold = that.bold;
272 this.blink = that.blink;
273 this.reverse = that.reverse;
274 this.underline = that.underline;
275 this.protect = that.protect;
276 this.foreColor = that.foreColor;
277 this.backColor = that.backColor;
278 }
279
280 /**
281 * Make human-readable description of this CellAttributes.
282 *
283 * @return displayable String
624ce48e
KL
284 */
285 @Override
286 public String toString() {
7b5261bc
KL
287 if (bold) {
288 return String.format("bold %s on %s",
289 foreColor, backColor);
290 } else {
291 return String.format("%s on %s", foreColor, backColor);
292 }
624ce48e
KL
293 }
294
295}