Home > Eclipse development, Software Development, Testing > Writing an Eclipse Plug-in (Part 5): Adding Icons and A New Project Structure

Writing an Eclipse Plug-in (Part 5): Adding Icons and A New Project Structure


It has been a while since I lasted posted on the implementation of a custom Eclipse project. Well, I’m back.

In the last posts we created:

  • a plug-in project that created a new project wizard to get the name and location of a new project
  • a navigator to display the project information
  • a testable class that created the actual project and tests to go with it

In addition we configured the new wizard to open a custom perspective and added the code to make the automatic opening of the project related perspective open automatically.

Not bad for 5 mostly empty classes, 1 class that actually does something, 1 test class and a configuration file.

Since the last post I have reconsidered including the custom navigator in the original plug-in so in this post we will remove it from the commonplugin plug-in and add a separate plug-in for the navigator instead. I hope doing that will make the navigator easier to maintain.

Today’s task list:

  1. Add icons to the Custom Project and Custom Perspective.
  2. Create a default folder structure at project creation.
  3. Create a new plug-in and add a custom navigator to it.
  4. Remove the custom navigator from the commonplugin.
  5. Integrate the custom navigator with the original plug-in (at this point we will be back to where we started. Woo hoo).
  6. Add an icon when a Custom Project is created.
  7. The custom navigator will display only projects of the proper nature with a list of categories that will eventually contain files taken from the physical folder structure.

Sounds like a lot for one blog. I will probably break this up into 2 parts.

Add Custom Icons to the Project and Perspective

Have I mentioned how important I think standards are (except when they are not)? Well, if you are doing any kind of plug-in development on Eclipse you should read the User Interface Guidelines for Eclipse which spell out in excruciating detail the things you should be doing to adhere to the look-and-feel of the really really cool platform.

In my case I care that the icons that appear next to perspectives, projects, folders and files are all supposed to be 16×16 pixels. Here are the two I used for this installment of this edge-of-your-seat blog (displayed as 32×32).

perspective project-folder

There is nothing very fancy about them. I had considered using Underdog and company as the icons, but I changed my mind (do you know how hard it is to find a decent icon of Riff Raff? Way harder than you think).

Giving our Custom Project Wizard an icon means:

  1. Go to the plugin.xml Extensions tab
  2. Open the org.eclipse.ui.newWizards extension.
  3. Select Custom Project (wizard) on the right and to the left click the Browse button on the same line as icon.
  4. Select your picture file.
  5. Save plugin.xml.

You should see your icon appear next to the Custom Project (wizard) extension entry.

menu-editor-new-wizard-with-icon

Giving our Custom Perspective an icon means:

  1. Open the org.eclipse.ui.perspectives extension.
  2. Select Custom Plug-in Perspective (perspective) on the right and to the left click the Browse button on the same line as icon.
  3. Select your picture file.
  4. Save plugin.xml.

You should see your icon appear next to the Custom Plug-in Perspective (perspective) extension entry.

menu-editor-perspective-with-icon

Quick test:

  1. Start the runtime workbench.
  2. Select Window –> Open Perspective –> Other
  3. The icon should appear next to the Custom Plug-in Perspective entry in the Open Perspective dialog.
  4. open-perspective-custom-plug-in-icon

  5. Click Cancel. First test passed.
  6. Press Ctrl+N.
  7. Open the Custom Wizards foilder of the New dialog.
  8. The icon should appear next to the Custom Project entry.
  9. new-dialog-custom-project-icon

  10. Click Cancel and exit the runtime workbench. The second test passed.

One down. Dozens to go.

Create a Default Folder Structure at Project Creation

There was a test of the folder structure located in CustomProjectSupportTest that looked for a rather bogus project structure that looked something like:
parent
  |- child1-1
    |- child2
  |- child1-2
    |- child2
    |- child3

The new structure is going to use the project folder as its root (certainly inspired) and will create three child folders: one to store a schema file, one to store deployment XML files and the other to store some Java files. This, of course, is just pretend as I have no hidden custom project type I am creating this for. In other words, this structure is no less bogus than the previous one. The names are just recognizable; in other words, easier for me.

project-folder
|- schema
|- deployment-files
|- clause
  |- java
    |- source
      |- hidden-clause

The schema folder will contain an XML file. This represents one category.

The deployment-files and clause folders are related. They represent another category. The deployment-files folder will contain an XML file. The clause/java/source folder will contain Java code.

When we get around to creating a custom navigator the two categories will be displayed and the contents of the related folders will be properly shown.

The original test code in CustomProjectSupportTest looked like this:

    private void assertFolderStructureIn(String projectPath) {
        String[] paths = { "parent/child1-1/child2", "parent/child1-2/child2/child3" };
        for (String path : paths) {
            File file = new File(projectPath + "/" + path);
            if (!file.exists()) {
                Assert.fail("Folder structure " + path + " does not exist.");
            }
        }
    }

Change the path assignment to:

    private void assertFolderStructureIn(String projectPath) {
        String[] paths = {
                    "schema", //$NON-NLS-1$
                    "deployment-files/java", //$NON-NLS-1$
                    "clause/java/source/hidden-clause"}; //$NON-NLS-1$
...
    }

When you run the test it should fail. Update CustomProjectSupport.createProject() with the new paths:

    public static IProject createProject(String projectName, URI location) {
        Assert.isNotNull(projectName);
        Assert.isTrue(projectName.trim().length() > 0);

        IProject project = createBaseProject(projectName, location);
        try {
            addNature(project);

            String[] paths = {
                    "schema", //$NON-NLS-1$
                    "deployment-files/java", //$NON-NLS-1$
                    "clause/java/source/hidden-clause"}; //$NON-NLS-1$
            addToProjectStructure(project, paths);
        } catch (CoreException e) {
            e.printStackTrace();
            project = null;
        }

        return project;
    }

