Creating a custom filtered items selection dialog

In this example, we will contribute a basic search dialog to illustrate the steps needed to create a custom subclass of FilteredItemsSelectionDialog.

  1. Create a new Plug-in Project using Hello, world template.
  2. Create a class extending org.eclipse.ui.dialogs.FilteredItemsSelectionDialog. Let's name it FilteredResourcesSelectionDialogExample.
  3. Choose a source of the resources that will be used during filtering. In our example we will generate our own set of random strings as follows:
       private static ArrayList resources = new ArrayList();
       
       static {
          generateRescourcesTestCases('A', 'C', 8, ""); //$NON-NLS-1$
          generateRescourcesTestCases('a', 'c', 4, ""); //$NON-NLS-1$
       }
    
       private static void generateRescourcesTestCases(char startChar, char endChar, int length, String resource){
          for (char ch = startChar; ch <= endChar; ch++) {
             String res = resource + String.valueOf(ch);
             if (length == res.length()) 
                resources.add(res);
             else if ((res.trim().length() % 2) == 0)
                generateRescourcesTestCases(Character.toUpperCase((char)(startChar + 1)), Character.toUpperCase((char)(endChar + 1)), length, res);
             else 
                generateRescourcesTestCases(Character.toLowerCase((char)(startChar + 1)), Character.toLowerCase((char)(endChar + 1)), length, res);
          }
       }
    
  4. Now, let's implement abstract methods from the FilteredItemsSelectionDialog class.
  5. Add title of dialog and set simple implementation of SelectionHistory on dialog:
    	public FilteredResourcesSelectionDialogExample(Shell shell, boolean multi) {
    	   super(shell, multi);
    	   setTitle("Filtered Resources Selection Dialog Example");
    	   setSelectionHistory(new ResourceSelectionHistory());
    	}
    	
    	private class ResourceSelectionHistory extends SelectionHistory {
    	   protected Object restoreItemFromMemento(IMemento element) {
    		  return null; 
    	   }
    	   protected void storeItemToMemento(Object item, IMemento element) {
    	   }
    	}
  6. Change run(IAction) method from SimpleAction to:
    	public void run(IAction action) {
    	   Shell shell = new Shell();
    	   FilteredItemsSelectionDialog dialog = new FilteredResourcesSelectionDialogExample(shell, true);
    	   dialog.setInitialPattern("a");
    	   dialog.open();
    	}
  7. Change tooltip of SimpleAction from "Hello, Eclipse world" to "Filtered Items Selection Dialog Example".
  8. Run Eclipse with created plug-in.
  9. The resulting dialog looks as follows:

    Screen shot of a simple search dialog