misc cleanup
[nikiroo-utils.git] / src / jexer / bits / Cell.java
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 * Set my field values to that's field.
112 *
113 * @param rhs an instance of either Cell or CellAttributes
114 */
115 @Override
116 public void setTo(final Object rhs) {
117 // Let this throw a ClassCastException
118 CellAttributes thatAttr = (CellAttributes) rhs;
119 super.setTo(thatAttr);
120
121 if (rhs instanceof Cell) {
122 Cell that = (Cell) rhs;
123 this.ch = that.ch;
124 }
125 }
126
127 /**
128 * Set my field attr values to that's field.
129 *
130 * @param that a CellAttributes instance
131 */
132 public void setAttr(final CellAttributes that) {
133 super.setTo(that);
134 }
135
136 /**
137 * Public constructor sets default values of the cell to blank.
138 *
139 * @see #isBlank()
140 * @see #reset()
141 */
142 public Cell() {
143 reset();
144 }
145
146 /**
147 * Public constructor sets the character. Attributes are the same as
148 * default.
149 *
150 * @param ch character to set to
151 * @see #reset()
152 */
153 public Cell(final char ch) {
154 reset();
155 this.ch = ch;
156 }
157
158 /**
159 * Make human-readable description of this Cell.
160 *
161 * @return displayable String
162 */
163 @Override
164 public String toString() {
165 return String.format("fore: %d back: %d bold: %s blink: %s ch %c",
166 getForeColor(), getBackColor(), getBold(), getBlink(), ch);
167 }
168 }