Posted in
Tutorials
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:
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.
![]()
Now the last bit of info. Invoking this dialog:
FilteredPersonsSelectionDialog dialog = new FilteredPersonsSelectionDialog(window.getShell(), persons);
dialog.setTitle("Select Person");
dialog.setInitialPattern("?");
dialog.open();
| < Prev | Next > | |
|---|---|---|


