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