Commit | Line | Data |
---|---|---|
12b55d76 KL |
1 | /* |
2 | * Jexer - Java Text User Interface | |
3 | * | |
4 | * The MIT License (MIT) | |
5 | * | |
6 | * Copyright (C) 2017 Kevin Lamonte | |
7 | * | |
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: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be included in | |
16 | * all copies or substantial portions of the Software. | |
17 | * | |
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. | |
25 | * | |
26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
27 | * @version 1 | |
28 | */ | |
29 | package jexer.teditor; | |
30 | ||
31 | import jexer.bits.CellAttributes; | |
32 | ||
33 | /** | |
34 | * A Word represents text that was entered by the user. It can be either | |
35 | * whitespace or non-whitespace. | |
36 | */ | |
37 | public class Word { | |
38 | ||
39 | /** | |
40 | * The color to render this word as on screen. | |
41 | */ | |
42 | private CellAttributes color = new CellAttributes(); | |
43 | ||
44 | /** | |
45 | * The actual text of this word. Average word length is 6 characters, | |
46 | * with a lot of shorter ones, so start with 3. | |
47 | */ | |
48 | private StringBuilder text = new StringBuilder(3); | |
49 | ||
50 | /** | |
51 | * Get the color used to display this word on screen. | |
52 | * | |
53 | * @return the color | |
54 | */ | |
55 | public CellAttributes getColor() { | |
56 | return new CellAttributes(color); | |
57 | } | |
58 | ||
59 | /** | |
60 | * Set the color used to display this word on screen. | |
61 | * | |
62 | * @param color the color | |
63 | */ | |
64 | public void setColor(final CellAttributes color) { | |
65 | color.setTo(color); | |
66 | } | |
67 | ||
68 | /** | |
69 | * Get the text to display. | |
70 | * | |
71 | * @return the text | |
72 | */ | |
73 | public String getText() { | |
74 | return text.toString(); | |
75 | } | |
76 | ||
77 | /** | |
78 | * Get the on-screen display length. | |
79 | * | |
80 | * @return the number of cells needed to display this word | |
81 | */ | |
82 | public int getDisplayLength() { | |
83 | // For now, just use the text length. In the future, this will be a | |
84 | // grapheme count. | |
85 | ||
86 | // TODO: figure out how to handle the tab character. Do we have a | |
87 | // global tab stops list and current word position? | |
88 | return text.length(); | |
89 | } | |
90 | ||
91 | /** | |
92 | * See if this is a whitespace word. Note that empty string is | |
93 | * considered whitespace. | |
94 | * | |
95 | * @return true if this word is whitespace | |
96 | */ | |
97 | public boolean isWhitespace() { | |
98 | if (text.length() == 0) { | |
99 | return true; | |
100 | } | |
101 | if (Character.isWhitespace(text.charAt(0))) { | |
102 | return true; | |
103 | } | |
104 | return false; | |
105 | } | |
106 | ||
107 | /** | |
108 | * Construct a word with one character. | |
109 | * | |
110 | * @param ch the first character of the word | |
111 | */ | |
112 | public Word(final char ch) { | |
113 | text.append(ch); | |
114 | } | |
115 | ||
116 | /** | |
117 | * Construct a word with an empty string. | |
118 | */ | |
119 | public Word() {} | |
120 | ||
121 | /** | |
122 | * Add a character to this word. If this is a whitespace character | |
123 | * adding to a non-whitespace word, create a new word and return that; | |
124 | * similarly if this a non-whitespace character adding to a whitespace | |
125 | * word, create a new word and return that. | |
126 | * | |
127 | * @param ch the new character to add | |
128 | * @return either this word (if it was added), or a new word that | |
129 | * contains ch | |
130 | */ | |
131 | public Word addChar(final char ch) { | |
132 | if (text.length() == 0) { | |
133 | text.append(ch); | |
134 | return this; | |
135 | } | |
136 | if (Character.isWhitespace(text.charAt(0)) | |
137 | && Character.isWhitespace(ch) | |
138 | ) { | |
139 | text.append(ch); | |
140 | return this; | |
141 | } | |
142 | if (!Character.isWhitespace(text.charAt(0)) | |
143 | && !Character.isWhitespace(ch) | |
144 | ) { | |
145 | text.append(ch); | |
146 | return this; | |
147 | } | |
148 | ||
149 | // We will be splitting here. | |
150 | Word newWord = new Word(ch); | |
151 | return newWord; | |
152 | } | |
153 | ||
154 | } |