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 KL |
35 | */ |
36 | public class Cell extends CellAttributes { | |
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 | */ | |
48 | public final char getChar() { | |
49 | return ch; | |
50 | } | |
51 | ||
52 | /** | |
53 | * Setter for cell character. | |
54 | * | |
55 | * @param ch new cell character | |
624ce48e | 56 | */ |
7b5261bc KL |
57 | public final void setChar(final char ch) { |
58 | this.ch = ch; | |
59 | } | |
624ce48e KL |
60 | |
61 | /** | |
7b5261bc | 62 | * Reset this cell to a blank. |
624ce48e KL |
63 | */ |
64 | @Override | |
7b5261bc KL |
65 | public final void reset() { |
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 | */ |
7b5261bc KL |
77 | public final 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 | } | |
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 | |
7b5261bc KL |
100 | public final boolean equals(final Object rhs) { |
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 | ||
110 | /** | |
7b5261bc KL |
111 | * Set my field values to that's field. |
112 | * | |
113 | * @param rhs an instance of either Cell or CellAttributes | |
624ce48e KL |
114 | */ |
115 | @Override | |
7b5261bc KL |
116 | public final void setTo(final Object rhs) { |
117 | // Let this throw a ClassCastException | |
118 | CellAttributes thatAttr = (CellAttributes) rhs; | |
119 | super.setTo(thatAttr); | |
624ce48e | 120 | |
7b5261bc KL |
121 | if (rhs instanceof Cell) { |
122 | Cell that = (Cell) rhs; | |
123 | this.ch = that.ch; | |
124 | } | |
624ce48e KL |
125 | } |
126 | ||
127 | /** | |
7b5261bc KL |
128 | * Set my field attr values to that's field. |
129 | * | |
130 | * @param that a CellAttributes instance | |
624ce48e | 131 | */ |
7b5261bc KL |
132 | public final void setAttr(final CellAttributes that) { |
133 | super.setTo(that); | |
624ce48e KL |
134 | } |
135 | ||
136 | /** | |
7b5261bc KL |
137 | * Public constructor sets default values of the cell to blank. |
138 | * | |
139 | * @see #isBlank() | |
140 | * @see #reset() | |
624ce48e KL |
141 | */ |
142 | public Cell() { | |
7b5261bc | 143 | reset(); |
624ce48e KL |
144 | } |
145 | ||
146 | /** | |
7b5261bc KL |
147 | * Public constructor sets the character. Attributes are the same as |
148 | * default. | |
624ce48e KL |
149 | * |
150 | * @param ch character to set to | |
7b5261bc | 151 | * @see #reset() |
624ce48e | 152 | */ |
7b5261bc KL |
153 | public Cell(final char ch) { |
154 | reset(); | |
155 | this.ch = ch; | |
624ce48e KL |
156 | } |
157 | ||
158 | /** | |
7b5261bc KL |
159 | * Make human-readable description of this Cell. |
160 | * | |
161 | * @return displayable String | |
624ce48e KL |
162 | */ |
163 | @Override | |
7b5261bc KL |
164 | public final String toString() { |
165 | return String.format("fore: %d back: %d bold: %s blink: %s ch %c", | |
166 | getForeColor(), getBackColor(), getBold(), getBlink(), ch); | |
624ce48e KL |
167 | } |
168 | } |