Reader small image

You're reading from  Eclipse Plug-in Development Beginner's Guide - Second Edition

Product typeBook
Published inAug 2016
Reading LevelExpert
Publisher
ISBN-139781783980697
Edition2nd Edition
Languages
Tools
Right arrow
Author (1)
Alex Blewitt
Alex Blewitt
author image
Alex Blewitt

contacted on 30 aug 16 _____________ Dr Alex Blewitt has over 20 years of experience in Objective-C and has been using Apple frameworks since NeXTstep 3.0. He upgraded his NeXTstation for a TiBook when Apple released Mac OS X in 2001 and has been developing on it ever since. Alex currently works for an investment bank in London, writes for the on-line technology news site InfoQ and has published two other books for Packt publishing. He also has a number of apps on the Apple AppStore through Bandlem Limited. When he's not working on technology, and if the weather is nice, he likes to go flying from the nearby Cranfield airport. Alex writes regularly at his blog, http://alblue.bandlem.com, as well tweeting regularly from Twitter as @alblue. Acknowledgements This book would not have been possible without the ongoing love and support of my wife Amy, who has helped me through both the highs and lows of life. She gave me the freedom to work during the many late nights and weekends that it takes to produce a book and its associated code repository. She truly is the Lem of my life. I'd also like to thank my parents, Ann and Derek, for their encouragement and support during my formative years. It was this work ethic that allowed me to start my technology career as a teenager and to incorporate my first company before I was 25. I'd also like to congratulate them on their 50th wedding anniversary in 2015, and I look forward to reaching that goal with Amy. Thanks are due especially to the reviewer of this version of the book: Antonio Bello, as well as the previous version of this book: Nate Cook, James Robert and Arvid Gerstmann, who provided excellent feedback on the contents of this book during development and caught many errors in both the text and code. Any remaining errors are my own. I'd also like to thank my children Sam and Holly for inspiring me and hope that they too can achieve anything that they set their minds to. Finally, I'd like to thank Ben Moseley and Eren Kotan, both of whom introduced me to NeXT in the first place and set my career going on a twenty year journey to this book.
Read more about Alex Blewitt

Right arrow

Chapter 6. Working with Resources

Resources – files, folders, and builders

As an IDE, Eclipse is used to work with files and folders. Eclipse creates the concept of a workspace (a group of related projects), a number of projects, and then files and folders underneath each. These resources are then used by builders to be able to create derived resources upon change, which is how Eclipse compiles Java source files into .class files.

In this chapter, we will:

  • Create a custom editor

  • Read the contents of a file

  • Create a resource file

  • Use a builder to automatically process changes

  • Integrate the builder with a nature

  • Highlight problems in the editor with markers

Using the workspace and resources


Everything in the Eclipse IDE is based on the concept of a workspace, which contains a number of projects, which in turn contain files and folders. Generically, these are all resources and are represented with a path and then either a set of contents or a set of children.

Time for action – creating an editor


The example will be based on a (made-up) markup language called minimark—which is essentially a plain text file with blank delimited paragraphs that can be translated into an HTML file. This will involve creating an editor for text-based content for minimark files.

  1. Create a new plug-in project called com.packtpub.e4.minimark.ui by going to File | New | Project | Plug-in project and fill in:

    1. Project name: com.packtpub.e4.minimark.ui

  2. Click on Next and fill in:

    1. ID: com.packtpub.e4.minimark.ui

    2. Version: 1.0.0.qualifier

    3. Name: Minimark

    4. Vendor: PACKTPUB

    5. Ensure that the Create an Activator option is selected

    6. Ensure that this plug-in will make contributions to the UI option is selected

    7. Ensure that the Create a Rich Client Application option is not selected

  3. Click on Finish and a new plug-in will be created.

  4. The next step is to create an editor for the minimark files. Open the plug-in's manifest, by right-clicking on the project and selecting Plug-in Tools | Open Manifest...

Time for action – writing the markup parser


First, the format of the markup language needs to be explained. The first line will be a title, and then subsequent paragraphs are blank-line separated. This can be translated into an HTML file as follows.

Minimark source

Translated HTML

This is the title

A paragraph with some text

Another paragraph

<html><head><title>This is the title</title></head><body><h1>This is the title</h1>

<p>
A paragraph with some text
</p>

