diff options
author | Robert Schuster <theBohemian@gmx.net> | 2006-04-24 14:59:10 +0000 |
---|---|---|
committer | Robert Schuster <theBohemian@gmx.net> | 2006-04-24 14:59:10 +0000 |
commit | cbb50dfd2cc4dee88a776db0c9aed8855a3b0af3 (patch) | |
tree | d922f1f29c2252e0b41e3e6da6399f11d25eb2d9 | |
parent | 636bbf05cb4b7dc32558741e4191a96bb89bdb81 (diff) | |
download | classpath-cbb50dfd2cc4dee88a776db0c9aed8855a3b0af3.tar.gz |
2006-04-24 Robert Schuster <robertschuster@fsfe.org>
* javax/swing/text/Utilities.java:
(getBreakLocation): Introduced shift variable, added notes.
* javax/swing/text/WrappedPlainView.java:
(calculateBreakPosition): Decrease allocation area bounds by insets,
added early return when allocation area is empty, provide start offset
as argument.
(WrappedPlainView.WrappedLine): Change default value for numLines to 1.
(WrappedPlainView.WrappedLine.paint): Added count variable, update
numLines after loop.
(WrappedPlainView.WrappedLine.determineNumLines): Added early return.
(WrappedPlainView.WrappedLine.getPreferredSpan): Removed if-statement.
(WrappedPlainView.WrappedLine.viewToModel): Changed note, removed
decreasing variable end by one, changed break condition in while-loop,
added check for return value.
(WrappedPlainView.WrappedLine.updateDamage): Set numLines to one if
allocation area is empty.
-rw-r--r-- | ChangeLog | 19 | ||||
-rw-r--r-- | javax/swing/text/Utilities.java | 22 | ||||
-rw-r--r-- | javax/swing/text/WrappedPlainView.java | 72 |
3 files changed, 85 insertions, 28 deletions
@@ -1,3 +1,22 @@ +2006-04-24 Robert Schuster <robertschuster@fsfe.org> + + * javax/swing/text/Utilities.java: + (getBreakLocation): Introduced shift variable, added notes. + * javax/swing/text/WrappedPlainView.java: + (calculateBreakPosition): Decrease allocation area bounds by insets, + added early return when allocation area is empty, provide start offset + as argument. + (WrappedPlainView.WrappedLine): Change default value for numLines to 1. + (WrappedPlainView.WrappedLine.paint): Added count variable, update + numLines after loop. + (WrappedPlainView.WrappedLine.determineNumLines): Added early return. + (WrappedPlainView.WrappedLine.getPreferredSpan): Removed if-statement. + (WrappedPlainView.WrappedLine.viewToModel): Changed note, removed + decreasing variable end by one, changed break condition in while-loop, + added check for return value. + (WrappedPlainView.WrappedLine.updateDamage): Set numLines to one if + allocation area is empty. + 2006-04-24 Sven de Marothy <sven@physto.se> * gnu/java/awt/java2d/Segment.java: New file. diff --git a/javax/swing/text/Utilities.java b/javax/swing/text/Utilities.java index 3149048f7..8abb9523a 100644 --- a/javax/swing/text/Utilities.java +++ b/javax/swing/text/Utilities.java @@ -515,20 +515,24 @@ public class Utilities BreakIterator breaker = BreakIterator.getWordInstance(); breaker.setText(s); - // If mark is equal to the end of the string, just use that position - if (mark >= s.count) + // If startOffset and s.offset differ then we need to use + // that difference two convert the offset between the two metrics. + int shift = startOffset - s.offset; + + // If mark is equal to the end of the string, just use that position. + if (mark >= shift + s.count) return mark; // Try to find a word boundary previous to the mark at which we - // can break the text - int preceding = breaker.preceding(mark + 1); + // can break the text. + int preceding = breaker.preceding(mark + 1 - shift); if (preceding != 0) - return preceding; - else - // If preceding is 0 we couldn't find a suitable word-boundary so - // just break it on the character boundary - return mark; + return preceding + shift; + + // If preceding is 0 we couldn't find a suitable word-boundary so + // just break it on the character boundary + return mark; } /** diff --git a/javax/swing/text/WrappedPlainView.java b/javax/swing/text/WrappedPlainView.java index b16376221..1cd85b515 100644 --- a/javax/swing/text/WrappedPlainView.java +++ b/javax/swing/text/WrappedPlainView.java @@ -275,7 +275,17 @@ public class WrappedPlainView extends BoxView implements TabExpander protected int calculateBreakPosition(int p0, int p1) { Container c = getContainer(); - Rectangle alloc = new Rectangle(0, 0, getWidth(), getHeight()); + + int li = getLeftInset(); + int ti = getTopInset(); + + Rectangle alloc = new Rectangle(li, ti, + getWidth()-getRightInset()-li, + getHeight()-getBottomInset()-ti); + + // Mimic a behavior observed in the RI. + if (alloc.isEmpty()) + return 0; updateMetrics(); @@ -288,10 +298,10 @@ public class WrappedPlainView extends BoxView implements TabExpander // this shouldn't happen throw new InternalError("Invalid offsets p0: " + p0 + " - p1: " + p1); } - // FIXME: Should we account for the insets of the container? + if (wordWrap) - return p0 + Utilities.getBreakLocation(lineBuffer, metrics, alloc.x, - alloc.x + alloc.width, this, 0); + return Utilities.getBreakLocation(lineBuffer, metrics, alloc.x, + alloc.x + alloc.width, this, p0); else return p0 + Utilities.getTabbedTextOffset(lineBuffer, metrics, alloc.x, alloc.x + alloc.width, this, 0, @@ -419,7 +429,7 @@ public class WrappedPlainView extends BoxView implements TabExpander class WrappedLine extends View { /** Used to cache the number of lines for this View **/ - int numLines = -1; + int numLines = 1; public WrappedLine(Element elem) { @@ -437,17 +447,30 @@ public class WrappedPlainView extends BoxView implements TabExpander int end = getEndOffset(); int currStart = getStartOffset(); - int currEnd; + int currEnd; + int count = 0; while (currStart < end) { currEnd = calculateBreakPosition(currStart, end); + drawLine(currStart, currEnd, g, rect.x, rect.y + metrics.getAscent()); + rect.y += lineHeight; if (currEnd == currStart) currStart ++; else - currStart = currEnd; + currStart = currEnd; + + count++; + + } + + if (count != numLines) + { + numLines = count; + preferenceChanged(this, false, true); } + } /** @@ -470,6 +493,9 @@ public class WrappedPlainView extends BoxView implements TabExpander // depending on which position calculateBreakPosition returns breakPoint = calculateBreakPosition(i, end); + if (breakPoint == 0) + return; + // If breakPoint is equal to the current index no further // line is needed and we can end the loop. if (breakPoint == i) @@ -489,14 +515,10 @@ public class WrappedPlainView extends BoxView implements TabExpander */ public float getPreferredSpan(int axis) { - // numLines may be invalid at this point and needs to be calculated. - if (numLines == -1) - determineNumLines(); - if (axis == X_AXIS) return getWidth(); else if (axis == Y_AXIS) - return numLines * metrics.getHeight(); + return numLines * metrics.getHeight(); throw new IllegalArgumentException("Invalid axis for getPreferredSpan: " + axis); @@ -582,13 +604,15 @@ public class WrappedPlainView extends BoxView implements TabExpander Rectangle rect = a.getBounds(); int currLineStart = getStartOffset(); - // WrappedLine does not represent the last possible offset in a line. - // So we should never return that offset. Behavior observed in the RI. - int end = getEndOffset() - 1; + // Although calling modelToView with the last possible offset will + // cause a BadLocationException in CompositeView it is allowed + // to return that offset in viewToModel. + int end = getEndOffset(); int lineHeight = metrics.getHeight(); if (y < rect.y) return currLineStart; + if (y > rect.y + rect.height) return end; @@ -596,9 +620,10 @@ public class WrappedPlainView extends BoxView implements TabExpander // text but the area where text *may* be painted. This means the width // is most of the time identical to the component's width. - while (true) + while (currLineStart != end) { int currLineEnd = calculateBreakPosition(currLineStart, end); + // If we're at the right y-position that means we're on the right // logical line and we should look for the character if (y >= rect.y && y < rect.y + lineHeight) @@ -612,18 +637,27 @@ public class WrappedPlainView extends BoxView implements TabExpander // Shouldn't happen } - return Utilities.getTabbedTextOffset(s, metrics, rect.x, + int offset = Utilities.getTabbedTextOffset(s, metrics, rect.x, (int) x, WrappedPlainView.this, currLineStart); + // If the calculated offset is the end of the line (in the + // document (= start of the next line) return the preceding + // offset instead. This makes sure that clicking right besides + // the last character in a line positions the cursor after the + // last character and not in the beginning of the next line. + return (offset == currLineEnd) ? offset - 1 : offset; } // Increment rect.y so we're checking the next logical line rect.y += lineHeight; // Increment currLineStart to the model position of the start - // of the next logical line + // of the next logical line. currLineStart = currLineEnd; + } + + return end; } /** @@ -642,7 +676,7 @@ public class WrappedPlainView extends BoxView implements TabExpander // later point. if (a.isEmpty()) { - numLines = -1; + numLines = 1; return; } |