summaryrefslogtreecommitdiff
path: root/javax
diff options
context:
space:
mode:
authorRobert Schuster <theBohemian@gmx.net>2006-02-22 11:35:17 +0000
committerRobert Schuster <theBohemian@gmx.net>2006-02-22 11:35:17 +0000
commit1f25c9aa3b23c6fb5ae852404d94fba169a9ccb7 (patch)
tree3568aab4af70563ed8bef3f13e4fd386138ab5a2 /javax
parentd01c195ab4c90ad638f2b03b167ddb34763c66d3 (diff)
downloadclasspath-1f25c9aa3b23c6fb5ae852404d94fba169a9ccb7.tar.gz
Fixes PR #26157.
2006-02-22 Robert Schuster <robertschuster@fsfe.org> * javax/swing/text/PlainDocument.java: (getPreferredSpan): Added missing 'break'. statement which corrects an unwanted fall through. (updateDamage): Update maxLineLength correctly when text is removed, call preferenceChanged accordingly. (viewToModel): Restrict line number to be within 0 and the number of elements-1.
Diffstat (limited to 'javax')
-rw-r--r--javax/swing/text/PlainView.java54
1 files changed, 45 insertions, 9 deletions
diff --git a/javax/swing/text/PlainView.java b/javax/swing/text/PlainView.java
index 49ed34aa5..2eec7af54 100644
--- a/javax/swing/text/PlainView.java
+++ b/javax/swing/text/PlainView.java
@@ -1,5 +1,5 @@
/* PlainView.java --
- Copyright (C) 2004, 2005 Free Software Foundation, Inc.
+ Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -307,18 +307,20 @@ public class PlainView extends View implements TabExpander
// make sure we have the metrics
updateMetrics();
- float span = 0;
Element el = getElement();
+ float span;
switch (axis)
{
case X_AXIS:
span = determineMaxLineLength();
+ break;
case Y_AXIS:
default:
span = metrics.getHeight() * el.getElementCount();
break;
}
+
return span;
}
@@ -341,12 +343,19 @@ public class PlainView extends View implements TabExpander
Element root = doc.getDefaultRootElement();
// PlainView doesn't support line-wrapping so we can find out which
- // Element was clicked on just by the y-position
- int lineClicked = (int) (y - rec.y) / metrics.getHeight();
- if (lineClicked >= root.getElementCount())
- return getEndOffset() - 1;
+ // Element was clicked on just by the y-position.
+ // Since the coordinates may be outside of the coordinate space
+ // of the allocation area (e.g. user dragged mouse outside
+ // the component) we have to limit the values.
+ // This has the nice effect that the user can drag the
+ // mouse above or below the component and it will still
+ // react to the x values (e.g. when selecting).
+ int lineClicked
+ = Math.min(Math.max((int) (y - rec.y) / metrics.getHeight(), 0),
+ root.getElementCount() - 1);
Element line = root.getElement(lineClicked);
+
Segment s = getLineBuffer();
int start = line.getStartOffset();
// We don't want the \n at the end of the line.
@@ -376,6 +385,8 @@ public class PlainView extends View implements TabExpander
*/
protected void updateDamage(DocumentEvent changes, Shape a, ViewFactory f)
{
+ float oldMaxLineLength = maxLineLength;
+ Rectangle alloc = a.getBounds();
Element el = getElement();
ElementChange ec = changes.getChange(el);
@@ -383,7 +394,19 @@ public class PlainView extends View implements TabExpander
// repaint the changed line
if (ec == null)
{
- int line = getElement().getElementIndex(changes.getOffset());
+ int line = el.getElementIndex(changes.getOffset());
+
+ // If characters have been removed from the current longest line
+ // we have to find out which one is the longest now otherwise
+ // the preferred x-axis span will not shrink.
+ if (changes.getType() == DocumentEvent.EventType.REMOVE
+ && el.getElement(line) == longestLine)
+ {
+ maxLineLength = -1;
+ if (determineMaxLineLength() != alloc.width)
+ preferenceChanged(this, true, false);
+ }
+
damageLineRange(line, line, a, getContainer());
return;
}
@@ -396,12 +419,13 @@ public class PlainView extends View implements TabExpander
if (removed == null && newElements == null)
{
int line = getElement().getElementIndex(changes.getOffset());
+
damageLineRange(line, line, a, getContainer());
return;
}
// Check to see if we removed the longest line, if so we have to
- // search through all lines and find the longest one again
+ // search through all lines and find the longest one again.
if (removed != null)
{
for (int i = 0; i < removed.length; i++)
@@ -409,8 +433,11 @@ public class PlainView extends View implements TabExpander
{
// reset maxLineLength and search through all lines for longest one
maxLineLength = -1;
- determineMaxLineLength();
+ if (determineMaxLineLength() != alloc.width)
+ preferenceChanged(this, true, removed.length != newElements.length);
+
((JTextComponent)getContainer()).repaint();
+
return;
}
}
@@ -420,6 +447,7 @@ public class PlainView extends View implements TabExpander
{
// No lines were added, just repaint the container and exit
((JTextComponent)getContainer()).repaint();
+
return;
}
@@ -468,6 +496,14 @@ public class PlainView extends View implements TabExpander
maxLineLength = longestNewLength;
longestLine = longestNewLine;
}
+
+ // Report any changes to the preferred sizes of the view
+ // which may cause the underlying component to be revalidated.
+ boolean widthChanged = oldMaxLineLength != maxLineLength;
+ boolean heightChanged = removed.length != newElements.length;
+ if (widthChanged || heightChanged)
+ preferenceChanged(this, widthChanged, heightChanged);
+
// Repaint the container
((JTextComponent)getContainer()).repaint();
}