1 year ago

#386076

test-img

Raghu

Eclipse SWT StyledText set caret at specified position forcefully

I have an editor implementation which uses SWT StyledText as an editor area. When Save Operation is invoked using CTRL+S (not on the toollbar, still focus is present on StyledText) caret is lost. Due to this next keyboard inputs are not received by StyledText. We need to manually click desired positoin to activate the caret and start typing in the text box. I did some anyalysis on selection and focus before and after save.

   `Control beforeFocusControl = Display.getCurrent().getFocusControl();
    IWorkbenchPart beforeActivePart = getActivePart();
    if (beforeFocusControl instanceof StyledText) {
        StyledText sText = (StyledText) beforeFocusControl;
        Cursor oldCursor = sText.getCursor();
    }

    // save the editor
    EditorWindowManager.getInstance().saveEditors(SnippetEditor);

    Control afterFocusControl = Display.getCurrent().getFocusControl();
    IWorkbenchPart afterActivePart = getActivePart();
    // before and after there is no change in selection
    // beforeFocusControl == afterFocusControl
    // beforeActivePart == afterActivePart
    if (afterFocusControl instanceof StyledText) {
        StyledText sText = (StyledText) afterFocusControl;
        //as old focus control was same i tried force setting the cursor and focus
        sText.setCursor(sText.getCursor());
        sText.forceFocus();
    }`

There is no change in focus or workbench selection. So i tried to set the caret again and force focus the StyledText but this did not get back the caret to last edited location.

Is there any convenient api in jface to do this ? I know the caret position where it was last present before it was lost.

java

swt

eclipse-rcp

jface

rcp

0 Answers

Your Answer

Accepted video resources