Eclipse: selection autocopy to clipboard - eclipse

I love an Emacs feature to copy selection to clipboard automatically. Is it possible to do the same on Eclipse?
Environment: Windows XP, Helios

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();
}
Then you can call this method from a MouseAdapter or KeyAdapter, depending on where you want to get your String from. In your case it could be MouseAdapter, which listens to doubleclicks, gets the current cursor position of the text, marks the word and then adds the String to the clipboard.
edit to answer a question: You can set up your own MouseAdapater and attach it to buttons, text fields or whateer you like. Here's an example for a button:
Button btnGo1 = new Button(parent, SWT.NONE);
btnGo1.setText("Go");
btnGo1.addMouseListener(new MouseAdapter() {
#Override
public void mouseDoubleClick(MouseEvent e) {
//do what you want to do in here
}
});
If you want to implement mouseUp and mouseDown events, too, you can just add MouseListenerinstead of the Adapter. The only advantage of the Adapter is, that you don't have to override the other methods of the interface.
Since the original question was to automatically get the selection of the text of an editor: the way to get the selection from an editor is explained here.

You can try this plugin. Along with auto copy points mentioned in Eclipse show number of lines and/or file size also addressed.

Related

Example showing Multi Edit in Nattable

I have a requirement wherein on a single click in the cell, normal editing must be possible and on double clicking in the cell a dialog should open for editing the cell. The two are possible individually. I see a method "boolean supportMultiEdit(IConfigRegistry configRegistry, List configLabels)" but there is no example to show the working. Has anyone used it or can show it's configuration.
Multi edit means it is possible to edit multiple cells at once. This is of course done in an editor, as it makes no sense to perform multi edit inline. You should rather have a look at openInline(IConfigRegistry, List<String>) or even better the EditConfigAttributes#OPEN_IN_DIALOG to solve what you are looking for.
But you are actually seeking for a way to handle opening an editor differently on different UI interactions. So you need to register the corresponding UI bindings. This is already discussed in the NatTable Forum.
And the EditorExample shows quite a lot of possible configuration options available for editing. And almost every editable example shows multi editing capabilities. You simply need to select multiple cells you want to edit and then start typing or pressing F2.
The following code would do the trick with a configuration based on a label that is added in the UI binding action:
public class OpenEditorConfiguration extends AbstractRegistryConfiguration {
#Override
public void configureRegistry(IConfigRegistry configRegistry) {
configRegistry.registerConfigAttribute(
EditConfigAttributes.OPEN_IN_DIALOG,
Boolean.TRUE,
DisplayMode.EDIT,
"open_in_dialog");
}
#Override
public void configureUiBindings(UiBindingRegistry uiBindingRegistry) {
uiBindingRegistry.registerDoubleClickBinding(
new CellEditorMouseEventMatcher(GridRegion.BODY),
new IMouseAction() {
#Override
public void run(NatTable natTable, MouseEvent event) {
int columnPosition = natTable.getColumnPositionByX(event.x);
int rowPosition = natTable.getRowPositionByY(event.y);
ILayerCell cell = natTable.getCellByPosition(columnPosition, rowPosition);
cell.getConfigLabels().add("open_in_dialog");
natTable.doCommand(new EditCellCommand(
natTable,
natTable.getConfigRegistry(),
cell));
}
});
}
}

Can I use the eclipse debugger to write a variable to a file

Is it possible to create some sort of "trace breakpoint" in eclipse whereby Eclipse will log variables that I choose to a file when a "breakpoint" is hit without pausing the application?
Sure. For this sample method:
public static void logArgs(final String one, final String two) {
System.out.println(one + two);
}
put your breakpoint in it, right click it and edit Breakpoint properties... as in the example. The important checkboxes are Conditional and Suspend when 'true'. Just return false so that breakpoint does not suspend at all.
java.io.FileWriter w = new java.io.FileWriter("/tmp/file.txt", true);
w.write(String.format("args(%s, %s)%n"), one, two));
w.close();
return false;
For Java 11 Michał Grzejszczak's answer can be written as
java.nio.file.Files.writeString(java.nio.file.Path.of("f.txt"), "My string to save");return true;
(Best way to write String to file using java nio)

