| 1 | /* |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (C) 2016 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 | * Bold attribute. |
| 38 | */ |
| 39 | private boolean bold; |
| 40 | |
| 41 | /** |
| 42 | * Getter for bold. |
| 43 | * |
| 44 | * @return bold value |
| 45 | */ |
| 46 | public final boolean isBold() { |
| 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 | */ |
| 69 | public final boolean isBlink() { |
| 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 | */ |
| 92 | public final boolean isReverse() { |
| 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. |
| 107 | */ |
| 108 | private boolean underline; |
| 109 | |
| 110 | /** |
| 111 | * Getter for underline. |
| 112 | * |
| 113 | * @return underline value |
| 114 | */ |
| 115 | public final boolean isUnderline() { |
| 116 | return underline; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Setter for underline. |
| 121 | * |
| 122 | * @param underline new underline value |
| 123 | */ |
| 124 | public final void setUnderline(final boolean underline) { |
| 125 | this.underline = underline; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Protected attribute. |
| 130 | */ |
| 131 | private boolean protect; |
| 132 | |
| 133 | /** |
| 134 | * Getter for protect. |
| 135 | * |
| 136 | * @return protect value |
| 137 | */ |
| 138 | public final boolean isProtect() { |
| 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 | } |
| 150 | |
| 151 | /** |
| 152 | * Foreground color. Color.WHITE, Color.RED, etc. |
| 153 | */ |
| 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 | } |
| 173 | |
| 174 | /** |
| 175 | * Background color. Color.WHITE, Color.RED, etc. |
| 176 | */ |
| 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 | } |
| 196 | |
| 197 | /** |
| 198 | * Set to default: white foreground on black background, no |
| 199 | * bold/underline/blink/rever/protect. |
| 200 | */ |
| 201 | public void reset() { |
| 202 | bold = false; |
| 203 | blink = false; |
| 204 | reverse = false; |
| 205 | underline = false; |
| 206 | protect = false; |
| 207 | foreColor = Color.WHITE; |
| 208 | backColor = Color.BLACK; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Public constructor sets default values of the cell to white-on-black, |
| 213 | * no bold/blink/reverse/underline/protect. |
| 214 | * |
| 215 | * @see #reset() |
| 216 | */ |
| 217 | public CellAttributes() { |
| 218 | reset(); |
| 219 | } |
| 220 | |
| 221 | /** |
| 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 |
| 226 | */ |
| 227 | @Override |
| 228 | public boolean equals(final Object rhs) { |
| 229 | if (!(rhs instanceof CellAttributes)) { |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | CellAttributes that = (CellAttributes) rhs; |
| 234 | return ((foreColor == that.foreColor) |
| 235 | && (backColor == that.backColor) |
| 236 | && (bold == that.bold) |
| 237 | && (reverse == that.reverse) |
| 238 | && (underline == that.underline) |
| 239 | && (blink == that.blink) |
| 240 | && (protect == that.protect)); |
| 241 | } |
| 242 | |
| 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 | |
| 263 | /** |
| 264 | * Set my field values to that's field. |
| 265 | * |
| 266 | * @param rhs another CellAttributes instance |
| 267 | */ |
| 268 | public void setTo(final Object rhs) { |
| 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 |
| 284 | */ |
| 285 | @Override |
| 286 | public String toString() { |
| 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 | } |
| 293 | } |
| 294 | |
| 295 | } |