1 package org.catacomb.druid.xtext.base;
2
3
4
5 import org.catacomb.druid.xtext.StandardGuises;
6 import org.catacomb.druid.xtext.data.XType;
7 import org.catacomb.interlish.content.BasicTouchTime;
8 import org.catacomb.report.E;
9
10 public abstract class TextBlock extends Block {
11
12
13 public String text;
14 int nchar;
15
16 int[] cachedLimits;
17
18 XType type;
19
20 BasicTouchTime cacheTime;
21
22
23
24
25
26
27 public TextBlock() {
28 super();
29
30 cachedLimits = new int[4];
31 cacheTime = new BasicTouchTime();
32 }
33
34
35 public TextBlock(String s) {
36 this();
37 text = s;
38 }
39
40
41 public int[] getCachedPosition() {
42 return cachedLimits;
43 }
44
45
46 public void setCachedPosition(int x, int y, int w, int h) {
47 cachedLimits[0] = x;
48 cachedLimits[1] = y;
49 cachedLimits[2] = w;
50 cachedLimits[3] = h;
51 }
52
53
54 public void setText(String s) {
55 text = s;
56 nchar = s.length();
57 }
58
59
60 public int textLength() {
61 return nchar;
62 }
63
64
65 public String getText() {
66 return text;
67 }
68
69 public String getExtendedText() {
70 return text;
71 }
72
73 boolean isText() {
74 return (text != null);
75 }
76
77
78
79
80
81 public TextBlock nextTextBlock() {
82 TextBlock ret = null;
83 Block b = this;
84 while (b.hasNext()) {
85 b = b.next();
86 if (b instanceof TextBlock) {
87 ret = (TextBlock)b;
88 break;
89 }
90 }
91 return ret;
92 }
93
94
95
96 public TextBlock previousTextBlock() {
97 TextBlock ret = null;
98 Block b = this;
99 while (b.hasPrevious()) {
100 b = b.previous();
101 if (b instanceof TextBlock) {
102 ret = (TextBlock)b;
103 break;
104 }
105 }
106 return ret;
107 }
108
109
110
111
112
113 public WordBlock getNextWordBlock() {
114 WordBlock ret = null;
115 Block b = this;
116 while (b.hasNext()) {
117 b = b.next();
118 if (b instanceof WordBlock) {
119 ret = (WordBlock)b;
120 break;
121 }
122 }
123 return ret;
124 }
125
126
127
128 public WordBlock getPreviousWordBlock() {
129 WordBlock ret = null;
130 Block b = this;
131 while (b.hasPrevious()) {
132 b = b.previous();
133 if (b instanceof WordBlock) {
134 ret = (WordBlock)b;
135 break;
136 }
137 }
138 return ret;
139 }
140
141
142
143
144 public void insertCharacter(char c, int ipos) {
145 if (ipos == nchar) {
146 setText(text + c);
147 } else if (ipos == 0) {
148 setText(c + text);
149 } else {
150 setText(text.substring(0, ipos) + c + text.substring(ipos, nchar));
151 }
152
153 }
154
155
156 public void newlineAfter() {
157 Block b = new NewlineBlock();
158 b.setNext(next());
159 setNext(b);
160 }
161
162
163 public void insertNewline(int pos) {
164 insert(new NewlineBlock(), pos);
165 }
166
167
168 public abstract TextBlock makeCopy();
169
170 public abstract boolean matches(TextBlock tb);
171
172
173 public void insert(TextBlock b_middle, int caretPos) {
174 String texta = text.substring(0, caretPos);
175 String textb = text.substring(caretPos, nchar);
176
177 TextBlock b_end = makeCopy();
178
179 Block b_after = next();
180 this.setText(texta);
181 b_end.setText(textb);
182
183 this.setNext(b_middle);
184 b_middle.setNext(b_end);
185 b_end.setNext(b_after);
186 }
187
188
189
190 public void remove() {
191 if (previous() == null) {
192 E.warning("null previous?");
193 } else {
194 Block pr = previous();
195 Block nx = next();
196 if (pr instanceof TextBlock && nx instanceof TextBlock) {
197 TextBlock tpr = (TextBlock)pr;
198 TextBlock tnx = (TextBlock)nx;
199
200 if (tpr.matches(tnx)) {
201 tpr.append(tnx);
202
203 } else {
204 E.warning("shouldnt get here? " + pr.getClass() + " " + nx.getClass());
205 previous().setNext(next());
206 }
207 }
208 }
209 }
210
211
212 public void append(TextBlock tb) {
213 Block bn = tb.next();
214 setText(text + tb.getText());
215 setNext(bn);
216 }
217
218
219 public void deleteCharBefore(int caretPos) {
220 String tnew = text.substring(0, caretPos-1) + text.substring(caretPos, nchar);
221 setText(tnew);
222 }
223
224
225
226
227
228
229 public void setType(XType xt) {
230 if (type != null) {
231
232 }
233 type = xt;
234 notifyAppearanceChange();
235 }
236
237 public XType getType() {
238 return type;
239 }
240
241 public Guise getGuise() {
242 Guise ret = null;
243 if (type != null) {
244 ret = type.getGuise();
245 } else {
246 ret = StandardGuises.getDefault();
247 }
248 return ret;
249 }
250
251 }