Creating a custom filtered item 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 class extending org.eclipse.ui.dialogs.FilteredItemsSelectionDialog. Let's name it FilteredResourcesSelectionDialogExample.
  2. 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);
          }
       }
    
  3. Now, let's implement abstract methods from the FilteredItemsSelectionDialog class.
  4. The resulting dialog looks as follows:

    Screen shot of a simple search dialog