diff options
author | Andrew John Hughes <gnu_andrew@member.fsf.org> | 2006-04-14 06:50:23 +0000 |
---|---|---|
committer | Andrew John Hughes <gnu_andrew@member.fsf.org> | 2006-04-14 06:50:23 +0000 |
commit | fe67a5ac72d12dd8faf471d1c612492fed829a4b (patch) | |
tree | e63047006842dfddf0388368324211987cc15509 /javax/swing/text/WrappedPlainView.java | |
parent | 71fa4db0b20e97806bd5b11d96370695112f21b4 (diff) | |
download | classpath-fe67a5ac72d12dd8faf471d1c612492fed829a4b.tar.gz |
2006-04-13 Andrew John Hughes <gnu_andrew@member.fsf.org>
* Merge of HEAD --> generics-branch from 2006/03/26 to 2006/04/13.
Diffstat (limited to 'javax/swing/text/WrappedPlainView.java')
-rw-r--r-- | javax/swing/text/WrappedPlainView.java | 130 |
1 files changed, 69 insertions, 61 deletions
diff --git a/javax/swing/text/WrappedPlainView.java b/javax/swing/text/WrappedPlainView.java index 3f65dd407..9477f23f7 100644 --- a/javax/swing/text/WrappedPlainView.java +++ b/javax/swing/text/WrappedPlainView.java @@ -1,5 +1,5 @@ /* WrappedPlainView.java -- - Copyright (C) 2005 Free Software Foundation, Inc. + Copyright (C) 2005, 2006 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -81,6 +81,9 @@ public class WrappedPlainView extends BoxView implements TabExpander /** The end of the selected text **/ int selectionEnd; + /** The height of the line (used while painting) **/ + int lineHeight; + /** * The instance returned by {@link #getLineBuffer()}. */ @@ -273,7 +276,9 @@ public class WrappedPlainView extends BoxView implements TabExpander { Container c = getContainer(); Rectangle alloc = new Rectangle(0, 0, getWidth(), getHeight()); + updateMetrics(); + try { getDocument().getText(p0, p1 - p0, getLineBuffer()); @@ -281,18 +286,16 @@ public class WrappedPlainView extends BoxView implements TabExpander catch (BadLocationException ble) { // 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, + return p0 + Utilities.getBreakLocation(lineBuffer, metrics, alloc.x, alloc.x + alloc.width, this, 0); else - { - return p0 - + Utilities.getTabbedTextOffset(lineBuffer, metrics, alloc.x, - alloc.x + alloc.width, this, 0); - } + return p0 + Utilities.getTabbedTextOffset(lineBuffer, metrics, alloc.x, + alloc.x + alloc.width, this, 0, + true); } void updateMetrics() @@ -338,8 +341,8 @@ public class WrappedPlainView extends BoxView implements TabExpander public void insertUpdate (DocumentEvent e, Shape a, ViewFactory f) { super.insertUpdate(e, a, viewFactory); - // FIXME: could improve performance by repainting only the necessary area - getContainer().repaint(); + + // No repaint needed, as this is done by the WrappedLine instances. } /** @@ -349,8 +352,8 @@ public class WrappedPlainView extends BoxView implements TabExpander public void removeUpdate (DocumentEvent e, Shape a, ViewFactory f) { super.removeUpdate(e, a, viewFactory); - // FIXME: could improve performance by repainting only the necessary area - getContainer().repaint(); + + // No repaint needed, as this is done by the WrappedLine instances. } /** @@ -361,8 +364,8 @@ public class WrappedPlainView extends BoxView implements TabExpander public void changedUpdate (DocumentEvent e, Shape a, ViewFactory f) { super.changedUpdate(e, a, viewFactory); - // FIXME: could improve performance by repainting only the necessary area - getContainer().repaint(); + + // No repaint needed, as this is done by the WrappedLine instances. } class WrappedLineCreator implements ViewFactory @@ -385,9 +388,19 @@ public class WrappedPlainView extends BoxView implements TabExpander public void paint(Graphics g, Shape a) { JTextComponent comp = (JTextComponent)getContainer(); + // Ensure metrics are up-to-date. + updateMetrics(); + selectionStart = comp.getSelectionStart(); selectionEnd = comp.getSelectionEnd(); - updateMetrics(); + + selectedColor = comp.getSelectedTextColor(); + unselectedColor = comp.getForeground(); + disabledColor = comp.getDisabledTextColor(); + selectedColor = comp.getSelectedTextColor(); + lineHeight = metrics.getHeight(); + g.setFont(comp.getFont()); + super.paint(g, a); } @@ -420,21 +433,7 @@ public class WrappedPlainView extends BoxView implements TabExpander */ public void paint(Graphics g, Shape s) { - // Ensure metrics are up-to-date. - updateMetrics(); - JTextComponent textComponent = (JTextComponent) getContainer(); - - g.setFont(textComponent.getFont()); - selectedColor = textComponent.getSelectedTextColor(); - unselectedColor = textComponent.getForeground(); - disabledColor = textComponent.getDisabledTextColor(); - - // FIXME: this is a hack, for some reason textComponent.getSelectedColor - // was returning black, which is not visible against a black background - selectedColor = Color.WHITE; - Rectangle rect = s.getBounds(); - int lineHeight = metrics.getHeight(); int end = getEndOffset(); int currStart = getStartOffset(); @@ -452,16 +451,16 @@ public class WrappedPlainView extends BoxView implements TabExpander } /** - * Determines the number of logical lines that the Element - * needs to be displayed - * @return the number of lines needed to display the Element + * Calculates the number of logical lines that the Element + * needs to be displayed and updates the variable numLines + * accordingly. */ - int determineNumLines() + void determineNumLines() { numLines = 0; int end = getEndOffset(); if (end == 0) - return 0; + return; int breakPoint; for (int i = getStartOffset(); i < end;) @@ -470,12 +469,14 @@ public class WrappedPlainView extends BoxView implements TabExpander // careful: check that there's no off-by-one problem here // depending on which position calculateBreakPosition returns breakPoint = calculateBreakPosition(i, end); + + // If breakPoint is equal to the current index no further + // line is needed and we can end the loop. if (breakPoint == i) - i ++; + break; else i = breakPoint; } - return numLines; } /** @@ -532,7 +533,7 @@ public class WrappedPlainView extends BoxView implements TabExpander // If pos is between currLineStart and currLineEnd then just find // the width of the text from currLineStart to pos and add that // to rect.x - if (pos >= currLineStart && pos < currLineEnd || pos == end - 1) + if (pos >= currLineStart && pos < currLineEnd) { try { @@ -576,12 +577,20 @@ public class WrappedPlainView extends BoxView implements TabExpander Segment s = getLineBuffer(); Rectangle rect = a.getBounds(); int currLineStart = getStartOffset(); - int end = getEndOffset(); + + // 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; + int lineHeight = metrics.getHeight(); if (y < rect.y) return currLineStart; if (y > rect.y + rect.height) - return end - 1; + return end; + + // Note: rect.x and rect.width do not represent the width of painted + // 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) { @@ -590,35 +599,26 @@ public class WrappedPlainView extends BoxView implements TabExpander // logical line and we should look for the character if (y >= rect.y && y < rect.y + lineHeight) { - // Check if the x position is to the left or right of the text - if (x < rect.x) - return currLineStart; - if (x > rect.x + rect.width) - return currLineEnd - 1; - try { - getDocument().getText(currLineStart, end - currLineStart, s); + getDocument().getText(currLineStart, currLineEnd - currLineStart, s); } catch (BadLocationException ble) { // Shouldn't happen } - int mark = Utilities.getTabbedTextOffset(s, metrics, rect.x, - (int) x, - WrappedPlainView.this, - currLineStart); - return currLineStart + mark; + + return Utilities.getTabbedTextOffset(s, metrics, rect.x, + (int) x, + WrappedPlainView.this, + currLineStart); } // 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 - if (currLineEnd == currLineStart) - currLineStart = end; - else - currLineStart = currLineEnd; + currLineStart = currLineEnd; } } @@ -632,12 +632,11 @@ public class WrappedPlainView extends BoxView implements TabExpander */ void updateDamage (Rectangle a) { - int newNumLines = determineNumLines(); - if (numLines != newNumLines) - { - numLines = newNumLines; - getContainer().repaint(); - } + int oldNumLines = numLines; + determineNumLines(); + + if (numLines != oldNumLines) + preferenceChanged(this, false, true); else getContainer().repaint(a.x, a.y, a.width, a.height); } @@ -665,6 +664,15 @@ public class WrappedPlainView extends BoxView implements TabExpander */ public void removeUpdate (DocumentEvent changes, Shape a, ViewFactory f) { + // Note: This method is not called when characters from the + // end of the document are removed. The reason for this + // can be found in the implementation of View.forwardUpdate: + // The document event will denote offsets which do not exist + // any more, getViewIndex() will therefore return -1 and this + // makes View.forwardUpdate() skip this method call. + // However this seems to cause no trouble and as it reduces the + // number of method calls it can stay this way. + updateDamage((Rectangle)a); } } |