Home > Eclipse development, Software Development > Writing an Eclipse Plug-in (Part 8): Common Navigator: Adding a New Sorter Under navigatorContent

Writing an Eclipse Plug-in (Part 8): Common Navigator: Adding a New Sorter Under navigatorContent


[Just an FYI: I updated Part 7‘s code to reflect the suggestions from Simon Zambrovski so the code is slightly different. The zip file was also updated.]

[I have upgraded to Eclipse 3.6 M2. Nothing should need to be changed.]

Today’s tasks:

  • Remove the warning from plugin.xml
  • Add a customSorter to the navigator
  • Refactor the CustomProjectSchema* classes
  • Implement the SchemaCategorySorter
  • Remove the warning from plugin.xml

Remove the Warning from plugin.xml

This is code hygiene.

  1. Open plugin.xml
  2. Click on the light bulb, select Add Missing Packages
  3. Save plugin.xml
    1. The manifest editor will add a new package to Export-Package:

      MANIFEST.MF
      ...
      Export-Package: customnavigator,
       customnavigator.navigator
      

      Pat yourself on the back.

      Add a customSorter to the Navigator

      A funny thing happened when I took a good look at the categories listed under Custom Project: the Schema categories were not in the right order. I wanted them to appear as:

      • Tables
      • Views
      • Filters

      Instead they appear as:

      • Filters
      • Tables
      • Views

      In other words, they are appearing in alphabetical order instead of the arbitrary order I have determined is the correct one for my custom project. How to fix such a stain on humanity? Add a custom sorter.

      Do the following in the customnavigator plugin.xml:

      1. Open org.eclipse.ui.navigator.navigatorContent
      2. Right-click on Custom Navigator Content and select New –> commonSorter. Enter:
        • class: customnavigator.sorter.SchemaCategorySorter
        • id: customnavigator.sorter.schemacategorysorter
      3. Click on the class link and, when the New Java Class wizard opens, click Finish.
      4. Right click customnavigator.sorter.schemacategorysorter (commonSorter) and select New –> parentExpression
      5. Right click parentExpression and select New –> or
      6. Right click or and select New –> instanceof. Since we want to sort the categories under the Schema category we set the class to trigger on to be the CustomProjectSchema. Enter:
        • value: customnavigator.navigator.CustomProjectSchema
      7. Save plugin.xml

      Refactor the CustomProjectSchema* classes

      Before we implement the SchemaCategorySorter let’s do some upfront work. Refactor the constant NAME into the 3 category class CustomProjectSchemaTables, CustomProjectSchemaViews, CustomProjectSchemaFilters.

      public class CustomProjectSchemaTables implements ICustomProjectElement {
      
          public static final String NAME = "Tables"; //$NON-NLS-1$
          ...
          public String getText() {
              return NAME;
          }
          ...
      }
      
      public class CustomProjectSchemaViews implements ICustomProjectElement {
      
          public static final String NAME = "Views"; //$NON-NLS-1$
          ...
          public String getText() {
              return NAME;
          }
          ...
      }
      
      public class CustomProjectSchemaFilters implements ICustomProjectElement {
      
          public static final String NAME = "Filters"; //$NON-NLS-1$
          ...
          public String getText() {
              return NAME;
          }
          ...
      }
      

      I know, I know…there is a lot of great refactoring in these classes. Patience. All will be revealed (in another post).

      Implement the SchemaCategorySorter

      The sorter is straightforward.

      The SchemaCategorySorter works just like a Comparable object: it is expecting either a 1, 0 or -1, or more generically, a positive number, a zero or a negative number depending on how you want to sort the incoming items. We inherit from ViewerSorter as that is the superclass the New Java Class wizard selected as the parent class. Who are we to argue?

      We override compare() to make Tables first and Filters last:

      /**
       * Coder beware: this code is not warranted to do anything.
       *
       * Copyright Oct 24, 2009 Carlos Valcarcel Hidden Clause
       */
      package customnavigator.sorter;
      
      import java.text.Collator;
      
      import org.eclipse.jface.viewers.Viewer;
      import org.eclipse.jface.viewers.ViewerSorter;
      
      import customnavigator.navigator.CustomProjectSchemaFilters;
      import customnavigator.navigator.CustomProjectSchemaTables;
      import customnavigator.navigator.CustomProjectSchemaViews;
      import customnavigator.navigator.ICustomProjectElement;
      
      /**
       * @author carlos
       *
       */
      public class SchemaCategorySorter extends ViewerSorter {
      
          /**
           * 
           */
          public SchemaCategorySorter() {
              // purposely empty
          }
      
          /**
           * @param collator
           */
          public SchemaCategorySorter(Collator collator) {
              super(collator);
          }
      
          @Override
          public int compare(Viewer viewer, Object e1, Object e2) {
              String catName1 = ((ICustomProjectElement)e1).getText();
              String catName2 = ((ICustomProjectElement)e2).getText();
              
              int result = -1;
              if (catName1.equals(CustomProjectSchemaTables.NAME)) {
                  result = -1;
              } else if (catName2.equals(CustomProjectSchemaTables.NAME)) {
                  result = 1;
              } else if (catName1.equals(CustomProjectSchemaViews.NAME)) {
                  result = -1;
              } else if (catName1.equals(CustomProjectSchemaFilters.NAME)) {
                  result = 1;
              } // else result == -1
              
              return result;
          }
      
      }
      

      Remove the warning from plugin.xml

      In the plugin.xml editor go to the MANIFEST.ML tab. There is a warning icon in the left hand margin of the file. Click on the icon; it will open a window with a single suggestion for fixing the warning: Add Missing Packages. Double click Add Missing Packages. The manifest editor will add a new package to Export-Package:

      MANIFEST.MF
      ...
      Export-Package: customnavigator,
       customnavigator.navigator,
       customnavigator.sorter
      

      Go ahead. Start the runtime workbench and take a look.
      custom-navigator-end-of-part-8

      So What Just Happened?

      After correcting the missing package export (which we should have done in the previous post) we:

      • Added a new commonSorter to the navigatorContent extension
      • Created and implemented the SchemaCategorySorter
      • Minimally refactored the CustomProjectSchema* classes so their names would be available in constants
      • Exported the customnavigator.sorter in MANIFEST.MF

      The cat is alive and resting comfortably.

      Advertisements
  1. No comments yet.
  1. No trackbacks yet.

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: