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.
- Open plugin.xml
- Click on the light bulb, select Add Missing Packages
- Save plugin.xml
- Tables
- Views
- Filters
- Filters
- Tables
- Views
- Open org.eclipse.ui.navigator.navigatorContent
- Right-click on Custom Navigator Content and select New –> commonSorter. Enter:
- class: customnavigator.sorter.SchemaCategorySorter
- id: customnavigator.sorter.schemacategorysorter
- Click on the class link and, when the New Java Class wizard opens, click Finish.
- Right click customnavigator.sorter.schemacategorysorter (commonSorter) and select New –> parentExpression
- Right click parentExpression and select New –> or
- 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
- Save plugin.xml
- 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 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:
Instead they appear as:
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:
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.
So What Just Happened?
After correcting the missing package export (which we should have done in the previous post) we:
The cat is alive and resting comfortably.