Eclipse E4 RCP StyledText obtain INSERT KEY state

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
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!
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.

Editable Label/Div losing focus on click

I have an editable GWT Label which shows a strange behavior. That is if I click the text “Add note…” the cursor does not appear until I click a second time. But if I click on the label outside the text the cursor appears on first click. How do I solve that? My guess is that replacing the text also removes the cursor when the cursor is in the text. So how can I get the cursor back on first click?
public class EditableLabel extends Label implements FocusHandler {
public EditableLabel() {
super();
getElement().setAttribute("contenteditable", "true");
getElement().setAttribute("tabindex", "1");
this.sinkEvents(Event.ONBLUR);
this.sinkEvents(Event.ONFOCUS);
addHandler(this, FocusEvent.getType());
setText("Add note...");
}
#Override
public void onFocus(FocusEvent event) {
setText("");
}
}
I think your problem depends on the browser. On FF it works fine for me.
I assume you want to write something, if so try to change Label for TextBox, it should work.

Making Ctrl-C copy from whichever SourceViewer has focus in Eclipse plug-in

I successfully extended the PyDev editor in Eclipse with a side-by-side display, but I can't copy the contents of the extra SourceViewer that I added. I can select some text in the display, but when I press Ctrl+C, it always copies the main PyDev editor's selected text.
I found an article on key bindings in Eclipse editors, but the code there seems incomplete and a bit out-of-date. How can I configure the copy command to copy from whichever SourceViewer has focus?
The reason I want to do this is that I've written a tool for live coding in Python, and it would be much easier for users to submit bug reports if they could just copy the display and paste it into the bug description.
David Green's article was a good start, but it took a bit of digging to make it all work. I published a full example project on GitHub, and I'll post a couple of snippets here.
The TextViewerSupport class wires up a new action handler for each command you want to delegate to the extra text viewer. If you have multiple text viewers, just instantiate a TextViewerSupport object for each of them. It wires up everything in its constructor.
public TextViewerSupport(TextViewer textViewer) {
this.textViewer = textViewer;
StyledText textWidget = textViewer.getTextWidget();
textWidget.addFocusListener(this);
textWidget.addDisposeListener(this);
IWorkbenchWindow window = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow();
handlerService = (IHandlerService) window
.getService(IHandlerService.class);
if (textViewer.getTextWidget().isFocusControl()) {
activateContext();
}
}
The activateContext() method has a list of all the commands you want to delegate, and registers a new handler for each one. This was one of the changes from David's article; his ITextEditorActionDefinitionIds has been deprecated and replaced with IWorkbenchCommandConstants.
protected void activateContext() {
if (handlerActivations.isEmpty()) {
activateHandler(ITextOperationTarget.COPY,
IWorkbenchCommandConstants.EDIT_COPY);
}
}
// Add a single handler.
protected void activateHandler(int operation, String actionDefinitionId) {
StyledText textWidget = textViewer.getTextWidget();
IHandler actionHandler = createActionHandler(operation,
actionDefinitionId);
IHandlerActivation handlerActivation = handlerService.activateHandler(
actionDefinitionId, actionHandler,
new ActiveFocusControlExpression(textWidget));
handlerActivations.add(handlerActivation);
}
// Create a handler that delegates to the text viewer.
private IHandler createActionHandler(final int operation,
String actionDefinitionId) {
Action action = new Action() {
#Override
public void run() {
if (textViewer.canDoOperation(operation)) {
textViewer.doOperation(operation);
}
}
};
action.setActionDefinitionId(actionDefinitionId);
return new ActionHandler(action);
}
The ActiveFocusControlExpression gives the new handler a high enough priority that it will replace the standard handler, and it's almost identical to David's version. However, to get it to compile, I had to add extra dependencies to my plug-in manifest: I imported packages org.eclipse.core.expressions and org.eclipse.ui.texteditor.