0

For an RCP E4 Text Editor application implemented with a StyledText/SourceViewer it is necessary receive the status of the inset key.

Once received the state (insert, smart-insert), the application shall modify the cursor icon and notify other parts the INSERT state (i.e. notify to the status bar control like in a normal plain text editor behavior).

SWT.INSERT only listens for the key to be pressed, but nothing if the StyledText is in INSERT MODE.

styledText.addKeyListener(new KeyAdapter(){
    public void keyPressed(KeyEvent e){
        if(e.keyCode == SWT.INSERT){
            System.out.println("INSERT KEY PRESSED!!!");
        }
    }
};

I have avoided to extend

org.eclipse.ui.texteditor.AbstractTextEditor

and use the method

getInsertMode()

since the application is intended to be pure E4 text editor.

Any hint?

Thanks in advance

greg-449
  • 109,219
  • 232
  • 102
  • 145
J Robes
  • 467
  • 1
  • 6
  • 19

2 Answers2

1

First off you need to tell the StyledText not to do the default action when it sees the Insert key:

textWidget.setKeyBinding(SWT.INSERT, SWT.NULL);

Next you need to define a Command, Handler and Key Binding in the context for the editor to deal with the Insert key.

The Handler for the insert command can update the status display and shoyld then tell the StyledText to update the overwrite mode:

textWidget.invokeAction(ST.TOGGLE_OVERWRITE);

Also note that Mac keyboards don't have an Insert key!

greg-449
  • 109,219
  • 232
  • 102
  • 145
0

Since I found some difficulties to deal with the INSERT_KEY within a sourceviewer control for E4 RCP text editor, I will write extra details to gregg449's answer (great help from him as everytime!).

Following the above answer, I have created Binding Context, Binding Table, Command, Handler and added the Binding Context to the required Part (the part implementing the SourceViewer).

The next code is for SourceViewer and InserKey Handler:

public class CheckKeyBindingSourceViewer extends ITextEditorPart{

    public SourceViewer sv = null;
    public StyledText st = null;

    @PostConstruct
    public void postConstruct(Composite parent) {
        sv = new SourceViewer(parent, null, null, true, SWT.MULTI | SWT.V_SCROLL |SWT.H_SCROLL);
        IDocument doc = new Document("");
        sv.setDocument(doc);
        st = sv.getTextWidget();

        //tell the StyledText not to do the default action when it sees the Insert key
        st.setKeyBinding(SWT.INSERT, SWT.NULL);
    }
}


public class InsertKeyHandler {
    @Execute
    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart) {
        if (activePart.getObject() instanceof ITextEditorPart){
            ITextEditorPart theSourceViewer = (ITextEditorPart) activePart.getObject();
            theSourceViewer.st.invokeAction(ST.TOGGLE_OVERWRITE);
            //TODO
            //Change cursor sourcewiewer, notify to Statusbar...
        }
    }
}

The next figure shows the Application.e4xmi with the Binding Context and Binding Table created. Note that if you do not add the supplementary tag "type:user" to the Binding Table, the bindings are not working at all. This is not reflected into vogella's tutorial (http://www.vogella.com/tutorials/EclipseRCP/article.html) neither his book.

The only place I found this information was at stackoverflow question: eclipse rcp keybindings don't work

I'm using eclipse Mars (4.5.0) for both Linux and Windows, I do not know if for newer verions this 'bug' is solved.

Key Binding configuration

Community
  • 1
  • 1
J Robes
  • 467
  • 1
  • 6
  • 19