To copy a string from Eclipse to the clipboard, you can use
void copyToClipboard (String toClipboard, Display display){ String toClipboard = "my String"; Clipboard clipboard = new Clipboard(display); TextTransfer [] textTransfer = {TextTransfer.getInstance()}; clipboard.setContents(new Object [] {toClipboard}, textTransfer); clipboard.dispose(); }
You can then call this method from the MouseAdapter or KeyAdapter , depending on where you want to get your string from. In your case, it could be a MouseAdapter that listens for doubleclicks, gets the current cursor position in the text, marks the word, and then adds String to the clipboard.
edit to answer the question: you can customize your own MouseAdapater and attach it to buttons, text fields, or you like. Here is an example of a button:
Button btnGo1 = new Button(parent, SWT.NONE); btnGo1.setText("Go"); btnGo1.addMouseListener(new MouseAdapter() { @Override public void mouseDoubleClick(MouseEvent e) {
If you want to implement mouseUp and mouseDown events, you can simply add MouseListener instead of an adapter. The only advantage of the adapter is that you do not need to override other interface methods.
Since the original question was to automatically get the editorโs text selection: the way to get the selection from the editor is explained here .
Calon source share