Not dead note
[fanfix.git] / src / jexer / bits / Cell.java
CommitLineData
daa4106c 1/*
624ce48e
KL
2 * Jexer - Java Text User Interface
3 *
e16dda65 4 * The MIT License (MIT)
624ce48e 5 *
a2018e99 6 * Copyright (C) 2017 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/**
7b5261bc 32 * This class represents a single text cell on the screen.
624ce48e 33 */
2b9c27db 34public final class Cell extends CellAttributes {
624ce48e 35
d36057df
KL
36 // ------------------------------------------------------------------------
37 // Variables --------------------------------------------------------------
38 // ------------------------------------------------------------------------
39
624ce48e 40 /**
7b5261bc
KL
41 * The character at this cell.
42 */
43 private char ch;
44
d36057df
KL
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
7b5261bc
KL
75 /**
76 * Getter for cell character.
77 *
78 * @return cell character
79 */
2b9c27db 80 public char getChar() {
7b5261bc
KL
81 return ch;
82 }
83
84 /**
85 * Setter for cell character.
86 *
87 * @param ch new cell character
624ce48e 88 */
2b9c27db 89 public void setChar(final char ch) {
7b5261bc
KL
90 this.ch = ch;
91 }
624ce48e
KL
92
93 /**
7b5261bc 94 * Reset this cell to a blank.
624ce48e
KL
95 */
96 @Override
2b9c27db 97 public void reset() {
7b5261bc
KL
98 super.reset();
99 ch = ' ';
624ce48e
KL
100 }
101
102 /**
7b5261bc
KL
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.
624ce48e 108 */
2b9c27db 109 public boolean isBlank() {
7b5261bc
KL
110 if ((getForeColor().equals(Color.WHITE))
111 && (getBackColor().equals(Color.BLACK))
7c870d89
KL
112 && !isBold()
113 && !isBlink()
114 && !isReverse()
115 && !isUnderline()
116 && !isProtect()
051e2913 117 && !isRGB()
7b5261bc
KL
118 && (ch == ' ')
119 ) {
120 return true;
121 }
624ce48e 122
7b5261bc 123 return false;
624ce48e
KL
124 }
125
126 /**
7b5261bc
KL
127 * Comparison check. All fields must match to return true.
128 *
129 * @param rhs another Cell instance
130 * @return true if all fields are equal
624ce48e
KL
131 */
132 @Override
2b9c27db 133 public boolean equals(final Object rhs) {
7b5261bc
KL
134 if (!(rhs instanceof Cell)) {
135 return false;
136 }
624ce48e 137
7b5261bc 138 Cell that = (Cell) rhs;
6358f6e5
KL
139
140 if (ch == that.ch) {
141 return super.equals(rhs);
142 }
143 return false;
624ce48e
KL
144 }
145
e826b451
KL
146 /**
147 * Hashcode uses all fields in equals().
148 *
149 * @return the hash
150 */
151 @Override
152 public int hashCode() {
153 int A = 13;
154 int B = 23;
155 int hash = A;
156 hash = (B * hash) + super.hashCode();
157 hash = (B * hash) + (int)ch;
158 return hash;
159 }
160
624ce48e 161 /**
7b5261bc
KL
162 * Set my field values to that's field.
163 *
164 * @param rhs an instance of either Cell or CellAttributes
624ce48e
KL
165 */
166 @Override
2b9c27db 167 public void setTo(final Object rhs) {
7b5261bc
KL
168 // Let this throw a ClassCastException
169 CellAttributes thatAttr = (CellAttributes) rhs;
170 super.setTo(thatAttr);
624ce48e 171
7b5261bc
KL
172 if (rhs instanceof Cell) {
173 Cell that = (Cell) rhs;
174 this.ch = that.ch;
175 }
624ce48e
KL
176 }
177
178 /**
7b5261bc
KL
179 * Set my field attr values to that's field.
180 *
181 * @param that a CellAttributes instance
624ce48e 182 */
2b9c27db 183 public void setAttr(final CellAttributes that) {
7b5261bc 184 super.setTo(that);
624ce48e
KL
185 }
186
624ce48e 187 /**
7b5261bc
KL
188 * Make human-readable description of this Cell.
189 *
190 * @return displayable String
624ce48e
KL
191 */
192 @Override
2b9c27db 193 public String toString() {
8a632d71 194 return String.format("fore: %s back: %s bold: %s blink: %s ch %c",
7c870d89 195 getForeColor(), getBackColor(), isBold(), isBlink(), ch);
624ce48e
KL
196 }
197}