diff options
author | Robert Schuster <theBohemian@gmx.net> | 2006-05-13 12:23:37 +0000 |
---|---|---|
committer | Robert Schuster <theBohemian@gmx.net> | 2006-05-13 12:23:37 +0000 |
commit | 5779f76011ec2c7c10e7fb91765eb8a766f58fb8 (patch) | |
tree | bd98b0eb2bdae2372b9433934b85a754f74a8313 /javax/swing/text/DefaultEditorKit.java | |
parent | 09d93ce4e542709e1b4a8f7a7bf97e3cc7166126 (diff) | |
download | classpath-5779f76011ec2c7c10e7fb91765eb8a766f58fb8.tar.gz |
Fixes PR27220.
2006-05-13 Robert Schuster <robertschuster@fsfe.org>
* javax/swing/text/TextAction.java:
(HorizontalMovementAction): New inner class.
(VerticalMovementAction): New inner class.
* javax/swing/text/DefaultEditorKit.java: Added assigning instances of
new inner classes to array 'defaultActions'.
(SelectionBeginWordAction): New inner class.
(SelectionEndWordAction): New inner class.
(BeginWordAction): New inner class.
(EndWordAction): New inner class.
(PreviousWordAction.actionPerformed): Rewritten.
(SelectLineAction): New inner class.
(SelectWordAction): New inner class.
(SelectionDownAction): Rewritten.
(SelectionUpAction): Rewritten.
(DownAction): Rewritten.
(UpAction): Rewritten.
(SelectionForwardAction): Rewritten.
(SelectionBackwardAction): Rewritten.
(ForwardAction): Rewritten.
(BackwardAction): Rewritten.
(BeginAction): New inner class.
(EndAction): New inner class.
(DefaultKeyTypedAction.actionPerformed): Use int variant of
Character.isISOControl.
Diffstat (limited to 'javax/swing/text/DefaultEditorKit.java')
-rw-r--r-- | javax/swing/text/DefaultEditorKit.java | 551 |
1 files changed, 326 insertions, 225 deletions
diff --git a/javax/swing/text/DefaultEditorKit.java b/javax/swing/text/DefaultEditorKit.java index 1b686182b..8602e69f8 100644 --- a/javax/swing/text/DefaultEditorKit.java +++ b/javax/swing/text/DefaultEditorKit.java @@ -52,6 +52,7 @@ import java.io.Reader; import java.io.Writer; import javax.swing.Action; +import javax.swing.SwingConstants; /** * The default implementation of {@link EditorKit}. This <code>EditorKit</code> @@ -60,6 +61,7 @@ import javax.swing.Action; * * @author original author unknown * @author Roman Kennke (roman@kennke.org) + * @author Robert Schuster (robertschuster@fsfe.org) */ public class DefaultEditorKit extends EditorKit { @@ -123,6 +125,122 @@ public class DefaultEditorKit extends EditorKit } } + static class SelectionBeginWordAction extends TextAction + { + SelectionBeginWordAction() + { + super(selectionBeginWordAction); + } + + public void actionPerformed(ActionEvent event) + { + try + { + JTextComponent t = getTextComponent(event); + + if (t != null) + { + int offs = Utilities.getWordStart(t, t.getCaretPosition()); + + Caret c = t.getCaret(); + c.moveDot(offs); + c.setMagicCaretPosition(t.modelToView(offs).getLocation()); + } + } + catch(BadLocationException ble) + { + // Can't happen. + } + } + } + + static class SelectionEndWordAction extends TextAction + { + SelectionEndWordAction() + { + super(selectionEndWordAction); + } + + public void actionPerformed(ActionEvent event) + { + try + { + JTextComponent t = getTextComponent(event); + + if (t != null) + { + int offs = Utilities.getWordEnd(t, t.getCaretPosition()); + + Caret c = t.getCaret(); + c.moveDot(offs); + c.setMagicCaretPosition(t.modelToView(offs).getLocation()); + } + } + catch(BadLocationException ble) + { + // Can't happen. + } + } + } + + static class BeginWordAction extends TextAction + { + BeginWordAction() + { + super(beginWordAction); + } + + public void actionPerformed(ActionEvent event) + { + try + { + JTextComponent t = getTextComponent(event); + + if (t != null) + { + int offs = Utilities.getWordStart(t, t.getCaretPosition()); + + Caret c = t.getCaret(); + c.setDot(offs); + c.setMagicCaretPosition(t.modelToView(offs).getLocation()); + } + } + catch(BadLocationException ble) + { + // Can't happen. + } + } + } + + static class EndWordAction extends TextAction + { + EndWordAction() + { + super(endWordAction); + } + + public void actionPerformed(ActionEvent event) + { + try + { + JTextComponent t = getTextComponent(event); + + if (t != null) + { + int offs = Utilities.getWordEnd(t, t.getCaretPosition()); + + Caret c = t.getCaret(); + c.setDot(offs); + c.setMagicCaretPosition(t.modelToView(offs).getLocation()); + } + } + catch(BadLocationException ble) + { + // Can't happen. + } + } + } + static class PreviousWordAction extends TextAction { @@ -258,336 +376,260 @@ public class DefaultEditorKit extends EditorKit } } } - - static class SelectionEndLineAction - extends TextAction + + static class SelectionBeginLineAction + extends TextAction { - SelectionEndLineAction() + + SelectionBeginLineAction() { - super(selectionEndLineAction); + super(selectionBeginLineAction); } 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--; - - Caret c = t.getCaret(); - c.moveDot(cur); - c.setMagicCaretPosition(t.modelToView(cur).getLocation()); - } - catch (BadLocationException ble) - { - // Nothing to do here - } + Caret c = t.getCaret(); + try + { + int offs = Utilities.getRowStart(t, c.getDot()); + c.setMagicCaretPosition(t.modelToView(offs).getLocation()); + } + catch(BadLocationException ble) + { + // Can't happen. + } + } } - static class SelectionBeginLineAction + static class SelectionEndLineAction extends TextAction { - SelectionBeginLineAction() + SelectionEndLineAction() { - super(selectionBeginLineAction); + super(selectionEndLineAction); } public void actionPerformed(ActionEvent event) { JTextComponent t = getTextComponent(event); - + Caret c = t.getCaret(); 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++; - - Caret c = t.getCaret(); - c.moveDot(cur); - c.setMagicCaretPosition(t.modelToView(cur).getLocation()); - } - catch (BadLocationException ble) - { - // Do nothing here. - } + { + int offs = Utilities.getRowEnd(t, c.getDot()); + c.setMagicCaretPosition(t.modelToView(offs).getLocation()); + } + catch(BadLocationException ble) + { + // Can't happen. + } + } } - - static class SelectionDownAction - extends TextAction + + static class SelectLineAction extends TextAction { - SelectionDownAction() + SelectLineAction() { - super(selectionDownAction); + super(selectLineAction); } - + public void actionPerformed(ActionEvent event) { JTextComponent t = getTextComponent(event); + Caret c = t.getCaret(); try { - if (t != null) - { - Caret c = t.getCaret(); - // The magic caret position may be null when the caret - // has not moved yet. - Point mcp = c.getMagicCaretPosition(); - int x = (mcp != null) ? mcp.x : 0; - int pos = Utilities.getPositionBelow(t, t.getCaretPosition(), x); - - if (pos > -1) - t.moveCaretPosition(pos); - } + int offs1 = Utilities.getRowStart(t, c.getDot()); + int offs2 = Utilities.getRowEnd(t, c.getDot()); + + c.setDot(offs2); + c.moveDot(offs1); + + c.setMagicCaretPosition(t.modelToView(offs2).getLocation()); } - catch(BadLocationException ble) + catch(BadLocationException ble) { - // FIXME: Swallowing allowed? + // Can't happen. } } } - - static class SelectionUpAction - extends TextAction + + static class SelectWordAction extends TextAction { - SelectionUpAction() + SelectWordAction() { - super(selectionUpAction); + super(selectWordAction); } - + public void actionPerformed(ActionEvent event) { JTextComponent t = getTextComponent(event); + Caret c = t.getCaret(); + int dot = c.getDot(); + try { - if (t != null) + int wordStart = Utilities.getWordStart(t, dot); + + if (dot == wordStart) { - Caret c = t.getCaret(); - // The magic caret position may be null when the caret - // has not moved yet. - Point mcp = c.getMagicCaretPosition(); - int x = (mcp != null) ? mcp.x : 0; - int pos = Utilities.getPositionAbove(t, t.getCaretPosition(), x); + // Current cursor position is on the first character in a word. + c.setDot(wordStart); + c.moveDot(Utilities.getWordEnd(t, wordStart)); + } + else + { + // Current cursor position is not on the first character + // in a word. + int nextWord = Utilities.getNextWord(t, dot); + int previousWord = Utilities.getPreviousWord(t, dot); + int previousWordEnd = Utilities.getWordEnd(t, previousWord); - if (pos > -1) - t.moveCaretPosition(pos); + // Cursor position is in the space between two words. In such a + // situation just select the space. + if (dot >= previousWordEnd && dot <= nextWord) + { + c.setDot(previousWordEnd); + c.moveDot(nextWord); + } + else + { + // Cursor position is inside a word. Just select it then. + c.setDot(previousWord); + c.moveDot(previousWordEnd); + } } + + // If the position was updated change the magic caret position + // as well. + if (c.getDot() != dot) + c.setMagicCaretPosition(t.modelToView(c.getDot()).getLocation()); + } - catch(BadLocationException ble) + catch(BadLocationException ble) { - // FIXME: Swallowing allowed? + // Can't happen. } } } + static class SelectionDownAction + extends TextAction.VerticalMovementAction + { + SelectionDownAction() + { + super(selectionDownAction, SwingConstants.SOUTH); + } + + protected void actionPerformedImpl(Caret c, int offs) + { + c.moveDot(offs); + } + + } + + static class SelectionUpAction + extends TextAction.VerticalMovementAction + { + SelectionUpAction() + { + super(selectionUpAction, SwingConstants.NORTH); + } + + protected void actionPerformedImpl(Caret c, int offs) + { + c.moveDot(offs); + } + + } + static class SelectionForwardAction - extends TextAction + extends TextAction.HorizontalMovementAction { SelectionForwardAction() { - super(selectionForwardAction); + super(selectionForwardAction, SwingConstants.EAST); } - public void actionPerformed(ActionEvent event) + protected void actionPerformedImpl(Caret c, int offs) { - JTextComponent t = getTextComponent(event); - if (t != null) - { - 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. - } - } - } + c.moveDot(offs); } } static class SelectionBackwardAction - extends TextAction + extends TextAction.HorizontalMovementAction { SelectionBackwardAction() { - super(selectionBackwardAction); + super(selectionBackwardAction, SwingConstants.WEST); } - public void actionPerformed(ActionEvent event) + protected void actionPerformedImpl(Caret c, int offs) { - JTextComponent t = getTextComponent(event); - if (t != null) - { - 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. - } - } - } + c.moveDot(offs); } } static class DownAction - extends TextAction + extends TextAction.VerticalMovementAction { DownAction() { - super(downAction); + super(downAction, SwingConstants.SOUTH); } - public void actionPerformed(ActionEvent event) + protected void actionPerformedImpl(Caret c, int offs) { - JTextComponent t = getTextComponent(event); - try - { - if (t != null) - { - Caret c = t.getCaret(); - // The magic caret position may be null when the caret - // has not moved yet. - Point mcp = c.getMagicCaretPosition(); - int x = (mcp != null) ? mcp.x : 0; - int pos = Utilities.getPositionBelow(t, t.getCaretPosition(), x); - - if (pos > -1) - t.setCaretPosition(pos); - } - } - catch(BadLocationException ble) - { - // FIXME: Swallowing allowed? - } + c.setDot(offs); } } static class UpAction - extends TextAction + extends TextAction.VerticalMovementAction { UpAction() { - super(upAction); + super(upAction, SwingConstants.NORTH); } - public void actionPerformed(ActionEvent event) + protected void actionPerformedImpl(Caret c, int offs) { - JTextComponent t = getTextComponent(event); - try - { - if (t != null) - { - Caret c = t.getCaret(); - // The magic caret position may be null when the caret - // has not moved yet. - Point mcp = c.getMagicCaretPosition(); - int x = (mcp != null) ? mcp.x : 0; - int pos = Utilities.getPositionAbove(t, t.getCaretPosition(), x); - - if (pos > -1) - t.setCaretPosition(pos); - } - } - catch(BadLocationException ble) - { - // FIXME: Swallowing allowed? - } + c.setDot(offs); } + } static class ForwardAction - extends TextAction + extends TextAction.HorizontalMovementAction { ForwardAction() { - super(forwardAction); + super(forwardAction, SwingConstants.EAST); } - public void actionPerformed(ActionEvent event) + protected void actionPerformedImpl(Caret c, int offs) { - JTextComponent t = getTextComponent(event); - if (t != null) - { - 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. - } - } - } - + c.setDot(offs); } + } static class BackwardAction - extends TextAction + extends TextAction.HorizontalMovementAction { BackwardAction() { - super(backwardAction); + super(backwardAction, SwingConstants.WEST); } - public void actionPerformed(ActionEvent event) + protected void actionPerformedImpl(Caret c, int offs) { - JTextComponent t = getTextComponent(event); - if (t != null) - { - 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. - } - } - } + c.setDot(offs); } + } static class DeletePrevCharAction @@ -720,6 +762,55 @@ public class DefaultEditorKit extends EditorKit } } + static class BeginAction extends TextAction + { + + BeginAction() + { + super(beginAction); + } + + public void actionPerformed(ActionEvent event) + { + JTextComponent t = getTextComponent(event); + Caret c = t.getCaret(); + c.setDot(0); + try + { + c.setMagicCaretPosition(t.modelToView(0).getLocation()); + } + catch(BadLocationException ble) + { + // Can't happen. + } + } + } + + static class EndAction extends TextAction + { + + EndAction() + { + super(endAction); + } + + public void actionPerformed(ActionEvent event) + { + JTextComponent t = getTextComponent(event); + int offs = t.getDocument().getLength(); + Caret c = t.getCaret(); + c.setDot(offs); + try + { + c.setMagicCaretPosition(t.modelToView(offs).getLocation()); + } + catch(BadLocationException ble) + { + // Can't happen. + } + } + } + /** * Creates a beep on the PC speaker. * @@ -867,8 +958,8 @@ public class DefaultEditorKit extends EditorKit // first we filter the following events: // - control characters // - key events with the ALT modifier (FIXME: filter that too!) - char c = event.getActionCommand().charAt(0); - if (Character.isISOControl(c)) + int cp = event.getActionCommand().codePointAt(0); + if (Character.isISOControl(cp)) return; JTextComponent t = getTextComponent(event); @@ -1345,8 +1436,6 @@ public class DefaultEditorKit extends EditorKit * The <code>Action</code>s that are supported by the * <code>DefaultEditorKit</code>. */ - // TODO: All these inner classes look ugly. Maybe work out a better way - // to handle this. private static Action[] defaultActions = new Action[] { // These classes are public because they are so in the RI. @@ -1387,9 +1476,21 @@ public class DefaultEditorKit extends EditorKit new PreviousWordAction(), new SelectionPreviousWordAction(), + new BeginAction(), new SelectionBeginAction(), + + new EndAction(), new SelectionEndAction(), + + new BeginWordAction(), + new SelectionBeginWordAction(), + + new EndWordAction(), + new SelectionEndWordAction(), + new SelectAllAction(), + new SelectLineAction(), + new SelectWordAction() }; /** |