Eclipse editor plugin - line numbers and print margin - eclipse

I write editor as plugin to eclipse. I write it as extends TextEditor. And I need show line numbers and print margin. I cant find method where to set. Where can I set it? Maybe I extends wrong class.
Edit:
I fixed it like that:
public void setFocus() {
getPreferenceStore().setValue(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_LINE_NUMBER_RULER, true);
getPreferenceStore().setValue(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_PRINT_MARGIN, true);
getPreferenceStore().setValue(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_PRINT_MARGIN_COLUMN, 72);
super.setFocus();
}

Related

Increased line height for Consolas font in Eclipse

Many people are using Consolas for their primary programming font but unfortunately there is no way to change line height in Eclipse so it looks kinda ugly as it is shown below:
I was wondering if there is anyone who solved this by adding some extra space between lines or simply changing the font itself which has longer height now.
It would be nice to share it with us here on Stackoverflow.
There are some topics I've found while searching for this but none of them were what I am looking for:
How can I change line height / line spacing in Eclipse?
https://stackoverflow.com/questions/15153938/improved-line-spacing-for-eclipse?lq=1
and so on...
Some of them designed their own fonts (such as Meslo Font) by modifying the existing ones so it would be nice if you could share your modified Consolas font.
As mentioned in one of the answers you reference the underlying StyledText control does have a setLineSpacing method, but the existing editors do not use it.
The CSS styling code in Eclipse 4.3 does provide a way to access this but it requires writing a plugin to extend the CSS in order to do so.
The plugin.xml for the plugin would look like this:
<plugin>
<extension
point="org.eclipse.e4.ui.css.core.elementProvider">
<provider
class="linespacing.LineSpacingElementProvider">
<widget
class="org.eclipse.swt.custom.StyledText"></widget>
</provider>
</extension>
<extension
point="org.eclipse.e4.ui.css.core.propertyHandler">
<handler
adapter="linespacing.StyledTextElement"
composite="false"
handler="linespacing.LineSpacingPropertyHandler">
<property-name
name="line-spacing">
</property-name>
</handler>
</extension>
</plugin>
which declares a CSS element provider LineSpacingElementProvider which would be:
public class LineSpacingElementProvider implements IElementProvider
{
#Override
public Element getElement(final Object element, final CSSEngine engine)
{
if (element instanceof StyledText)
return new StyledTextElement((StyledText)element, engine);
return null;
}
}
The StyledTextElement this provides is just:
public class StyledTextElement extends ControlElement
{
public StyledTextElement(StyledText control, CSSEngine theEngine)
{
super(control, theEngine);
}
}
The second declaration in the plugin.xml is a CSS property handler for a property called line-spacing
public class LineSpacingPropertyHandler extends AbstractCSSPropertySWTHandler implements ICSSPropertyHandler
{
#Override
protected void applyCSSProperty(Control control, String property, CSSValue value, String pseudo, CSSEngine engine) throws Exception
{
if (!(control instanceof StyledText))
return;
StyledText text = (StyledText)control;
if ("line-spacing".equals(property))
{
int pixelValue = (int)((CSSPrimitiveValue)value).getFloatValue(CSSPrimitiveValue.CSS_PX);
text.setLineSpacing(pixelValue);
}
}
#Override
protected String retrieveCSSProperty(Control control, String property, String pseudo, CSSEngine engine) throws Exception
{
return null;
}
}
With a plugin containing this installed you can then modify one of the existing CSS style sheets to contain:
StyledText {
line-spacing: 2px;
}
You better choose different fonts. Don't just stick to things. Try new things and accept them. :D I was also using Consolas. But now I am using Courier New and they are pretty fine. If you have 21" or larger display you can use Courier New at 12 or 14 size. Once you use this you will get used to it as you are with Consolas now :P
You could try Fira Mono. See some screenshots here. Small sizes look good too.

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.

Eclipse: selection autocopy to clipboard

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.

Eclipse custom formatting

I'm trying to create a JAVA code formatter such that it doesn't wrap any lines. BUT for any lines in my code that I have manually wrapped I want the formatter to respect them and not format them in to one line.
For example:
public Class {
public Class(String a,
String b,
String c,
String d) {
// The constructor arguments should stay as they are
}
public void aMethod() {
// This statement should not be wrapped
get().doSomething().getAnohterMethodThatHasAReeeeeeeaalllyyLongName();
}
}
I have made the line width 9999 (the max), and I have turned off line wrapping for everything. What have I missed?
Thanks.
I opened the preference page for
"Java - Code Style - Formatter"
and activated "never join lines"
and selected "Do not wrap" in the combo box "line wrapping policy"
After this change i was able to write code, which was not wrapped.