Run the test and it should pass.

As a more complete test, execute the runtime workbench, create a project, open the regular Navigator and check out the folder structure. Life is good…at least for this step.

Create the Custom Navigator Plug-in

Let’s create a new plug-in just for the Custom Navigator.

  1. Press Ctrl+N and select Plug-in Project
  2. Enter:
    • Project Name: customnavigator
    • Eclipse version: 3.4
  3. Click Next
    • Name: Custom Navigator Plug-in
    • Activator: customnavigator.Activator
  4. Click Finish.

In plugin.xml:
Overview tab:
Name: Custom Navigator Plug-in

Extensions Tab (this is actually cut-and-pasted from Part 2 with minor revisions):

  1. Click Add, click the Extension Wizards tab and select the Common Navigator View template.
  2. Click Next
  3. Enter the following:
    • View Id: customnavigator.navigator
    • View Name: Custom Plug-in Navigator
    • Uncheck Add to Resource Perspective
  4. Click Finish.
  5. If a dialog opens requesting to save changes click Yes. Two extensions have been added as well as three dependencies.

Just for yucks, execute the runtime workbench. Go to Window –> Show View –> Other –> Other –> Custom Navigator. The Custom Navigator should be listed with a large red square next to it (very catchy icon on the same level as a pointy stick to the eye).

A Metaphorical 90 Degree Turn

When you try to run the custom navigator plug-in you may find that it doesn’t run as cleanly as you would like. At least, I found that various things would be visible that I did not expect.

So, from the customnavigator plugin.xml Overview tab, after you click on Launch An Eclipse Application and kill the runtime workbench, open the Run Configurations dialog (Run –> Run Configurations or any of about 8 other ways). Make sure the following are set in their respective tabs:

  • Main
    • Location: ${workspace_loc}/../runtime-customnavigator
    • [check] Clear: workspace
  • Plug-ins
    • Launch with: plug-ins selected below only [click Deselect All, check customnavigator, and click Add Required Plug-ins]
    • Check Include optional dependencies when computing required plug-ins
    • Uncheck Add new workspace plug-ins to this launch configuration automatically
    • Check Validate plug-ins automatically prior to launching
  • Configuration
    • Check Use Default Location
    • Check Clear the configuration area before launching

The above should keep the runtime workbench working in an isolated way so anything we do is not affected by things that shouldn’t be there.

We now metaphorically turn back 90 degrees.

Amazing how simple some of this can be…well, until we actually try to make it do something. Another few steps and we shall see what we shall see.

Remove the Navigator from the commonplugin

Time to perform a navigatorectomy.

From the customplugin plug-in (not from customnavigator!) remove:

  • org.eclipse.ui.views
  • org.eclipse.ui.navigator.viewer

The easiest way to do the above is to right click on the extension and select Delete. Save customplugin plugin.xml when you are done.

Run the runtime workbench for the customplugin plug-in. Create a Custom Project. Everything should be the same except that the Custom Navigator will not appear when the Custom Plug-in Perspective opens.

We’ll take care of that next. Close the runtime workbench.

Integrate the Navigator with commonplugin

In the customplugin plugin.xml file’s Extensions tab select org.eclipse.ui.perspectiveExtensions --> customplugin.perspective (perspectiveExtension) --> customplugin.navigator (view). In the ID field enter the name of the new navigator: customnavigator.navigator. Save the commonplugin plugin.xml file.

Open the Run Configurations dialog for customplugin and go to the Plug-ins tab. Check customnavigator and click Run. From the runtime workbench create a new Custom Project, say Yes to opening the custom perspective and the Custom Navigator should appear where it was before displaying the new Custom Project.

We are now back to where we started. Woo hoo.

What did we do this time around?

  1. Added icons to the Custom Project and Custom Perspective.
  2. Created a default folder structure at project creation.
  3. Created a new plug-in and add a custom navigator to it.
  4. Removed the custom navigator from the commonplugin.
  5. Integrated the custom navigator with the original plug-in.

Not bad for an old guy.

Next time I will show you how to:

  1. Add an icon when a Custom Project is created.
  2. Display in the Custom Navigator only projects of the proper nature with a list of categories that will eventually contain files taken from the physical folder structure.

I will check out the cat’s health next time.

Advertisements
  1. vivek
    January 6, 2012 at 6:29 pm

    Amazing man…I am starting to like this…Thank you so much.

  2. Federico Sellanes
    January 16, 2012 at 2:21 pm

    Excelent.

    Maybe you can clarify that you must create a new configuration for “customplugin.navigator”.

    • Federico Sellanes
      January 16, 2012 at 2:21 pm

      This is a good help for noobs like me ^^)

      • Federico Sellanes
        January 16, 2012 at 2:22 pm

        Sorry, was not for “customplugin.navigator”, was for “customnavigator” plug in.

  3. fat
    July 27, 2012 at 3:07 am

    thanks for the tutorial it’s amazing, you said life is good but not for me now because eclipse gives a lot of errors in the console when I create the project and then I don’t find the strucure mentionned above nothing happens
    please I need you help

  4. Muhammad Elsayed
    August 24, 2014 at 8:32 am

    Thank you so much for your tutorial. I have problem with the step “Remove the Navigator from the commonplugin”. My project is not working after it and I’m not able to solve it.
    could you please help

  1. August 2, 2011 at 5:30 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: