Skip to content

Problem with caret position after text replacement(FIXED) #353

@liz3

Description

@liz3

Hey,
im really impressed by this API, but i have one issue:
im using a CodeArea and add/replace text from a popup window like in ide´s, that open at the caret.
After a replacement of a selection, i want to set the caret to a specific location.

  public void handle(MouseEvent event) {
                     String seletion = chooseView.getSelectionModel().getSelectedItem();
                     String before = "";
                     if (seletion.contains("Event")) {

                    String trueT = "";
                    String[] split = seletion.split(" ");
                    String[] r = split[0].split("(?=\\p{Upper})");
                    for (int i = 0; i != r.length; i++) {
                        trueT = trueT + " " + r[i];
                    }


                    before = area.getText(area.getCaretPosition(), area.getCaretPosition() - prefix.length() + trueT.trim().length() * 2);


                    area.replaceText(area.getCaretPosition() - prefix.length(), area.getCaretPosition() - prefix.length() + trueT.trim().length() * 2, trueT.trim() + ":\n        " + before);


                    pos = area.getCaretPosition() - before.length();

                } else {
                    String trueT = "";
                    String[] split = seletion.split(" ");
                    trueT = split[0];


                    before = area.getText(area.getCaretPosition(), area.getCaretPosition() - prefix.length() + trueT.trim().length() * 2);

                    area.replaceText(area.getCaretPosition() - prefix.length(), area.getCaretPosition() - prefix.length() + trueT.trim().length() * 2, trueT.trim() + " " + before);


                    pos = area.getCaretPosition() - before.length();
                }
                System.out.println(pos);
                area.positionCaret(pos);



                if (showList) {
                    win.hide();
                    chooseView.setVisible(false);
                    showList = false;


                }


            }

Thats not the problem.
It goes to the location, i have set.
The problem is, if i type a letter the caret jumps to another location below.
The caret index is (sout) the same.

(By clicking the location of the caret before typing, solve it)
Here is the setup method for the popup, maybe needed:

     private void setList() {


    if(win == null) {
        win = new Popup();
        chooseView = new ListView();
    }

    ArrayList<ApiCondition> conditions = skUnity.getConditions();
    ArrayList<ApiEffect> effects = skUnity.getEffects();
    ArrayList<ApiEvent> events = skUnity.getEvents();
    ArrayList<ApiExpression> expressions = skUnity.getExpressions();
    ArrayList<ApiType> types = skUnity.getTypes();
    for (int i = 1; i != conditions.size(); i++) {
        chooseView.getItems().add(conditions.get(i).getId() + " Condition");
    }
    for (int i = 0; i != effects.size(); i++) {
        chooseView.getItems().add(effects.get(i).getId() + " Effect");
    }
    for (int i = 0; i != events.size(); i++) {
        chooseView.getItems().add(events.get(i).getId() + " Event");
    }
    for (int i = 0; i != expressions.size(); i++) {
        chooseView.getItems().add(expressions.get(i).getId() + " Expression");
    }
    for (int i = 0; i != types.size(); i++) {
        chooseView.getItems().add(types.get(i).getId() + " Type");
    }

    chooseView.getItems().addAll(new Supers().getSupervArray());

    chooseView.setPrefSize(180, 200);
    Tab tab = codeTabPane.getSelectionModel().getSelectedItem();
    CodeArea area = (CodeArea) tab.getContent();
    win.getContent().add(chooseView);

    area.setPopupWindow(win);



    area.setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            if (showList) {

                win.hide();
                chooseView.setVisible(false);
                showList = false;

            }
        }
    });


}

Here is the method, that toggles the list(i already tried something another user, wrote, to move the caret):

 public void chooseList() {
    Tab tab = codeTabPane.getSelectionModel().getSelectedItem();
    CodeArea area = (CodeArea) tab.getContent();

    if (win == null) {
        setList();

    }


    if (!win.isShowing()) {

        Stage stage = (Stage) commandSendBtn.getScene().getWindow();




        javafx.application.Platform.runLater(() -> {
                    int caretPosition = area.getCaretPosition();
            System.out.println(pos);
                    if (0 != caretPosition) {
                        area.positionCaret(caretPosition - 1);
                        area.positionCaret(caretPosition);
                    } else {
                        area.positionCaret(caretPosition + 1);
                        area.positionCaret(caretPosition);
                    }


                }

        );
        area.setPopupAlignment(PopupAlignment.CARET_BOTTOM);
        win.show(stage);
        chooseView.setVisible(true);
        showList = true;


    }
    updateList();


}

Pressing a key, triggers the toggle method, which checks and setup if needed.
Can someone help me out?

Liz3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions