Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Eclipse Plug-in Developer Guide
Previous Page Home Next Page

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.
    • createExtendedContentArea(Composite): This method creates an extra content area located above the details. For now, we will just return null because we don't need any extra fields for this simple example:
         protected Control createExtendedContentArea(Composite parent) {
            return null;
         }
    • createFilter(): Creates a new instance of a filter. In the simplest implementation you should also extend FilteredItemsSelectionDialog.ItemsFilter and implement the abstract methods. Eg.:
         protected ItemsFilter createFilter() {
            return new ItemsFilter() {
               public boolean matchItem(Object item) {
                  return matches(item.toString());
               }
               public boolean isConsistentItem(Object item) {
                  return true;
               }
            };
         }
    • fillContentProvider(FilteredItemsSelectionDialog.AbstractContentProvider, FilteredItemsSelectionDialog.ItemsFilter, org.eclipse.core.runtime.IProgressMonitor): Fills the content provider with matching items. Eg.:
         protected void fillContentProvider(AbstractContentProvider contentProvider,
            ItemsFilter itemsFilter, IProgressMonitor progressMonitor)
               throws CoreException {
            progressMonitor.beginTask("Searching", resources.size()); //$NON-NLS-1$
            for (Iterator iter = resources.iterator(); iter.hasNext();) {
               contentProvider.add(iter.next(), itemsFilter);
               progressMonitor.worked(1);
            }
            progressMonitor.done();
         }
    • getDialogSettings(): Returns the settings object that stores information about how the dialog information is persisted. This method can't return null, so we'll just return a simple settings object:
         private static final String DIALOG_SETTINGS = "FilteredResourcesSelectionDialogExampleSettings";	
      		
         protected IDialogSettings getDialogSettings() {
            	IDialogSettings settings = Activator.getDefault().getDialogSettings()
      				.getSection(DIALOG_SETTINGS);
      		if (settings == null) {
      			settings = Activator.getDefault().getDialogSettings()
      					.addNewSection(DIALOG_SETTINGS);
      		}
      		return settings;
         }
    • getElementName(Object): Returns a name for the given object. This is used to check duplicates.
         public String getElementName(Object item) {
            return item.toString();
         }
    • getItemsComparator(): Returns a comparator used to sort items. In our example we will just use standard string comparison:
         protected Comparator getItemsComparator() {
            return new Comparator() {
               public int compare(Object arg0, Object arg1) {
                  return arg0.toString().compareTo(arg1.toString());
               }
            };
         }
    • validateItem(Object): Validates that the item is a valid selection. In our example we just return an OK status, because all items are valid:
         protected IStatus validateItem(Object item) {
            return Status.OK_STATUS;
         }
  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


 
 
  Published under the terms of the Eclipse Public License Version 1.0 ("EPL") Design by Interspire