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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
/* ZoneView.java -- An effective BoxView subclass
Copyright (C) 2006 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package javax.swing.text;
import java.awt.Shape;
import java.util.ArrayList;
import java.util.LinkedList;
import javax.swing.event.DocumentEvent;
/**
* A View implementation that delays loading of sub views until they are
* needed for display or internal transformations. This can be used for
* editors that need to handle large documents more effectivly than the
* standard {@link BoxView}.
*
* @author Roman Kennke (kennke@aicas.com)
*
* @since 1.3
*/
public class ZoneView
extends BoxView
{
/**
* The default zone view implementation. The specs suggest that this is
* a subclass of AsyncBoxView, so do we.
*/
static class Zone
extends AsyncBoxView
{
/**
* The start position for this zone.
*/
private Position p0;
/**
* The end position for this zone.
*/
private Position p1;
/**
* Creates a new Zone for the specified element, start and end positions.
*
* @param el the element
* @param pos0 the start position
* @param pos1 the end position
* @param axis the major axis
*/
Zone(Element el, Position pos0, Position pos1, int axis)
{
super(el, axis);
p0 = pos0;
p1 = pos1;
}
/**
* Returns the start offset of the zone.
*
* @return the start offset of the zone
*/
public int getStartOffset()
{
return p0.getOffset();
}
/**
* Returns the end offset of the zone.
*
* @return the end offset of the zone
*/
public int getEndOffset()
{
return p1.getOffset();
}
}
/**
* The maximumZoneSize.
*/
private int maximumZoneSize;
/**
* The maximum number of loaded zones.
*/
private int maxZonesLoaded;
/**
* A queue of loaded zones. When the number of loaded zones exceeds the
* maximum number of zones, the oldest zone(s) get unloaded.
*/
private LinkedList loadedZones;
/**
* Creates a new ZoneView for the specified element and axis.
*
* @param element the element for which to create a ZoneView
* @param axis the major layout axis for the box
*/
public ZoneView(Element element, int axis)
{
super(element, axis);
maximumZoneSize = 8192;
maxZonesLoaded = 3;
loadedZones = new LinkedList();
}
/**
* Sets the maximum zone size. Note that zones might still become larger
* then the size specified when a singe child view is larger for itself,
* because zones are formed on child view boundaries.
*
* @param size the maximum zone size to set
*
* @see #getMaximumZoneSize()
*/
public void setMaximumZoneSize(int size)
{
maximumZoneSize = size;
}
/**
* Returns the maximum zone size. Note that zones might still become larger
* then the size specified when a singe child view is larger for itself,
* because zones are formed on child view boundaries.
*
* @return the maximum zone size
*
* @see #setMaximumZoneSize(int)
*/
public int getMaximumZoneSize()
{
return maximumZoneSize;
}
/**
* Sets the maximum number of zones that are allowed to be loaded at the
* same time. If the new number of allowed zones is smaller then the
* previous settings, this unloads all zones the aren't allowed to be
* loaded anymore.
*
* @param num the number of zones allowed to be loaded at the same time
*
* @throws IllegalArgumentException if <code>num <= 0</code>
*
* @see #getMaxZonesLoaded()
*/
public void setMaxZonesLoaded(int num)
{
if (num < 1)
throw new IllegalArgumentException("Illegal number of zones");
maxZonesLoaded = num;
unloadOldestZones();
}
/**
* Returns the number of zones that are allowed to be loaded.
*
* @return the number of zones that are allowed to be loaded
*
* @see #setMaxZonesLoaded(int)
*/
public int getMaxZonesLoaded()
{
return maxZonesLoaded;
}
/**
* Gets called after a zone has been loaded. This unloads the oldest zone(s)
* when the maximum number of zones is reached.
*
* @param zone the zone that has been loaded
*/
protected void zoneWasLoaded(View zone)
{
loadedZones.addLast(zone);
unloadOldestZones();
}
/**
* This unloads the specified zone. This is implemented to simply remove
* all child views from that zone.
*
* @param zone the zone to be unloaded
*/
protected void unloadZone(View zone)
{
zone.removeAll();
}
/**
* Returns <code>true</code> when the specified zone is loaded,
* <code>false</code> otherwise. The default implementation checks if
* the zone view has child elements.
*
* @param zone the zone view to check
*
* @return <code>true</code> when the specified zone is loaded,
* <code>false</code> otherwise
*/
protected boolean isZoneLoaded(View zone)
{
return zone.getViewCount() > 0;
}
/**
* Creates a zone for the specified range. Subclasses can override this
* to provide a custom implementation for the zones.
*
* @param p0 the start of the range
* @param p1 the end of the range
*
* @return the zone
*/
protected View createZone(int p0, int p1)
{
Document doc = getDocument();
Position pos0 = null;
Position pos1 = null;
try
{
pos0 = doc.createPosition(p0);
pos1 = doc.createPosition(p1);
}
catch (BadLocationException ex)
{
assert false : "Must not happen";
}
Zone zone = new Zone(getElement(), pos0, pos1, getAxis());
return zone;
}
// --------------------------------------------------------------------------
// CompositeView methods.
// --------------------------------------------------------------------------
/**
* Overridden to not load all the child views. This methods creates
* initial zones without actually loading them.
*
* @param vf not used
*/
protected void loadChildren(ViewFactory vf)
{
int p0 = getStartOffset();
int p1 = getEndOffset();
append(createZone(p0, p1));
checkZoneAt(p0);
}
/**
* Returns the index of the child view at the document position
* <code>pos</code>.
*
* This overrides the CompositeView implementation because the ZoneView does
* not provide a one to one mapping from Elements to Views.
*
* @param pos the document position
*
* @return the index of the child view at the document position
* <code>pos</code>
*/
protected int getViewIndexAtPosition(int pos)
{
int index = -1;
boolean found = false;
if (pos >= getStartOffset() && pos <= getEndOffset())
{
int upper = getViewCount() - 1;
int lower = 0;
index = (upper - lower) / 2 + lower;
int bias = 0;
do
{
View child = getView(index);
int childStart = child.getStartOffset();
int childEnd = child.getEndOffset();
if (pos >= childStart && pos < childEnd)
found = true;
else if (pos < childStart)
{
upper = index;
bias = -1;
}
else if (pos >= childEnd)
{
lower = index;
bias = 1;
}
if (! found)
{
int newIndex = (upper - lower) / 2 + lower;
if (newIndex == index)
index = newIndex + bias;
else
index = newIndex;
}
} while (upper != lower && ! found);
}
// If no child view actually covers the specified offset, reset index to
// -1.
if (! found)
index = -1;
return index;
}
// --------------------------------------------------------------------------
// View methods.
// --------------------------------------------------------------------------
public void insertUpdate(DocumentEvent e, Shape a, ViewFactory vf)
{
// TODO: Implement this.
}
public void removeUpdate(DocumentEvent e, Shape a, ViewFactory vf)
{
// TODO: Implement this.
}
protected boolean updateChildren(DocumentEvent.ElementChange ec,
DocumentEvent e, ViewFactory vf)
{
// TODO: Implement this.
return false;
}
// --------------------------------------------------------------------------
// Internal helper methods.
// --------------------------------------------------------------------------
/**
* A helper method to unload the oldest zones when there are more loaded
* zones then allowed.
*/
private void unloadOldestZones()
{
int maxZones = getMaxZonesLoaded();
while (loadedZones.size() > maxZones)
{
View zone = (View) loadedZones.removeFirst();
unloadZone(zone);
}
}
/**
* Checks if the zone view at position <code>pos</code> should be split
* (its size is greater than maximumZoneSize) and tries to split it.
*
* @param pos the document position to check
*/
private void checkZoneAt(int pos)
{
int viewIndex = getViewIndexAtPosition(pos); //, Position.Bias.Forward);
View view = getView(viewIndex);
int p0 = view.getStartOffset();
int p1 = view.getEndOffset();
if (p1 - p0 > maximumZoneSize)
splitZone(viewIndex, p0, p1);
}
/**
* Tries to break the view at the specified index and inside the specified
* range into pieces that are acceptable with respect to the maximum zone
* size.
*
* @param index the index of the view to split
* @param p0 the start offset
* @param p1 the end offset
*/
private void splitZone(int index, int p0, int p1)
{
ArrayList newZones = new ArrayList();
int p = p0;
do
{
p0 = p;
p = Math.min(getPreferredZoneEnd(p0), p1);
newZones.add(createZone(p0, p));
} while (p < p1);
View[] newViews = new View[newZones.size()];
newViews = (View[]) newZones.toArray(newViews);
replace(index, 1, newViews);
}
/**
* Calculates the positions at which a zone split is performed. This
* tries to create zones sized close to half the maximum zone size.
*
* @param start the start offset
*
* @return the preferred end offset
*/
private int getPreferredZoneEnd(int start)
{
Element el = getElement();
int index = el.getElementIndex(start + (maximumZoneSize / 2));
Element child = el.getElement(index);
int p0 = child.getStartOffset();
int p1 = child.getEndOffset();
int end = p1;
if (p0 - start > maximumZoneSize && p0 > start)
end = p0;
return end;
}
}
|