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