1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
package palmed.edit.text; |
32 |
|
|
33 |
|
import java.io.IOException; |
34 |
|
import java.io.InputStream; |
35 |
|
import java.io.OutputStream; |
36 |
|
import palmed.edit.util.Coordinate; |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
final class Chunk implements ITextView, ICachable, IChunk |
45 |
|
{ |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
private IText text_; |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
private int columns_, lines_; |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
private int firstLineLength_, lastLineLength_; |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
private boolean hasBeenModified_; |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
private final ICache cache_; |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
public Chunk( final ICache cache ) |
73 |
260 |
{ |
74 |
260 |
if( cache == null ) |
75 |
0 |
throw new IllegalArgumentException( "parameter 'cache' is null" ); |
76 |
260 |
cache_ = cache; |
77 |
260 |
text_ = new Text(); |
78 |
260 |
text_.register( this ); |
79 |
260 |
} |
80 |
|
|
81 |
|
private IText getText() |
82 |
|
{ |
83 |
1760 |
if( text_ == null ) |
84 |
|
{ |
85 |
0 |
text_ = new Text(); |
86 |
0 |
cache_.wake( this ); |
87 |
0 |
text_.register( this ); |
88 |
|
} |
89 |
1760 |
return text_; |
90 |
|
} |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
public void sleep() |
96 |
|
{ |
97 |
0 |
text_ = null; |
98 |
0 |
} |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
public int getHeight() |
104 |
|
{ |
105 |
190 |
return lines_; |
106 |
|
} |
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
public void update( final int columns, final int lines ) |
112 |
|
{ |
113 |
505 |
columns_ = columns; |
114 |
505 |
lines_ = lines - 1; |
115 |
505 |
firstLineLength_ = getText().getLine( 0 ).length(); |
116 |
505 |
lastLineLength_ = getText().getLine( lines_ ).length(); |
117 |
505 |
} |
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
public void modified( final boolean status ) |
123 |
|
{ |
124 |
545 |
hasBeenModified_ = status; |
125 |
545 |
} |
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
public boolean hasBeenModified() |
131 |
|
{ |
132 |
385 |
return hasBeenModified_; |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
public int handle( final ILineExtractor functor, final int line ) |
139 |
|
{ |
140 |
505 |
final int lines = lines_; |
141 |
505 |
if( line <= lines ) |
142 |
425 |
functor.select( getText(), line ); |
143 |
505 |
return lines; |
144 |
|
} |
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
public Coordinate handle( final ITextExtractor functor, final Coordinate start, final Coordinate from, |
150 |
|
final Coordinate to ) |
151 |
|
{ |
152 |
80 |
final int lines = lines_; |
153 |
80 |
if( from.y_ - start.y_ <= lines ) |
154 |
|
{ |
155 |
80 |
final int length = lastLineLength_; |
156 |
80 |
final Coordinate translatedFrom = translate( from, start ); |
157 |
80 |
final Coordinate translatedTo = translate( to, start ); |
158 |
80 |
if( !translatedTo.equals( translatedFrom ) ) |
159 |
35 |
functor.select( getText(), translatedFrom, translatedTo ); |
160 |
80 |
if( from.y_ - start.y_ == 0 ) |
161 |
65 |
return new Coordinate( start.x_ + length, start.y_ + lines ); |
162 |
15 |
return new Coordinate( length, start.y_ + lines ); |
163 |
|
} |
164 |
0 |
return new Coordinate( start.x_, start.y_ + lines ); |
165 |
|
} |
166 |
|
|
167 |
|
private Coordinate translate( final Coordinate coordinate, final Coordinate start ) |
168 |
|
{ |
169 |
160 |
if( coordinate.y_ < start.y_ ) |
170 |
10 |
return new Coordinate( 0, 0 ); |
171 |
150 |
if( coordinate.y_ > start.y_ + lines_ ) |
172 |
0 |
return new Coordinate( lastLineLength_, lines_ ); |
173 |
150 |
final int y = coordinate.y_ - start.y_; |
174 |
150 |
if( coordinate.x_ <= start.x_ ) |
175 |
80 |
return new Coordinate( 0, y ); |
176 |
70 |
final int x = getText().getLine( y ).length(); |
177 |
70 |
if( coordinate.x_ >= start.x_ + x ) |
178 |
20 |
return new Coordinate( x, y ); |
179 |
50 |
return new Coordinate( coordinate.x_ - start.x_, y ); |
180 |
|
} |
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
public int count( final Coordinate size, final int previous ) |
186 |
|
{ |
187 |
345 |
final int length = previous + firstLineLength_; |
188 |
345 |
size.x_ = Math.max( size.x_, Math.max( length, columns_ ) ); |
189 |
345 |
size.y_ += lines_; |
190 |
345 |
if( lines_ > 0 ) |
191 |
90 |
return lastLineLength_; |
192 |
255 |
return length; |
193 |
|
} |
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
|
public void marshall( final OutputStream stream ) throws IOException |
199 |
|
{ |
200 |
0 |
getText().marshall( stream ); |
201 |
0 |
} |
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
public void unmarshall( final InputStream stream ) throws IOException |
207 |
|
{ |
208 |
0 |
getText().unmarshall( stream ); |
209 |
0 |
} |
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
public void read( final InputStream stream ) throws IOException |
215 |
|
{ |
216 |
180 |
getText().read( stream ); |
217 |
180 |
} |
218 |
|
|
219 |
|
|
220 |
|
|
221 |
|
|
222 |
|
public void write( final OutputStream stream ) throws IOException |
223 |
|
{ |
224 |
40 |
getText().write( stream ); |
225 |
40 |
} |
226 |
|
} |