Correct right and left traversal, backspace and delete key functioning

dbus-notify
Zlatin Balevsky 2022-06-10 11:01:45 +01:00
parent 6ec0b3c295
commit d883b96ed2
No known key found for this signature in database
GPG Key ID: A72832072D525E41
1 changed files with 47 additions and 14 deletions

View File

@ -65,7 +65,22 @@ class ChatEntryPane extends JTextPane {
KeyStroke back = KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0) KeyStroke back = KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0)
Object backObject = inputMap.get(back) Object backObject = inputMap.get(back)
backspaceAction = actionMap.get(backObject) backspaceAction = actionMap.get(backObject)
actionMap.put(backObject, new BackspaceAction(backspaceAction)) actionMap.put(backObject, new ForwardingAction(backspaceAction, false, true))
KeyStroke delete = KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0)
Object deleteObject = inputMap.get(delete)
Action originalDeleteAction = actionMap.get(deleteObject)
actionMap.put(deleteObject, new ForwardingAction(originalDeleteAction, true, true))
KeyStroke left = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0)
Object leftObject = inputMap.get(left)
Action originalLeftAction = actionMap.get(leftObject)
actionMap.put(leftObject, new ForwardingAction(originalLeftAction, false, false))
KeyStroke right = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0)
Object rightObject = inputMap.get(right)
Action originalRightAction = actionMap.get(rightObject)
actionMap.put(rightObject, new ForwardingAction(originalRightAction, true, false))
Action noAction = new NullAction() Action noAction = new NullAction()
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0) KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0)
@ -178,30 +193,48 @@ class ChatEntryPane extends JTextPane {
} }
} }
private class BackspaceAction extends UIAction { private class ForwardingAction extends UIAction {
private final Action delegate private final Action delegate
BackspaceAction(Action delegate) { private final boolean forward, modifying
super("backspace") ForwardingAction(Action delegate, boolean forward, boolean modifying) {
super("forwarding")
this.delegate = delegate this.delegate = delegate
this.forward = forward
this.modifying = modifying
} }
@Override @Override
void actionPerformed(ActionEvent e) { void actionPerformed(ActionEvent e) {
StyledDocument document = getStyledDocument() int position = getCaret().getDot()
int position = getCaret().getDot() - 1 if (!forward)
Element element = document.getCharacterElement(position) position--
if (element.getAttributes().getAttribute(StyleConstants.ComponentAttribute) == null) { if (!isInComponent(position)) {
delegate.actionPerformed(e) delegate.actionPerformed(e)
return return
} }
while(position > 0) { if (forward) {
element = document.getCharacterElement(position) while(position < getDocument().getEndPosition().getOffset()) {
if (element.getAttributes().getAttribute(StyleConstants.ComponentAttribute) == null) if (!isInComponent(position))
break break
delegate.actionPerformed(e) delegate.actionPerformed(e)
position-- if (!modifying)
position++
}
} else {
while (position > 0) {
if (!isInComponent(position))
break
delegate.actionPerformed(e)
position--
}
} }
} }
private boolean isInComponent(int position) {
StyledDocument document = getStyledDocument()
Element element = document.getCharacterElement(position)
return element.getAttributes().getAttribute(StyleConstants.ComponentAttribute) != null
}
} }
private static class NullAction extends UIAction { private static class NullAction extends UIAction {