Search in sources :

Example 1 with TextInputContentChangeEvent

use of com.spinyowl.legui.component.event.textinput.TextInputContentChangeEvent in project legui by SpinyOwl.

the class TextInputCharEventListener method process.

/**
 * Used to handle {@link CharEvent}.
 *
 * @param event event to handle.
 */
@Override
public void process(CharEvent event) {
    TextInput textInput = (TextInput) event.getTargetComponent();
    if (textInput.isFocused() && textInput.isEditable() && !MOUSE_BUTTON_LEFT.isPressed()) {
        String str = cpToStr(event.getCodepoint());
        TextState textState = textInput.getTextState();
        String oldText = textState.getText();
        int start = textInput.getStartSelectionIndex();
        int end = textInput.getEndSelectionIndex();
        if (start > end) {
            start = textInput.getEndSelectionIndex();
            end = textInput.getStartSelectionIndex();
        }
        if (start != end) {
            StringBuilder t = new StringBuilder(textState.getText());
            t.delete(start, end);
            textState.setText(t.toString());
            textInput.setCaretPosition(start);
            textInput.setStartSelectionIndex(start);
            textInput.setEndSelectionIndex(start);
        }
        int caretPosition = textInput.getCaretPosition();
        StringBuilder t = new StringBuilder(textState.getText());
        t.insert(caretPosition, str);
        textState.setText(t.toString());
        int newCaretPosition = caretPosition + str.length();
        textInput.setCaretPosition(newCaretPosition);
        textInput.setEndSelectionIndex(newCaretPosition);
        textInput.setStartSelectionIndex(newCaretPosition);
        String newText = textState.getText();
        EventProcessorProvider.getInstance().pushEvent(new TextInputContentChangeEvent(textInput, event.getContext(), event.getFrame(), oldText, newText));
    }
}
Also used : TextState(com.spinyowl.legui.component.optional.TextState) TextInputContentChangeEvent(com.spinyowl.legui.component.event.textinput.TextInputContentChangeEvent) TextInput(com.spinyowl.legui.component.TextInput)

Aggregations

TextInput (com.spinyowl.legui.component.TextInput)1 TextInputContentChangeEvent (com.spinyowl.legui.component.event.textinput.TextInputContentChangeEvent)1 TextState (com.spinyowl.legui.component.optional.TextState)1