diff options
author | Robert Schuster <theBohemian@gmx.net> | 2006-02-07 15:38:51 +0000 |
---|---|---|
committer | Robert Schuster <theBohemian@gmx.net> | 2006-02-07 15:38:51 +0000 |
commit | 3cda57b54475ea5a9f9702076232f11056ed0125 (patch) | |
tree | 1bcc77a0d26b7a5b16ea475e3cca7049bf401494 /javax/swing/text/DefaultEditorKit.java | |
parent | c6871d853ad0db9a8c2c04c3ece9e05c14e2739e (diff) | |
download | classpath-3cda57b54475ea5a9f9702076232f11056ed0125.tar.gz |
Adds new selection features to text components and fixes PR #26033.
2006-02-07 Robert Schuster <robertschuster@fsfe.org>
* javax/swing/text/DefaultEditorToolkit.java: Changed behavior
of actions "delete-next" and "delete-previous", added new TextAction
implementations for "selection-begin", "selection-begin-line",
"selection-end" and "selection-end-line".
Diffstat (limited to 'javax/swing/text/DefaultEditorKit.java')
-rw-r--r-- | javax/swing/text/DefaultEditorKit.java | 94 |
1 files changed, 85 insertions, 9 deletions
diff --git a/javax/swing/text/DefaultEditorKit.java b/javax/swing/text/DefaultEditorKit.java index 88094b898..e5097a59a 100644 --- a/javax/swing/text/DefaultEditorKit.java +++ b/javax/swing/text/DefaultEditorKit.java @@ -756,11 +756,15 @@ public class DefaultEditorKit extends EditorKit { try { - int pos = t.getCaret().getDot(); - if (pos < t.getDocument().getEndPosition().getOffset()) - { - t.getDocument().remove(t.getCaret().getDot(), 1); - } + int pos = t.getSelectionStart(); + int len = t.getSelectionEnd() - pos; + + if (len > 0) + t.getDocument().remove(pos, len); + else if (pos < t.getDocument().getLength()) + t.getDocument().remove(pos, 1); + + t.setCaretPosition(pos); } catch (BadLocationException e) { @@ -778,12 +782,16 @@ public class DefaultEditorKit extends EditorKit { try { - int pos = t.getCaret().getDot(); - if (pos > t.getDocument().getStartPosition().getOffset()) + int pos = t.getSelectionStart(); + int len = t.getSelectionEnd() - pos; + + if (len > 0) { - t.getDocument().remove(pos - 1, 1); - t.getCaret().setDot(pos - 1); + t.getDocument().remove(pos, len); + t.setCaretPosition(pos); } + else if (pos > 0) + t.getDocument().remove(pos - 1, 1); } catch (BadLocationException e) { @@ -840,6 +848,74 @@ public class DefaultEditorKit extends EditorKit } } }, + new TextAction(selectionBeginLineAction) + { + public void actionPerformed(ActionEvent event) + { + 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.getCaret().moveDot(cur); + } + catch (BadLocationException ble) + { + // Do nothing here. + } + } + }, + new TextAction(selectionEndLineAction) + { + public void actionPerformed(ActionEvent event) + { + JTextComponent t = getTextComponent(event); + 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.moveCaretPosition(cur); + } + catch (BadLocationException ble) + { + // Nothing to do here + } + } + }, + new TextAction(selectionEndAction) + { + public void actionPerformed(ActionEvent event) + { + JTextComponent t = getTextComponent(event); + t.moveCaretPosition(t.getDocument().getLength()); + } + }, + new TextAction(selectionBeginAction) + { + public void actionPerformed(ActionEvent event) + { + JTextComponent t = getTextComponent(event); + t.moveCaretPosition(0); + } + } }; /** |