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.

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.

3. About this website

4. EclipseText resources

Nothing listed.

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.