<p>
Another paragraph
</p></body></html>
  1. Create a class called MinimarkTranslator in the com.packtpub.e4.minimark.ui package as follows:

    public class MinimarkTranslator {
      public static void convert(Reader reader, Writer writer)
       throws IOException {
        BufferedReader lines = new BufferedReader(reader);
        String line;
        String title = String.valueOf(lines.readLine());
        writer.write("<html><head><title>");
        writer...

Time for action – building the builder


Compilers (and every other kind of translator) in Eclipse are implemented with builders. These are notified when a file or a set of files are changed, and can take appropriate action. In the case of the Java builder, it translates .java source files into .class files.

  1. Open the .project file in the com.packtpub.e4.minimark.ui project. This is visible in the Navigator view, but not in the Package Explorer or other views. Alternatively, use Cmd + Shift + R on macOS (or Ctrl + Shift + R on other platforms) to open the resource by name. Builders are associated to a project within the .project file. The builder ID is referenced via the buildCommand, for example:

    <projectDescription>
      <name>com.packtpub.e4.minimark.ui</name>
      <buildSpec>
        <buildCommand>
          <name>org.eclipse.jdt.core.javabuilder</name>
        </buildCommand>
          ...
  2. To translate a .minimark file into HTML automatically, a builder is needed...

Time for action – iterating through resources


A project (represented by the IProject interface) is a top-level unit in the workspace (which is represented by the IWorkspaceRoot interface). These can contain resources (represented by the IResource interface), which are either folders or files (represented by the IFolder or IFile interfaces). They can be iterated with the members method, but this will result in the creation of IResource objects for every element processed, even if they aren't relevant. Instead, defer to the platform's internal tree by passing it a visitor that will step through each element required.

  1. Create a class MinimarkVisitor in the com.packtpub.e4.minimark.ui package that implements the IResourceProxyVisitor and IResourceDeltaVisitor interfaces.

  2. Implement the visit(IResourceProxy) method to get the name of the resource, and display a message if it finds a file whose name ends with .minimark. It should return true to allow child resources to be processed:

    public boolean...

Time for action – creating resources


The next step is to create an IFile resource for the .html file (based on the name of the .minimark file). Eclipse uses an IPath object to represent the filename from the root of the workspace. An IPath with /project/folder/file.txt refers to the file.txt file in a folder called folder contained within the project project. The root path represents the IWorkspaceRoot.

  1. In the processResource method of the MinimarkVisitor class, calculate the new filename, and use it to get an IFile object from the file's parent IContainer object:

    try {
      IFile file = (IFile) resource;
      InputStream in = file.getContents();
      String htmlName = file.getName().replace(".minimark", ".html");
      IContainer container = file.getParent();
      IFile htmlFile = container.getFile(new Path(htmlName));
  2. To create the contents of the file, an InputStream instance has to be passed to the setContents method. The easiest way to create this is to pass a ByteArrayOutputStream instance to the convert...

Time for action – implementing incremental builds


The final part of the puzzle is to implement the incremental part of the builder. Most of the builds that Eclipse performs are incremental, which means that it only compiles the files that are needed at each point. An incremental build gives a resource delta, which contains which files have been modified, added, or removed. This is implemented in an IResourceDelta interface, which is handed to the IResourceDeltaVisitor method visit. A resource delta combines an IResource instance with a flag that says whether it was added or removed.

  1. Open the MinimarkVisitor and go to the visit(IResourceDelta) method. This is used by the incremental build when individual files are changed. Since the delta already has a resource, it can be used to determine whether the file is relevant, and if so, pass it to the processResource method:

    public boolean visit(IResourceDelta delta) throws CoreException {
      IResource resource = delta.getResource();
      if(resource...

Time for action: handling deletion


The incremental builder does not handle deletion in its current implementation. To handle deletion, the IResourceDelta instance needs to be inspected to find out what kind of delta took place and handle deleted resources accordingly.

  1. Run the target Eclipse instance, and delete a .minimark file. An exception is thrown and reported to the user:

  2. To fix this issue, modify the check in the MinimarkVisitor method processResource to see whether the resource exists or not:

    private void processResource(IResource resource) throws
     CoreException {
      if (resource instanceof IFile && resource.exists()) {
  3. This solves the NullPointerException, but the generated HTML file is left behind. If the .minimark file is deleted, and there is a corresponding .html file, that can be deleted as well. Modify the visit(IResourceDelta) method as follows:

    public boolean visit(IResourceDelta delta) throws CoreException {
      boolean deleted = (IResourceDelta.REMOVED & delta.getKind...

Using natures


Although builders can be configured on a project manually, they aren't usually added directly. Instead, a project may have natures which describe the type of project; and natures can be automatically associated with builders. For example, a Java project is identified with a Java nature, and others (such as the PDE project) are identified as both a Java project and an additional nature for PDE processing. Other languages have their own natures, such as C.

Time for action – creating a nature


A nature is created by implementing the IProjectNature interface. This will be used to create a MinimarkNature class, which will allow projects to be associated with the MinimarkBuilder class.

  1. Create a class called MinimarkNature in the com.packtpub.e4.minimark.ui package as follows:

    public class MinimarkNature implements IProjectNature {
      public static final String ID =
       "com.packtpub.e4.minimark.ui.MinimarkNature";
      private IProject project;
      public IProject getProject() {
        return project;
      }
      public void setProject(IProject project) {
        this.project = project;
      }
      public void configure() throws CoreException {
      }
      public void deconfigure() throws CoreException {
      }
    }
  2. The purpose of a nature is to assist by adding (or configuring) the builders, which are associated by an ID. To make cross-referencing possible, define a constant in the MinimarkBuilder class, which can be used to refer to it by the nature:

    public class MinimarkBuilder extends...

Using markers


The final section in this chapter is devoted to markers. These are the errors and warnings that the compiler shows when it detects problems in files. They can be created automatically, typically from a builder.

Time for action – error markers if file is empty


Errors and warning markers are used to indicate if there are problems in the source files. These are used by the Eclipse compiler to indicate Java compile errors, but they are also used for non-Java errors. For example, text editors also show warnings when words are misspelled. A warning can be shown if the .minimark file is empty and the title is missing.

There isn't a simple way of accessing the file's size in Eclipse, so one heuristic is that if the generated HTML file is less than about 100 bytes then there probably wasn't much to start with anyway.

  1. Open the MinimarkVisitor class and go to the processResource method.

  2. When the HTML file is generated, put in a test to determine if the size is less than 100 bytes:

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    MinimarkTranslator.convert(new InputStreamReader(in),
     new OutputStreamWriter(baos));
    ByteArrayInputStream contents = new ByteArrayInputStream(
     baos.toByteArray());
    if (baos...

Time for action – registering a marker type


The current implementation has a flaw, in that it doesn't appear to fix the problems after they have been resolved. That's because the marker types are kept associated with a resource even if that resource is changed. The reason this isn't seen in Java files is that the builder wipes out all (Java) errors prior to the start of a build, and then adds new ones as applicable. To avoid wiping out other plug-ins' markers, each marker has an associated marker type. JDT uses this to distinguish between its markers and others contributed by different systems. This can be done for MinimarkMarker instances as well.

  1. Open the plugin.xml file in the com.packtpub.e4.minimark.ui project. Add the following extension to define a MinimarkMarker:

    <extension id="com.packtpub.e4.minimark.ui.MinimarkMarker"
     name="Minimark Marker"
     point="org.eclipse.core.resources.markers">
      <persistent value="false"/>
      <super type="org.eclipse.core.resources.problemmarker...

Summary


In this chapter, we looked at how resources are accessed, created, and processed. We used an example of a markup language to associate a builder that translated markup items into HTML when the source files were saved. We then hooked up the builder with a nature so that a project can automatically be configured with content when needed.

The next chapter will look at the E4 model and how it differs from Eclipse 3.x in creating views and contributing to a Rich Client Platform.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Eclipse Plug-in Development Beginner's Guide - Second Edition
Published in: Aug 2016Publisher: ISBN-13: 9781783980697
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Alex Blewitt

contacted on 30 aug 16 _____________ Dr Alex Blewitt has over 20 years of experience in Objective-C and has been using Apple frameworks since NeXTstep 3.0. He upgraded his NeXTstation for a TiBook when Apple released Mac OS X in 2001 and has been developing on it ever since. Alex currently works for an investment bank in London, writes for the on-line technology news site InfoQ and has published two other books for Packt publishing. He also has a number of apps on the Apple AppStore through Bandlem Limited. When he's not working on technology, and if the weather is nice, he likes to go flying from the nearby Cranfield airport. Alex writes regularly at his blog, http://alblue.bandlem.com, as well tweeting regularly from Twitter as @alblue. Acknowledgements This book would not have been possible without the ongoing love and support of my wife Amy, who has helped me through both the highs and lows of life. She gave me the freedom to work during the many late nights and weekends that it takes to produce a book and its associated code repository. She truly is the Lem of my life. I'd also like to thank my parents, Ann and Derek, for their encouragement and support during my formative years. It was this work ethic that allowed me to start my technology career as a teenager and to incorporate my first company before I was 25. I'd also like to congratulate them on their 50th wedding anniversary in 2015, and I look forward to reaching that goal with Amy. Thanks are due especially to the reviewer of this version of the book: Antonio Bello, as well as the previous version of this book: Nate Cook, James Robert and Arvid Gerstmann, who provided excellent feedback on the contents of this book during development and caught many errors in both the text and code. Any remaining errors are my own. I'd also like to thank my children Sam and Holly for inspiring me and hope that they too can achieve anything that they set their minds to. Finally, I'd like to thank Ben Moseley and Eren Kotan, both of whom introduced me to NeXT in the first place and set my career going on a twenty year journey to this book.
Read more about Alex Blewitt