Home Tutorials Training Consulting Products Books Company Donate Contact us









NOW Hiring

Quick links

Share

This tutorial contains notes about using the Eclipse Text API.

1. Eclipse Text

1.1. What is the Eclipse Text API

Eclipse Text is part of the Eclipse Platform UI project. It provides the capabilities to develop base text editors, code completion, hyperlinking and similar functionality.

The approach as of Eclipse 4.7 is to extend the generic editor of the Eclipse platform we language specific features.

If you hold down the Ctrl and click on an element in the Java editor you can navigate to it. This functionality is provided by the Eclipse Text framework and can be extended for every editor.

In this exercise you create a hyperlink detector for the vogella keyword.

Create a new plug-in project. Add the org.eclipse.ui.workbench.texteditor.hyperlinkDetectors extension with a unique id and a descriptive name. This name is visible in the preferences under General ▸ Editors ▸ Text Editors ▸ Hyperlinking.

The targetId points to the type of editor you want to support. If you want to use in all text editors use org.eclipse.ui.DefaultTextEditor.

Create the following two classes to determine the hyperlink and to react to it.

package com.vogella.eclipse.text.hyperlinkdetector;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.hyperlink.IHyperlink;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

public class VogellaHyperlink implements IHyperlink {

 private final IRegion fUrlRegion;

 public VogellaHyperlink(IRegion urlRegion) {
  fUrlRegion = urlRegion;
 }

 @Override
 public IRegion getHyperlinkRegion() {
  return fUrlRegion;
 }

 @Override
 public String getTypeLabel() {
  return null;
 }

 @Override
 public String getHyperlinkText() {
  return null;
 }

 @Override
 public void open() {
     Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
     MessageDialog.openInformation(shell, "Clicked", "Clicked on vogella");
 }
}
package com.vogella.eclipse.text.hyperlinkdetector;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector;
import org.eclipse.jface.text.hyperlink.IHyperlink;

public class VogelHyperlinkDetector extends AbstractHyperlinkDetector {

    public VogelHyperlinkDetector() {
    }

    @Override
    public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {

        IDocument document = textViewer.getDocument();
        int offset = region.getOffset();

        // extract relevant characters
        IRegion lineRegion;
        String candidate;
        try {
            lineRegion = document.getLineInformationOfOffset(offset);
            candidate = document.get(lineRegion.getOffset(), lineRegion.getLength());
        } catch (BadLocationException ex) {
            return null;
        }

        // look for keyword
        int index = candidate.indexOf("vogella");
        if (index != -1) {

            // detect region containing keyword
            IRegion targetRegion = new Region(lineRegion.getOffset() + index, "vogella".length());
            if ((targetRegion.getOffset() <= offset)
                    && ((targetRegion.getOffset() + targetRegion.getLength()) > offset))
                // create link
                return new IHyperlink[] { new VogellaHyperlink(targetRegion) };
        }

        return null;
    }

}

Start your plug-in and add vogella to one of the text editor, for example the Java editor. Press Ctrl and click on vogella. This should open a dialog.

2.3. Adding colors and fonts preferences

Eclipse provides a page for the customizations of colors and fonts by the user under General ▸ Appearance ▸ Colors and Fonts.

To define an entry for this page, you need to define an extension for the org.eclipse.ui.themes extension point.

For example, you can provide a category, font and color with the following entry in the plugin.xml file or your plug-in.

 <extension point="org.eclipse.ui.themes">
    <themeElementCategory
        id="com.vogella.eclipse.preferences.mythemeElementCategory"
        label="vogella category">
        <description>
            An example theme category
        </description>
    </themeElementCategory>
    <colorDefinition
        categoryId="com.vogella.eclipse.preferences.mythemeElementCategory"
        id="com.vogella.eclipse.preferences.myFirstColorDefinition"
        label="vogella color"
        value="COLOR_DARK_BLUE">
        <description>
            Your description for the color
        </description>
    </colorDefinition>
    <fontDefinition
        categoryId="com.vogella.eclipse.preferences.mythemeElementCategory"
        id="com.vogella.eclipse.preferences.myFirstFontDefinition"
        label="vogella Font"
        value="Lucida Sans-italic-18">
        <description>
            Your description for the font
        </description>
    </fontDefinition>
</extension>

The value for the color can be a COLOR_* constants defined in the SWT class. You can also specify RGB values like 125,245,96. The value for the font is defined via the following pattern:`fontname-style-height`

The preference can now be changed via the user or via the CSS engine. To get the current value you can use the IThemeManager.

// Eclipse 4 API
@Inject IThemeManager themeManager;

// Eclipse 3 API
IThemeManager themeManager = PlatformUI.getWorkbench().getThemeManager();

ITheme currentTheme = themeManager.getCurrentTheme();

ColorRegistry colorRegistry = currentTheme.getColorRegistry();
Color color = colorRegistry.get("com.vogella.eclipse.preferences.myFirstColorDefinition");

FontRegistry fontRegistry = currentTheme.getFontRegistry();
Font font = fontRegistry.get("com.vogella.eclipse.preferences.myFirstFontDefinition");

3. About this website

4. Eclipse Text resources

4.1. vogella GmbH training and consulting support

TRAINING SERVICE & SUPPORT

The vogella company provides comprehensive training and education services from experts in the areas of Eclipse RCP, Android, Git, Java, Gradle and Spring. We offer both public and inhouse training. Whichever course you decide to take, you are guaranteed to experience what many before you refer to as “The best IT class I have ever attended”.

The vogella company offers expert consulting services, development support and coaching. Our customers range from Fortune 100 corporations to individual developers.

Copyright © 2012-2017 vogella GmbH. Free use of the software examples is granted under the terms of the EPL License. This tutorial is published under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Germany license.

See Licence.