Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
liz3 opened this issue Sep 1, 2016 · 3 comments
Closed

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

liz3 opened this issue Sep 1, 2016 · 3 comments

Comments

@liz3
Copy link

liz3 commented Sep 1, 2016

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

@JordanMartinez
Copy link
Contributor

@liz3 I haven't looked with that much depth into your code, but you might want to try using moveTo(caretPosition) instead of positionCaret, as latter method is actually supposed to be package-private.

@liz3 liz3 changed the title Problem with caret position after text replacement Problem with caret position after text replacement(FIXED) Sep 2, 2016
@liz3
Copy link
Author

liz3 commented Sep 2, 2016

@JordanMartinez Thank you very very much, it solved the problem)

@JordanMartinez
Copy link
Contributor

👍
Can you close this issue now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants