In a previous tip, you have seen various Selection Dialogs in Eclipse. One thing which was not explained in it was FilteredItemsSelectionDialog, as it deserves a tip on its own. In this tip, I'll explain how to extend that class.

The Open Resource Dialog and Open Type Dialog are the ones you might be using very frequently. They are subclasses of FilteredItemsSelectionDialog. The dialog looks like this:

image

Lets see what does it makes to create a simple dialog like this which allows us to select some Persons. First step is to extend the FilteredItemsSelectionDialog:

public class FilteredPersonsSelectionDialog extends FilteredItemsSelectionDialog {

private final List<Person> persons;

public FilteredPersonsSelectionDialog(Shell shell, List<Person> persons) {
super(shell);
this.persons = persons;
setListLabelProvider(getListLabelProvider());
setDetailsLabelProvider(getDetailsLabelProvider());
setSelectionHistory(new PersonHistory());
}

@Override
protected void fillContentProvider(AbstractContentProvider contentProvider,
ItemsFilter itemsFilter, IProgressMonitor progressMonitor){

progressMonitor.beginTask(<span style="color: #006080">"Looking for persons...", persons.size());
for (Person person : persons) {
contentProvider.add(person, itemsFilter);
progressMonitor.worked(1);
}

}

private static final String SETTINGS = FilteredPersonsSelectionDialog.class
.getCanonicalName();

@Override
protected IDialogSettings getDialogSettings() {

IDialogSettings settings = Activator.getDefault().getDialogSettings().getSection(SETTINGS);

if (settings == null) {
settings = Activator.getDefault().getDialogSettings()
.addNewSection(SETTINGS);
}

return settings;
}

@Override
protected ItemsFilter createFilter() {
return new PersonFilter();
}

// other members
}

When you have hundreds of thousands of items, adding it to the list up front is a huge task. So ideally, what happens is the dialog is displayed first and then the fillContentProvider is called when the user starts typing. (Or if you have specified the initial pattern, then its called as soon as the dialog is displayed to the user). For the sake of simplicity, I'm passing in all the items in the constructor and adding them to the contentProvider in the loop. This can't happen if the number of items is huge. Ideally you can specify the place where to search for thru the constructor and in the fillContentProvider, you start searching for all the items and add them. The progress will be shown below the search text box.

Apart from this FilteredPersonsSelectionDialog class, you would need few helper classes. The first one is the filter class, which should extend ItemFilter. This decides how to filter the items with the query provided in the search text box. You can decide on the wildcard strings, whether CamelCase searching is allowed etc.

The next one is related to the history. The FilteredItemsSelectionDialog can remember the selection from the previous searches. To enable that, you need to extend the SelectionHistory and set it as we have did it in the line number 10. You need to override the abstract methods in the SelectionHistory to store & restore the item from the memento. The location where the memento saved into is dictated by the getDialogSettings().

The last set of classes are the LabelProviders. You need two of them. One for displaying in the list and the other for displaying in the details area, which should ideally provide more information than the first one.

image

Now the last bit of info. Invoking this dialog:

FilteredPersonsSelectionDialog dialog = new FilteredPersonsSelectionDialog(window.getShell(), persons);
dialog.setTitle("Select Person");
dialog.setInitialPattern("?");
dialog.open();
More articles :

» Top 10 mistakes in Eclipse Plug-in Development

Having trained a lot of new comers to the Eclipse plug-in development, I've seen certain common mistakes repeated all the time. I've tried to compile a top 10 list of such common mistakes, so next time you hit them, you will know that you are not...

» How to open an external file in Eclipse

Background Based on work in SWT () and the native launcher (), the Eclipse IDE now supports opening a file from the command line, or opening a file that has been associated with the Eclipse executable ().

» Selection Dialogs in Eclipse

If you are an Eclipse Plug-in developer, you must have used the MessageDialog. There are many other Dialogs provided by Eclipse Platform are reusable and part of the API. I'll try to explain the various selection dialogs that I know of. In case I've...

» e4: First e4 RCP Application

The next big thing in Eclipse is Eclipse 4.0 dubbed as e4. It will be released in 2010. That doesn't mean that the 3.x stream will be deprecated or discontinued. The 3.x releases will go on for "few" years till everyone boards the 4.0. But clearly...

» A glimpse at the Faceted Project Framework

If you haven't noticed, the Faceted Project Framework from WTP is now proposed as a separate project. I like the framework very much, probably because my very first Eclipse Plug-in is simply a WTP Facet :-) When I wrote that plug-in, there was no...

Subscribe To

Unless stated, all the text contents of this site is available under Eclipse Public License