Commit | Line | Data |
---|---|---|
624ce48e KL |
1 | /** |
2 | * Jexer - Java Text User Interface | |
3 | * | |
624ce48e KL |
4 | * License: LGPLv3 or later |
5 | * | |
7b5261bc KL |
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. | |
624ce48e KL |
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 | |
7b5261bc KL |
27 | * |
28 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
29 | * @version 1 | |
624ce48e KL |
30 | */ |
31 | package jexer.bits; | |
32 | ||
33 | /** | |
7b5261bc | 34 | * This class represents a single text cell on the screen. |
624ce48e | 35 | */ |
2b9c27db | 36 | public final class Cell extends CellAttributes { |
624ce48e KL |
37 | |
38 | /** | |
7b5261bc KL |
39 | * The character at this cell. |
40 | */ | |
41 | private char ch; | |
42 | ||
43 | /** | |
44 | * Getter for cell character. | |
45 | * | |
46 | * @return cell character | |
47 | */ | |
2b9c27db | 48 | public char getChar() { |
7b5261bc KL |
49 | return ch; |
50 | } | |
51 | ||
52 | /** | |
53 | * Setter for cell character. | |
54 | * | |
55 | * @param ch new cell character | |
624ce48e | 56 | */ |
2b9c27db | 57 | public void setChar(final char ch) { |
7b5261bc KL |
58 | this.ch = ch; |
59 | } | |
624ce48e KL |
60 | |
61 | /** | |
7b5261bc | 62 | * Reset this cell to a blank. |
624ce48e KL |
63 | */ |
64 | @Override | |
2b9c27db | 65 | public void reset() { |
7b5261bc KL |
66 | super.reset(); |
67 | ch = ' '; | |
624ce48e KL |
68 | } |
69 | ||
70 | /** | |
7b5261bc KL |
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. | |
624ce48e | 76 | */ |
2b9c27db | 77 | public boolean isBlank() { |
7b5261bc KL |
78 | if ((getForeColor().equals(Color.WHITE)) |
79 | && (getBackColor().equals(Color.BLACK)) | |
7c870d89 KL |
80 | && !isBold() |
81 | && !isBlink() | |
82 | && !isReverse() | |
83 | && !isUnderline() | |
84 | && !isProtect() | |
7b5261bc KL |
85 | && (ch == ' ') |
86 | ) { | |
87 | return true; | |
88 | } | |
624ce48e | 89 | |
7b5261bc | 90 | return false; |
624ce48e KL |
91 | } |
92 | ||
93 | /** | |
7b5261bc KL |
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 | |
624ce48e KL |
98 | */ |
99 | @Override | |
2b9c27db | 100 | public boolean equals(final Object rhs) { |
7b5261bc KL |
101 | if (!(rhs instanceof Cell)) { |
102 | return false; | |
103 | } | |
624ce48e | 104 | |
7b5261bc KL |
105 | Cell that = (Cell) rhs; |
106 | return (super.equals(rhs) | |
107 | && (ch == that.ch)); | |
624ce48e KL |
108 | } |
109 | ||
e826b451 KL |
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 | ||
624ce48e | 125 | /** |
7b5261bc KL |
126 | * Set my field values to that's field. |
127 | * | |
128 | * @param rhs an instance of either Cell or CellAttributes | |
624ce48e KL |
129 | */ |
130 | @Override | |
2b9c27db | 131 | public void setTo(final Object rhs) { |
7b5261bc KL |
132 | // Let this throw a ClassCastException |
133 | CellAttributes thatAttr = (CellAttributes) rhs; | |
134 | super.setTo(thatAttr); | |
624ce48e | 135 | |
7b5261bc KL |
136 | if (rhs instanceof Cell) { |
137 | Cell that = (Cell) rhs; | |
138 | this.ch = that.ch; | |
139 | } | |
624ce48e KL |
140 | } |
141 | ||
142 | /** | |
7b5261bc KL |
143 | * Set my field attr values to that's field. |
144 | * | |
145 | * @param that a CellAttributes instance | |
624ce48e | 146 | */ |
2b9c27db | 147 | public void setAttr(final CellAttributes that) { |
7b5261bc | 148 | super.setTo(that); |
624ce48e KL |
149 | } |
150 | ||
151 | /** | |
7b5261bc KL |
152 | * Public constructor sets default values of the cell to blank. |
153 | * | |
154 | * @see #isBlank() | |
155 | * @see #reset() | |
624ce48e KL |
156 | */ |
157 | public Cell() { | |
7b5261bc | 158 | reset(); |
624ce48e KL |
159 | } |
160 | ||
161 | /** | |
7b5261bc KL |
162 | * Public constructor sets the character. Attributes are the same as |
163 | * default. | |
624ce48e KL |
164 | * |
165 | * @param ch character to set to | |
7b5261bc | 166 | * @see #reset() |
624ce48e | 167 | */ |
7b5261bc KL |
168 | public Cell(final char ch) { |
169 | reset(); | |
170 | this.ch = ch; | |
624ce48e KL |
171 | } |
172 | ||
173 | /** | |
7b5261bc KL |
174 | * Make human-readable description of this Cell. |
175 | * | |
176 | * @return displayable String | |
624ce48e KL |
177 | */ |
178 | @Override | |
2b9c27db | 179 | public String toString() { |
7b5261bc | 180 | return String.format("fore: %d back: %d bold: %s blink: %s ch %c", |
7c870d89 | 181 | getForeColor(), getBackColor(), isBold(), isBlink(), ch); |
624ce48e KL |
182 | } |
183 | } |