| 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 | * This class represents a single text cell on the screen. |
| 33 | */ |
| 34 | public final class Cell extends CellAttributes { |
| 35 | |
| 36 | // ------------------------------------------------------------------------ |
| 37 | // Variables -------------------------------------------------------------- |
| 38 | // ------------------------------------------------------------------------ |
| 39 | |
| 40 | /** |
| 41 | * The character at this cell. |
| 42 | */ |
| 43 | private char ch; |
| 44 | |
| 45 | // ------------------------------------------------------------------------ |
| 46 | // Constructors ----------------------------------------------------------- |
| 47 | // ------------------------------------------------------------------------ |
| 48 | |
| 49 | /** |
| 50 | * Public constructor sets default values of the cell to blank. |
| 51 | * |
| 52 | * @see #isBlank() |
| 53 | * @see #reset() |
| 54 | */ |
| 55 | public Cell() { |
| 56 | reset(); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Public constructor sets the character. Attributes are the same as |
| 61 | * default. |
| 62 | * |
| 63 | * @param ch character to set to |
| 64 | * @see #reset() |
| 65 | */ |
| 66 | public Cell(final char ch) { |
| 67 | reset(); |
| 68 | this.ch = ch; |
| 69 | } |
| 70 | |
| 71 | // ------------------------------------------------------------------------ |
| 72 | // Cell ------------------------------------------------------------------- |
| 73 | // ------------------------------------------------------------------------ |
| 74 | |
| 75 | /** |
| 76 | * Getter for cell character. |
| 77 | * |
| 78 | * @return cell character |
| 79 | */ |
| 80 | public char getChar() { |
| 81 | return ch; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Setter for cell character. |
| 86 | * |
| 87 | * @param ch new cell character |
| 88 | */ |
| 89 | public void setChar(final char ch) { |
| 90 | this.ch = ch; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Reset this cell to a blank. |
| 95 | */ |
| 96 | @Override |
| 97 | public void reset() { |
| 98 | super.reset(); |
| 99 | ch = ' '; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Check to see if this cell has default attributes: white foreground, |
| 104 | * black background, no bold/blink/reverse/underline/protect, and a |
| 105 | * character value of ' ' (space). |
| 106 | * |
| 107 | * @return true if this cell has default attributes. |
| 108 | */ |
| 109 | public boolean isBlank() { |
| 110 | if ((getForeColor().equals(Color.WHITE)) |
| 111 | && (getBackColor().equals(Color.BLACK)) |
| 112 | && !isBold() |
| 113 | && !isBlink() |
| 114 | && !isReverse() |
| 115 | && !isUnderline() |
| 116 | && !isProtect() |
| 117 | && (ch == ' ') |
| 118 | ) { |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Comparison check. All fields must match to return true. |
| 127 | * |
| 128 | * @param rhs another Cell instance |
| 129 | * @return true if all fields are equal |
| 130 | */ |
| 131 | @Override |
| 132 | public boolean equals(final Object rhs) { |
| 133 | if (!(rhs instanceof Cell)) { |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | Cell that = (Cell) rhs; |
| 138 | |
| 139 | if (ch == that.ch) { |
| 140 | return super.equals(rhs); |
| 141 | } |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Hashcode uses all fields in equals(). |
| 147 | * |
| 148 | * @return the hash |
| 149 | */ |
| 150 | @Override |
| 151 | public int hashCode() { |
| 152 | int A = 13; |
| 153 | int B = 23; |
| 154 | int hash = A; |
| 155 | hash = (B * hash) + super.hashCode(); |
| 156 | hash = (B * hash) + (int)ch; |
| 157 | return hash; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Set my field values to that's field. |
| 162 | * |
| 163 | * @param rhs an instance of either Cell or CellAttributes |
| 164 | */ |
| 165 | @Override |
| 166 | public void setTo(final Object rhs) { |
| 167 | // Let this throw a ClassCastException |
| 168 | CellAttributes thatAttr = (CellAttributes) rhs; |
| 169 | super.setTo(thatAttr); |
| 170 | |
| 171 | if (rhs instanceof Cell) { |
| 172 | Cell that = (Cell) rhs; |
| 173 | this.ch = that.ch; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Set my field attr values to that's field. |
| 179 | * |
| 180 | * @param that a CellAttributes instance |
| 181 | */ |
| 182 | public void setAttr(final CellAttributes that) { |
| 183 | super.setTo(that); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Make human-readable description of this Cell. |
| 188 | * |
| 189 | * @return displayable String |
| 190 | */ |
| 191 | @Override |
| 192 | public String toString() { |
| 193 | return String.format("fore: %s back: %s bold: %s blink: %s ch %c", |
| 194 | getForeColor(), getBackColor(), isBold(), isBlink(), ch); |
| 195 | } |
| 196 | } |