Hi, with this patch JTextArea's caret learns to move and select up and down correctly. The patch mainly adds code that updates the so called magic caret position.
In the respective actions this properties value is then used to determine the
new location.
The actions for up & down selections are, the other ones have been tweaked only
(to make use of the viewToModel related fix I committed a few days ago).
Please comment.
The ChangeLog:
2006-02-15 Robert Schuster <[EMAIL PROTECTED]>
* javax/swing/text/JTextComponent.java:
(replaceSelection): Added code to update the magic caret position.
* javax/swing/text/DefaultEditorKit.java: Added code to update
the magic caret position of the text component in all relevant
movement actions, make use of the magic caret position in up
and down movements and selections, simplified some actions
(code-wise).
cya
Robert
Index: javax/swing/text/DefaultEditorKit.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/DefaultEditorKit.java,v
retrieving revision 1.28
diff -u -r1.28 DefaultEditorKit.java
--- javax/swing/text/DefaultEditorKit.java 7 Feb 2006 15:38:51 -0000 1.28
+++ javax/swing/text/DefaultEditorKit.java 15 Feb 2006 11:30:19 -0000
@@ -707,16 +707,14 @@
JTextComponent t = getTextComponent(event);
try
{
- // TODO: There is a more efficent solution, but
- // viewToModel doesn't work properly.
- Point p = t.modelToView(t.getCaret().getDot()).getLocation();
- int cur = t.getCaretPosition();
- int y = p.y;
- while (y == p.y && cur > 0)
- y = t.modelToView(--cur).getLocation().y;
- if (cur != 0)
- cur++;
- t.setCaretPosition(cur);
+ int offs = Utilities.getRowStart(t, t.getCaretPosition());
+
+ if (offs > -1)
+ {
+ Caret c = t.getCaret();
+ c.setDot(offs);
+ c.setMagicCaretPosition(t.modelToView(offs).getLocation());
+ }
}
catch (BadLocationException ble)
{
@@ -729,17 +727,16 @@
public void actionPerformed(ActionEvent event)
{
JTextComponent t = getTextComponent(event);
- try
+ try
{
- Point p = t.modelToView(t.getCaret().getDot()).getLocation();
- int cur = t.getCaretPosition();
- int y = p.y;
- int length = t.getDocument().getLength();
- while (y == p.y && cur < length)
- y = t.modelToView(++cur).getLocation().y;
- if (cur != length)
- cur--;
- t.setCaretPosition(cur);
+ int offs = Utilities.getRowEnd(t, t.getCaretPosition());
+
+ if (offs > -1)
+ {
+ Caret c = t.getCaret();
+ c.setDot(offs);
+ c.setMagicCaretPosition(t.modelToView(offs).getLocation());
+ }
}
catch (BadLocationException ble)
{
@@ -764,7 +761,9 @@
else if (pos < t.getDocument().getLength())
t.getDocument().remove(pos, 1);
- t.setCaretPosition(pos);
+ Caret c = t.getCaret();
+ c.setDot(pos);
+ c.setMagicCaretPosition(t.modelToView(pos).getLocation());
}
catch (BadLocationException e)
{
@@ -786,12 +785,15 @@
int len = t.getSelectionEnd() - pos;
if (len > 0)
- {
t.getDocument().remove(pos, len);
- t.setCaretPosition(pos);
- }
else if (pos > 0)
- t.getDocument().remove(pos - 1, 1);
+ {
+ pos--;
+ t.getDocument().remove(pos, 1);
+ Caret c = t.getCaret();
+ c.setDot(pos);
+ c.setMagicCaretPosition(t.modelToView(pos).getLocation());
+ }
}
catch (BadLocationException e)
{
@@ -807,8 +809,21 @@
JTextComponent t = getTextComponent(event);
if (t != null)
{
- t.getCaret().setDot(Math.max(t.getCaret().getDot() - 1,
- t.getDocument().getStartPosition().getOffset()));
+ int offs = t.getCaretPosition() - 1;
+ if (offs >= 0)
+ {
+ Caret c = t.getCaret();
+ c.setDot(offs);
+
+ try
+ {
+ c.setMagicCaretPosition(t.modelToView(offs).getLocation());
+ }
+ catch (BadLocationException ble)
+ {
+ // Should not happen.
+ }
+ }
}
}
},
@@ -819,8 +834,68 @@
JTextComponent t = getTextComponent(event);
if (t != null)
{
- t.getCaret().setDot(Math.min(t.getCaret().getDot() + 1,
- t.getDocument().getEndPosition().getOffset()));
+ int offs = t.getCaretPosition() + 1;
+ if (offs <= t.getDocument().getLength())
+ {
+ Caret c = t.getCaret();
+ c.setDot(offs);
+
+ try
+ {
+ c.setMagicCaretPosition(t.modelToView(offs).getLocation());
+ }
+ catch (BadLocationException ble)
+ {
+ // Should not happen.
+ }
+ }
+ }
+
+ }
+ },
+ new TextAction(upAction)
+ {
+ public void actionPerformed(ActionEvent event)
+ {
+ JTextComponent t = getTextComponent(event);
+ try
+ {
+ if (t != null)
+ {
+ Caret c = t.getCaret();
+ int x = c.getMagicCaretPosition().x;
+ int pos = Utilities.getPositionAbove(t, t.getCaretPosition(), x);
+
+ if (pos > -1)
+ t.setCaretPosition(pos);
+ }
+ }
+ catch(BadLocationException ble)
+ {
+ // FIXME: Swallowing allowed?
+ }
+ }
+ },
+ new TextAction(downAction)
+ {
+ public void actionPerformed(ActionEvent event)
+ {
+ JTextComponent t = getTextComponent(event);
+ try
+ {
+ if (t != null)
+ {
+ Caret c = t.getCaret();
+ int x = c.getMagicCaretPosition().x;
+ int pos = Utilities.getPositionBelow(t, t.getCaretPosition(), x);
+
+ if (pos > -1)
+ t.setCaretPosition(pos);
+ }
+ }
+ catch(BadLocationException ble)
+ {
+ // FIXME: Swallowing allowed?
}
}
},
@@ -831,8 +906,21 @@
JTextComponent t = getTextComponent(event);
if (t != null)
{
- t.getCaret().moveDot(Math.max(t.getCaret().getDot() - 1,
- t.getDocument().getStartPosition().getOffset()));
+ int offs = t.getCaretPosition() - 1;
+
+ if(offs > 0)
+ {
+ Caret c = t.getCaret();
+ c.moveDot(offs);
+ try
+ {
+ c.setMagicCaretPosition(t.modelToView(offs).getLocation());
+ }
+ catch(BadLocationException ble)
+ {
+ // Can't happen.
+ }
+ }
}
}
},
@@ -843,8 +931,67 @@
JTextComponent t = getTextComponent(event);
if (t != null)
{
- t.getCaret().moveDot(Math.min(t.getCaret().getDot() + 1,
- t.getDocument().getEndPosition().getOffset()));
+ int offs = t.getCaretPosition() + 1;
+
+ if(offs <= t.getDocument().getLength())
+ {
+ Caret c = t.getCaret();
+ c.moveDot(offs);
+ try
+ {
+ c.setMagicCaretPosition(t.modelToView(offs).getLocation());
+ }
+ catch(BadLocationException ble)
+ {
+ // Can't happen.
+ }
+ }
+ }
+ }
+ },
+ new TextAction(selectionUpAction)
+ {
+ public void actionPerformed(ActionEvent event)
+ {
+ JTextComponent t = getTextComponent(event);
+ try
+ {
+ if (t != null)
+ {
+ Caret c = t.getCaret();
+ int x = c.getMagicCaretPosition().x;
+ int pos = Utilities.getPositionAbove(t, t.getCaretPosition(), x);
+
+ if (pos > -1)
+ t.moveCaretPosition(pos);
+ }
+ }
+ catch(BadLocationException ble)
+ {
+ // FIXME: Swallowing allowed?
+ }
+ }
+ },
+ new TextAction(selectionDownAction)
+ {
+ public void actionPerformed(ActionEvent event)
+ {
+ JTextComponent t = getTextComponent(event);
+ try
+ {
+ if (t != null)
+ {
+ Caret c = t.getCaret();
+ int x = c.getMagicCaretPosition().x;
+ int pos = Utilities.getPositionBelow(t, t.getCaretPosition(), x);
+
+ if (pos > -1)
+ t.moveCaretPosition(pos);
+ }
+ }
+ catch(BadLocationException ble)
+ {
+ // FIXME: Swallowing allowed?
}
}
},
@@ -868,7 +1015,9 @@
if (cur != 0)
cur++;
- t.getCaret().moveDot(cur);
+ Caret c = t.getCaret();
+ c.moveDot(cur);
+ c.setMagicCaretPosition(t.modelToView(cur).getLocation());
}
catch (BadLocationException ble)
{
@@ -892,7 +1041,9 @@
if (cur != length)
cur--;
- t.moveCaretPosition(cur);
+ Caret c = t.getCaret();
+ c.moveDot(cur);
+ c.setMagicCaretPosition(t.modelToView(cur).getLocation());
}
catch (BadLocationException ble)
{
@@ -905,7 +1056,17 @@
public void actionPerformed(ActionEvent event)
{
JTextComponent t = getTextComponent(event);
- t.moveCaretPosition(t.getDocument().getLength());
+ int offs = t.getDocument().getLength();
+ Caret c = t.getCaret();
+ c.moveDot(offs);
+ try
+ {
+ c.setMagicCaretPosition(t.modelToView(offs).getLocation());
+ }
+ catch(BadLocationException ble)
+ {
+ // Can't happen.
+ }
}
},
new TextAction(selectionBeginAction)
@@ -913,7 +1074,16 @@
public void actionPerformed(ActionEvent event)
{
JTextComponent t = getTextComponent(event);
- t.moveCaretPosition(0);
+ Caret c = t.getCaret();
+ c.moveDot(0);
+ try
+ {
+ c.setMagicCaretPosition(t.modelToView(0).getLocation());
+ }
+ catch(BadLocationException ble)
+ {
+ // Can't happen.
+ }
}
}
};
Index: javax/swing/text/JTextComponent.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/JTextComponent.java,v
retrieving revision 1.52
diff -u -r1.52 JTextComponent.java
--- javax/swing/text/JTextComponent.java 7 Feb 2006 11:44:54 -0000 1.52
+++ javax/swing/text/JTextComponent.java 15 Feb 2006 11:30:19 -0000
@@ -1598,8 +1598,12 @@
// Insert new text.
doc.insertString(start, content, null);
- // Set dot to new position.
- setCaretPosition(start + content.length());
+ // Set dot to new position,
+ dot = start + content.length();
+ setCaretPosition(dot);
+
+ // and update it's magic position.
+ caret.setMagicCaretPosition(modelToView(dot).getLocation());
}
catch (BadLocationException e)
{
signature.asc
Description: OpenPGP digital signature
