KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > WrappedPlainView


1 /*
2  * @(#)WrappedPlainView.java 1.38 04/05/26
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.text;
8
9 import java.util.Vector JavaDoc;
10 import java.util.Properties JavaDoc;
11 import java.awt.*;
12 import javax.swing.event.*;
13
14 /**
15  * View of plain text (text with only one font and color)
16  * that does line-wrapping. This view expects that its
17  * associated element has child elements that represent
18  * the lines it should be wrapping. It is implemented
19  * as a vertical box that contains logical line views.
20  * The logical line views are nested classes that render
21  * the logical line as multiple physical line if the logical
22  * line is too wide to fit within the allocation. The
23  * line views draw upon the outer class for its state
24  * to reduce their memory requirements.
25  * <p>
26  * The line views do all of their rendering through the
27  * <code>drawLine</code> method which in turn does all of
28  * its rendering through the <code>drawSelectedText</code>
29  * and <code>drawUnselectedText</code> methods. This
30  * enables subclasses to easily specialize the rendering
31  * without concern for the layout aspects.
32  *
33  * @author Timothy Prinzing
34  * @version 1.38 05/26/04
35  * @see View
36  */

37 public class WrappedPlainView extends BoxView JavaDoc implements TabExpander JavaDoc {
38
39     /**
40      * Creates a new WrappedPlainView. Lines will be wrapped
41      * on character boundaries.
42      *
43      * @param elem the element underlying the view
44      */

45     public WrappedPlainView(Element JavaDoc elem) {
46     this(elem, false);
47     }
48
49     /**
50      * Creates a new WrappedPlainView. Lines can be wrapped on
51      * either character or word boundaries depending upon the
52      * setting of the wordWrap parameter.
53      *
54      * @param elem the element underlying the view
55      * @param wordWrap should lines be wrapped on word boundaries?
56      */

57     public WrappedPlainView(Element JavaDoc elem, boolean wordWrap) {
58     super(elem, Y_AXIS);
59     this.wordWrap = wordWrap;
60     }
61
62     /**
63      * Returns the tab size set for the document, defaulting to 8.
64      *
65      * @return the tab size
66      */

67     protected int getTabSize() {
68         Integer JavaDoc i = (Integer JavaDoc) getDocument().getProperty(PlainDocument.tabSizeAttribute);
69         int size = (i != null) ? i.intValue() : 8;
70         return size;
71     }
72
73     /**
74      * Renders a line of text, suppressing whitespace at the end
75      * and expanding any tabs. This is implemented to make calls
76      * to the methods <code>drawUnselectedText</code> and
77      * <code>drawSelectedText</code> so that the way selected and
78      * unselected text are rendered can be customized.
79      *
80      * @param p0 the starting document location to use >= 0
81      * @param p1 the ending document location to use >= p1
82      * @param g the graphics context
83      * @param x the starting X position >= 0
84      * @param y the starting Y position >= 0
85      * @see #drawUnselectedText
86      * @see #drawSelectedText
87      */

88     protected void drawLine(int p0, int p1, Graphics g, int x, int y) {
89         Element JavaDoc lineMap = getElement();
90     Element JavaDoc line = lineMap.getElement(lineMap.getElementIndex(p0));
91     Element JavaDoc elem;
92
93         try {
94         if (line.isLeaf()) {
95             drawText(line, p0, p1, g, x, y);
96         } else {
97         // this line contains the composed text.
98
int idx = line.getElementIndex(p0);
99         int lastIdx = line.getElementIndex(p1);
100         for(; idx <= lastIdx; idx++) {
101             elem = line.getElement(idx);
102             int start = Math.max(elem.getStartOffset(), p0);
103             int end = Math.min(elem.getEndOffset(), p1);
104             x = drawText(elem, start, end, g, x, y);
105         }
106         }
107         } catch (BadLocationException JavaDoc e) {
108             throw new StateInvariantError JavaDoc("Can't render: " + p0 + "," + p1);
109         }
110     }
111         
112     private int drawText(Element JavaDoc elem, int p0, int p1, Graphics g, int x, int y) throws BadLocationException JavaDoc {
113         p1 = Math.min(getDocument().getLength(), p1);
114     AttributeSet JavaDoc attr = elem.getAttributes();
115
116     if (Utilities.isComposedTextAttributeDefined(attr)) {
117         g.setColor(unselected);
118         x = Utilities.drawComposedText(this, attr, g, x, y,
119                     p0-elem.getStartOffset(),
120                     p1-elem.getStartOffset());
121     } else {
122         if (sel0 == sel1 || selected == unselected) {
123         // no selection, or it is invisible
124
x = drawUnselectedText(g, x, y, p0, p1);
125         } else if ((p0 >= sel0 && p0 <= sel1) && (p1 >= sel0 && p1 <= sel1)) {
126         x = drawSelectedText(g, x, y, p0, p1);
127         } else if (sel0 >= p0 && sel0 <= p1) {
128         if (sel1 >= p0 && sel1 <= p1) {
129             x = drawUnselectedText(g, x, y, p0, sel0);
130             x = drawSelectedText(g, x, y, sel0, sel1);
131             x = drawUnselectedText(g, x, y, sel1, p1);
132         } else {
133             x = drawUnselectedText(g, x, y, p0, sel0);
134             x = drawSelectedText(g, x, y, sel0, p1);
135         }
136         } else if (sel1 >= p0 && sel1 <= p1) {
137         x = drawSelectedText(g, x, y, p0, sel1);
138         x = drawUnselectedText(g, x, y, sel1, p1);
139         } else {
140         x = drawUnselectedText(g, x, y, p0, p1);
141         }
142     }
143
144         return x;
145     }
146
147     /**
148      * Renders the given range in the model as normal unselected
149      * text.
150      *
151      * @param g the graphics context
152      * @param x the starting X coordinate >= 0
153      * @param y the starting Y coordinate >= 0
154      * @param p0 the beginning position in the model >= 0
155      * @param p1 the ending position in the model >= p0
156      * @return the X location of the end of the range >= 0
157      * @exception BadLocationException if the range is invalid
158      */

159     protected int drawUnselectedText(Graphics g, int x, int y,
160                                      int p0, int p1) throws BadLocationException JavaDoc {
161         g.setColor(unselected);
162         Document JavaDoc doc = getDocument();
163         Segment JavaDoc segment = SegmentCache.getSharedSegment();
164         doc.getText(p0, p1 - p0, segment);
165         int ret = Utilities.drawTabbedText(this, segment, x, y, g, this, p0);
166         SegmentCache.releaseSharedSegment(segment);
167         return ret;
168     }
169
170     /**
171      * Renders the given range in the model as selected text. This
172      * is implemented to render the text in the color specified in
173      * the hosting component. It assumes the highlighter will render
174      * the selected background.
175      *
176      * @param g the graphics context
177      * @param x the starting X coordinate >= 0
178      * @param y the starting Y coordinate >= 0
179      * @param p0 the beginning position in the model >= 0
180      * @param p1 the ending position in the model >= p0
181      * @return the location of the end of the range.
182      * @exception BadLocationException if the range is invalid
183      */

184     protected int drawSelectedText(Graphics g, int x,
185                                    int y, int p0, int p1) throws BadLocationException JavaDoc {
186         g.setColor(selected);
187         Document JavaDoc doc = getDocument();
188         Segment JavaDoc segment = SegmentCache.getSharedSegment();
189         doc.getText(p0, p1 - p0, segment);
190         int ret = Utilities.drawTabbedText(this, segment, x, y, g, this, p0);
191         SegmentCache.releaseSharedSegment(segment);
192         return ret;
193     }
194
195     /**
196      * Gives access to a buffer that can be used to fetch
197      * text from the associated document.
198      *
199      * @return the buffer
200      */

201     protected final Segment JavaDoc getLineBuffer() {
202         if (lineBuffer == null) {
203             lineBuffer = new Segment JavaDoc();
204         }
205         return lineBuffer;
206     }
207
208     /**
209      * This is called by the nested wrapped line
210      * views to determine the break location. This can
211      * be reimplemented to alter the breaking behavior.
212      * It will either break at word or character boundaries
213      * depending upon the break argument given at
214      * construction.
215      */

216     protected int calculateBreakPosition(int p0, int p1) {
217     int p;
218         Segment JavaDoc segment = SegmentCache.getSharedSegment();
219     loadText(segment, p0, p1);
220         int currentWidth = getWidth();
221     if (wordWrap) {
222         p = p0 + Utilities.getBreakLocation(segment, metrics,
223                         tabBase, tabBase + currentWidth,
224                         this, p0);
225     } else {
226         p = p0 + Utilities.getTabbedTextOffset(segment, metrics,
227                            tabBase, tabBase + currentWidth,
228                            this, p0, false);
229     }
230         SegmentCache.releaseSharedSegment(segment);
231     return p;
232     }
233
234     /**
235      * Loads all of the children to initialize the view.
236      * This is called by the <code>setParent</code> method.
237      * Subclasses can reimplement this to initialize their
238      * child views in a different manner. The default
239      * implementation creates a child view for each
240      * child element.
241      *
242      * @param f the view factory
243      */

244     protected void loadChildren(ViewFactory JavaDoc f) {
245         Element JavaDoc e = getElement();
246         int n = e.getElementCount();
247         if (n > 0) {
248             View JavaDoc[] added = new View JavaDoc[n];
249             for (int i = 0; i < n; i++) {
250                 added[i] = new WrappedLine(e.getElement(i));
251             }
252             replace(0, 0, added);
253         }
254     }
255
256     /**
257      * Update the child views in response to a
258      * document event.
259      */

260     void updateChildren(DocumentEvent e, Shape a) {
261         Element JavaDoc elem = getElement();
262         DocumentEvent.ElementChange ec = e.getChange(elem);
263         if (ec != null) {
264             // the structure of this element changed.
265
Element JavaDoc[] removedElems = ec.getChildrenRemoved();
266             Element JavaDoc[] addedElems = ec.getChildrenAdded();
267             View JavaDoc[] added = new View JavaDoc[addedElems.length];
268             for (int i = 0; i < addedElems.length; i++) {
269                 added[i] = new WrappedLine(addedElems[i]);
270             }
271             replace(ec.getIndex(), removedElems.length, added);
272
273             // should damge a little more intelligently.
274
if (a != null) {
275                 preferenceChanged(null, true, true);
276                 getContainer().repaint();
277             }
278         }
279
280     // update font metrics which may be used by the child views
281
updateMetrics();
282     }
283
284     /**
285      * Load the text buffer with the given range
286      * of text. This is used by the fragments
287      * broken off of this view as well as this
288      * view itself.
289      */

290     final void loadText(Segment JavaDoc segment, int p0, int p1) {
291     try {
292         Document JavaDoc doc = getDocument();
293         doc.getText(p0, p1 - p0, segment);
294     } catch (BadLocationException JavaDoc bl) {
295         throw new StateInvariantError JavaDoc("Can't get line text");
296     }
297     }
298
299     final void updateMetrics() {
300     Component host = getContainer();
301     Font f = host.getFont();
302     metrics = host.getFontMetrics(f);
303     tabSize = getTabSize() * metrics.charWidth('m');
304     }
305
306     // --- TabExpander methods ------------------------------------------
307

308     /**
309      * Returns the next tab stop position after a given reference position.
310      * This implementation does not support things like centering so it
311      * ignores the tabOffset argument.
312      *
313      * @param x the current position >= 0
314      * @param tabOffset the position within the text stream
315      * that the tab occurred at >= 0.
316      * @return the tab stop, measured in points >= 0
317      */

318     public float nextTabStop(float x, int tabOffset) {
319     if (tabSize == 0)
320         return x;
321         int ntabs = ((int) x - tabBase) / tabSize;
322         return tabBase + ((ntabs + 1) * tabSize);
323     }
324
325     
326     // --- View methods -------------------------------------
327

328     /**
329      * Renders using the given rendering surface and area
330      * on that surface. This is implemented to stash the
331      * selection positions, selection colors, and font
332      * metrics for the nested lines to use.
333      *
334      * @param g the rendering surface to use
335      * @param a the allocated region to render into
336      *
337      * @see View#paint
338      */

339     public void paint(Graphics g, Shape a) {
340     Rectangle alloc = (Rectangle) a;
341     tabBase = alloc.x;
342     JTextComponent JavaDoc host = (JTextComponent JavaDoc) getContainer();
343     sel0 = host.getSelectionStart();
344     sel1 = host.getSelectionEnd();
345     unselected = (host.isEnabled()) ?
346         host.getForeground() : host.getDisabledTextColor();
347     Caret JavaDoc c = host.getCaret();
348         selected = c.isSelectionVisible() && host.getHighlighter() != null ?
349                         host.getSelectedTextColor() : unselected;
350     g.setFont(host.getFont());
351
352         // superclass paints the children
353
super.paint(g, a);
354     }
355
356     /**
357      * Sets the size of the view. This should cause
358      * layout of the view along the given axis, if it
359      * has any layout duties.
360      *
361      * @param width the width >= 0
362      * @param height the height >= 0
363      */

364     public void setSize(float width, float height) {
365     updateMetrics();
366     if ((int) width != getWidth()) {
367         // invalidate the view itself since the childrens
368
// desired widths will be based upon this views width.
369
preferenceChanged(null, true, true);
370         widthChanging = true;
371     }
372     super.setSize(width, height);
373     widthChanging = false;
374     }
375
376     /**
377      * Determines the preferred span for this view along an
378      * axis. This is implemented to provide the superclass
379      * behavior after first making sure that the current font
380      * metrics are cached (for the nested lines which use
381      * the metrics to determine the height of the potentially
382      * wrapped lines).
383      *
384      * @param axis may be either View.X_AXIS or View.Y_AXIS
385      * @return the span the view would like to be rendered into.
386      * Typically the view is told to render into the span
387      * that is returned, although there is no guarantee.
388      * The parent may choose to resize or break the view.
389      * @see View#getPreferredSpan
390      */

391     public float getPreferredSpan(int axis) {
392     updateMetrics();
393     return super.getPreferredSpan(axis);
394     }
395
396     /**
397      * Determines the minimum span for this view along an
398      * axis. This is implemented to provide the superclass
399      * behavior after first making sure that the current font
400      * metrics are cached (for the nested lines which use
401      * the metrics to determine the height of the potentially
402      * wrapped lines).
403      *
404      * @param axis may be either View.X_AXIS or View.Y_AXIS
405      * @return the span the view would like to be rendered into.
406      * Typically the view is told to render into the span
407      * that is returned, although there is no guarantee.
408      * The parent may choose to resize or break the view.
409      * @see View#getMinimumSpan
410      */

411     public float getMinimumSpan(int axis) {
412     updateMetrics();
413     return super.getMinimumSpan(axis);
414     }
415
416     /**
417      * Determines the maximum span for this view along an
418      * axis. This is implemented to provide the superclass
419      * behavior after first making sure that the current font
420      * metrics are cached (for the nested lines which use
421      * the metrics to determine the height of the potentially
422      * wrapped lines).
423      *
424      * @param axis may be either View.X_AXIS or View.Y_AXIS
425      * @return the span the view would like to be rendered into.
426      * Typically the view is told to render into the span
427      * that is returned, although there is no guarantee.
428      * The parent may choose to resize or break the view.
429      * @see View#getMaximumSpan
430      */

431     public float getMaximumSpan(int axis) {
432     updateMetrics();
433     return super.getMaximumSpan(axis);
434     }
435
436     /**
437      * Gives notification that something was inserted into the
438      * document in a location that this view is responsible for.
439      * This is implemented to simply update the children.
440      *
441      * @param e the change information from the associated document
442      * @param a the current allocation of the view
443      * @param f the factory to use to rebuild if the view has children
444      * @see View#insertUpdate
445      */

446     public void insertUpdate(DocumentEvent e, Shape a, ViewFactory JavaDoc f) {
447         updateChildren(e, a);
448
449         Rectangle alloc = ((a != null) && isAllocationValid()) ?
450             getInsideAllocation(a) : null;
451         int pos = e.getOffset();
452         View JavaDoc v = getViewAtPosition(pos, alloc);
453         if (v != null) {
454             v.insertUpdate(e, alloc, f);
455         }
456     }
457
458     /**
459      * Gives notification that something was removed from the
460      * document in a location that this view is responsible for.
461      * This is implemented to simply update the children.
462      *
463      * @param e the change information from the associated document
464      * @param a the current allocation of the view
465      * @param f the factory to use to rebuild if the view has children
466      * @see View#removeUpdate
467      */

468     public void removeUpdate(DocumentEvent e, Shape a, ViewFactory JavaDoc f) {
469         updateChildren(e, a);
470
471         Rectangle alloc = ((a != null) && isAllocationValid()) ?
472             getInsideAllocation(a) : null;
473         int pos = e.getOffset();
474         View JavaDoc v = getViewAtPosition(pos, alloc);
475         if (v != null) {
476             v.removeUpdate(e, alloc, f);
477         }
478     }
479
480     /**
481      * Gives notification from the document that attributes were changed
482      * in a location that this view is responsible for.
483      *
484      * @param e the change information from the associated document
485      * @param a the current allocation of the view
486      * @param f the factory to use to rebuild if the view has children
487      * @see View#changedUpdate
488      */

489     public void changedUpdate(DocumentEvent e, Shape a, ViewFactory JavaDoc f) {
490         updateChildren(e, a);
491     }
492
493     // --- variables -------------------------------------------
494

495     FontMetrics metrics;
496     Segment JavaDoc lineBuffer;
497     boolean widthChanging;
498     int tabBase;
499     int tabSize;
500     boolean wordWrap;
501     
502     int sel0;
503     int sel1;
504     Color unselected;
505     Color selected;
506
507
508     /**
509      * Simple view of a line that wraps if it doesn't
510      * fit withing the horizontal space allocated.
511      * This class tries to be lightweight by carrying little
512      * state of it's own and sharing the state of the outer class
513      * with it's sibblings.
514      */

515     class WrappedLine extends View JavaDoc {
516
517         WrappedLine(Element JavaDoc elem) {
518             super(elem);
519         }
520
521         /**
522          * Calculate the number of lines that will be rendered
523          * by logical line when it is wrapped.
524          */

525         final int calculateLineCount() {
526             int nlines = 0;
527             int p1 = getEndOffset();
528             for (int p0 = getStartOffset(); p0 < p1; ) {
529                 nlines += 1;
530         int p = calculateBreakPosition(p0, p1);
531         p0 = (p == p0) ? ++p : p; // this is the fix of #4410243
532
// we check on situation when
533
// width is too small and
534
// break position is calculated
535
// incorrect
536
}
537             return nlines;
538         }
539
540         /**
541          * Determines the preferred span for this view along an
542          * axis.
543          *
544          * @param axis may be either X_AXIS or Y_AXIS
545          * @return the span the view would like to be rendered into.
546          * Typically the view is told to render into the span
547          * that is returned, although there is no guarantee.
548          * The parent may choose to resize or break the view.
549          * @see View#getPreferredSpan
550          */

551         public float getPreferredSpan(int axis) {
552             switch (axis) {
553             case View.X_AXIS:
554                 float width = getWidth();
555                 if (width == Integer.MAX_VALUE) {
556                     // We have been initially set to MAX_VALUE, but we don't
557
// want this as our preferred.
558
return 100f;
559                 }
560                 return width;
561             case View.Y_AXIS:
562         if (nlines == 0 || widthChanging) {
563             nlines = calculateLineCount();
564         }
565                 int h = nlines * metrics.getHeight();
566                 return h;
567             default:
568                 throw new IllegalArgumentException JavaDoc("Invalid axis: " + axis);
569             }
570         }
571
572         /**
573          * Renders using the given rendering surface and area on that
574          * surface. The view may need to do layout and create child
575          * views to enable itself to render into the given allocation.
576          *
577          * @param g the rendering surface to use
578          * @param a the allocated region to render into
579          * @see View#paint
580          */

581         public void paint(Graphics g, Shape a) {
582             Rectangle alloc = (Rectangle) a;
583             int y = alloc.y + metrics.getAscent();
584             int x = alloc.x;
585
586         JTextComponent JavaDoc host = (JTextComponent JavaDoc)getContainer();
587         Highlighter JavaDoc h = host.getHighlighter();
588         LayeredHighlighter JavaDoc dh = (h instanceof LayeredHighlighter JavaDoc) ?
589                              (LayeredHighlighter JavaDoc)h : null;
590             int p1 = getEndOffset();
591             for (int p0 = getStartOffset(); p0 < p1; ) {
592         int p = calculateBreakPosition(p0, p1);
593         if (dh != null) {
594             if (p == p1) {
595             dh.paintLayeredHighlights(g, p0, p - 1, a, host, this);
596             }
597             else {
598             dh.paintLayeredHighlights(g, p0, p, a, host, this);
599             }
600         }
601                 drawLine(p0, p, g, x, y);
602                 
603                 p0 = (p == p0) ? p1 : p;
604                 y += metrics.getHeight();
605             }
606         }
607
608         /**
609          * Provides a mapping from the document model coordinate space
610          * to the coordinate space of the view mapped to it.
611          *
612          * @param pos the position to convert
613          * @param a the allocated region to render into
614          * @return the bounding box of the given position is returned
615          * @exception BadLocationException if the given position does not represent a
616          * valid location in the associated document
617          * @see View#modelToView
618          */

619         public Shape modelToView(int pos, Shape a, Position.Bias JavaDoc b) throws BadLocationException JavaDoc {
620         Rectangle alloc = a.getBounds();
621             alloc.height = metrics.getHeight();
622             alloc.width = 1;
623             
624             int p1 = getEndOffset();
625         int p0 = getStartOffset();
626         int testP = (b == Position.Bias.Forward) ? pos :
627                 Math.max(p0, pos - 1);
628             while (p0 < p1) {
629         int p = calculateBreakPosition(p0, p1);
630                 if ((pos >= p0) && (testP < p)) {
631                     // it's in this line
632
Segment JavaDoc segment = SegmentCache.getSharedSegment();
633                     loadText(segment, p0, pos);
634                     alloc.x += Utilities.getTabbedTextWidth(segment, metrics,
635                                                             alloc.x,
636                                                             WrappedPlainView.this, p0);
637                     SegmentCache.releaseSharedSegment(segment);
638                     return alloc;
639                 }
640         if (p == p1 && pos == p1) {
641             // Wants end.
642
if (pos > p0) {
643                         Segment JavaDoc segment = SegmentCache.getSharedSegment();
644             loadText(segment, p0, pos);
645             alloc.x += Utilities.getTabbedTextWidth(segment,
646                          metrics, alloc.x,
647                          WrappedPlainView.this, p0);
648                         SegmentCache.releaseSharedSegment(segment);
649             }
650                     return alloc;
651         }
652                 p0 = (p == p0) ? p1 : p;
653                 alloc.y += alloc.height;
654             }
655             throw new BadLocationException JavaDoc(null, pos);
656         }
657
658         /**
659          * Provides a mapping from the view coordinate space to the logical
660          * coordinate space of the model.
661          *
662          * @param x the X coordinate
663          * @param y the Y coordinate
664          * @param a the allocated region to render into
665          * @return the location within the model that best represents the
666          * given point in the view
667          * @see View#viewToModel
668          */

669         public int viewToModel(float fx, float fy, Shape a, Position.Bias JavaDoc[] bias) {
670         // PENDING(prinz) implement bias properly
671
bias[0] = Position.Bias.Forward;
672
673         Rectangle alloc = (Rectangle) a;
674         Document JavaDoc doc = getDocument();
675         int x = (int) fx;
676         int y = (int) fy;
677         if (y < alloc.y) {
678         // above the area covered by this icon, so the the position
679
// is assumed to be the start of the coverage for this view.
680
return getStartOffset();
681         } else if (y > alloc.y + alloc.height) {
682         // below the area covered by this icon, so the the position
683
// is assumed to be the end of the coverage for this view.
684
return getEndOffset() - 1;
685         } else {
686         // positioned within the coverage of this view vertically,
687
// so we figure out which line the point corresponds to.
688
// if the line is greater than the number of lines contained, then
689
// simply use the last line as it represents the last possible place
690
// we can position to.
691
alloc.height = metrics.getHeight();
692         int p1 = getEndOffset();
693         for (int p0 = getStartOffset(); p0 < p1; ) {
694             int p = calculateBreakPosition(p0, p1);
695             if ((y >= alloc.y) && (y < (alloc.y + alloc.height))) {
696             // it's in this line
697
if (x < alloc.x) {
698                 // point is to the left of the line
699
return p0;
700             } else if (x > alloc.x + alloc.width) {
701                 // point is to the right of the line
702
return p - 1;
703             } else {
704                 // Determine the offset into the text
705
Segment JavaDoc segment = SegmentCache.getSharedSegment();
706                             loadText(segment, p0, p1);
707                 int n = Utilities.getTabbedTextOffset(segment, metrics,
708                                     alloc.x, x,
709                                     WrappedPlainView.this, p0);
710                             SegmentCache.releaseSharedSegment(segment);
711                 return Math.min(p0 + n, p1 - 1);
712             }
713             }
714             
715             p0 = (p == p0) ? p1 : p;
716             alloc.y += alloc.height;
717         }
718         return getEndOffset() - 1;
719         }
720     }
721
722         public void insertUpdate(DocumentEvent e, Shape a, ViewFactory JavaDoc f) {
723         int n = calculateLineCount();
724         if (this.nlines != n) {
725         this.nlines = n;
726         WrappedPlainView.this.preferenceChanged(this, false, true);
727         // have to repaint any views after the receiver.
728
getContainer().repaint();
729         }
730         else if (a != null) {
731                 Component c = getContainer();
732                 Rectangle alloc = (Rectangle) a;
733                 c.repaint(alloc.x, alloc.y, alloc.width, alloc.height);
734             }
735         }
736
737         public void removeUpdate(DocumentEvent e, Shape a, ViewFactory JavaDoc f) {
738         int n = calculateLineCount();
739         if (this.nlines != n) {
740         // have to repaint any views after the receiver.
741
this.nlines = n;
742         WrappedPlainView.this.preferenceChanged(this, false, true);
743         getContainer().repaint();
744         }
745         else if (a != null) {
746                 Component c = getContainer();
747                 Rectangle alloc = (Rectangle) a;
748                 c.repaint(alloc.x, alloc.y, alloc.width, alloc.height);
749             }
750         }
751
752         // --- variables ---------------------------------------
753

754         int nlines;
755     }
756     
757 }
758
759
Popular Tags