Eclipse Search

Loading

Jul 17, 2008

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 missed any, add a comment and do let me know. Will add it to the list
ContainerSelectionDialog:
     You can use this dialog when you want to select a container (Project/Folder) for your new resource.
 
Example:
ContainerSelectionDialog dialog = new ContainerSelectionDialog(window.getShell(), null, true, "Select a parent:");

dialog.setTitle("Container Selection");

dialog.open();
You can even restrict the resource to be within a project/folder by passing the respective object as the second parameter for the constructor.
 ResourceSelectionDialog:
The ContainerSelectionDialog allowed you to select only one resource that too it should be a container. If you want to select multiple resources including files, this is the dialog you should be using.
 
Example:
ResourceSelectionDialog dialog = new ResourceSelectionDialog(window.getShell(), ResourcesPlugin.getWorkspace().getRoot(), "Select Resource:");

dialog.setTitle("Resource Selection");

dialog.open();
ResourceListSelectionDialog:
The above dialog is good when you want to present the entire set of resources under a parent and allow the user to select multiple resources. But if you have a set of resources and want the user to select only one from that, then probably this dialog is the one you should be using.
Example:
ResourceListSelectionDialog dialog = new ResourceListSelectionDialog(window.getShell(), resourcesArray);
dialog.setTitle("Resource Selection");
dialog.open();


ElementListSelectionDialog:
Alright. So far we have been looking at selecting something from the workspace. But what if I have some elements on my own and I want to select from that? The first dialog you would be using is ElementListSelectionDialog. The user can select an element from the set. You have to pass the elements as an array and supply a  label provider to render the element. The user can filter using wildcards as well:
 
Example:
ElementListSelectionDialog dialog = new ElementListSelectionDialog(window.getShell(), new LabelProvider());

dialog.setTitle("String Selection");

dialog.setMessage("Select a String (* = any string, ? = any char):");

dialog.setElements(new Object[] { "one", "two", "three" });

dialog.open();


 
ListSelectionDialog:
That holds good for selecting a single element. What if you want to select multiple elements from the given set? ListSelectionDialog is the answer to it. It is basically a single column TableViewer with SWT.CHECK style applied. You have to supply your own ContentProvider and LabelProvider for the TableViewer. It also has Select All & Deselect All buttons. (In the example I've used workspace and the associated label & content providers, but this dialog is not tied to IResource in anyway
Example:
ListSelectionDialog dlg = new ListSelectionDialog(window.getShell(), ResourcesPlugin.getWorkspace().getRoot(), new BaseWorkbenchContentProvider(), new WorkbenchLabelProvider(), "Select the Project:");
dlg.setTitle("Project Selection");
dlg.open();
CheckedTreeSelectionDialog:
If you have your items in a tree structure and want to select few elements from them, then CheckedTreeSelectionDialog is your choice. You have to bring your own content & label provider and the input. Again, due to sheer laziness, I'm using the workspace as the input and associated content & label providers, but remember that this dialog can work well with your own data as well.
Example:
CheckedTreeSelectionDialog dialog = new CheckedTreeSelectionDialog(window.getShell(), new WorkbenchLabelProvider(), new BaseWorkbenchContentProvider());

dialog.setTitle("Tree Selection");

dialog.setMessage("Select the elements from the tree:");

dialog.setInput(ResourcesPlugin.getWorkspace().getRoot());

dialog.open();
ElementTreeSelectionDialog:
This is the same as the above except that it will allow you to select a single element in the whole tree rather than multiple elements. Again, not tied to workspace & resources.
Example:
ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(window.getShell(), new WorkbenchLabelProvider(), new BaseWorkbenchContentProvider());
dialog.setTitle("Tree Selection");
dialog.setMessage("Select the elements from the tree:");
dialog.setInput(ResourcesPlugin.getWorkspace().getRoot());
dialog.open();
FilteredItemsSelectionDialog:
Have you used the Open Type (Ctrl + Shift + T) or Open Resource (Ctrl+Shift+R) dialog? Its similar to the ElementListSelectionDialog, but it has more features. It can select multiple items, display a detail pane about the item that is currently selected, it can even remember your previous selections; store them in history and present them before the other choices. 
FilteredItemsSelectionDialog is the key. Its an abstract class and you have to extend it and create a concrete implementation to use it. I'll resever the details of extending it for a separate tip, till then you can go thru the JavaDoc.



[Update]: This has been added to the Eclipse User Interface Guidelines.

10 comments:

  1. Hi, thanks for the article, I'm looking for a "TableSelectionDialog"...is it possibile that it doesn't exist?

    Any hint?
    Regards
    Raffaele

    ReplyDelete
  2. AFAIK, there is no API for creating a Table based SelectionDialog. You should be creating on your own.

    ReplyDelete
  3. Thanks Prakash,

    In your opinion, developing an RCP app, my application specific dialogs should extends swt.Dialog or JFace.Dialog ?
    Which are main differences?

    Regards

    ReplyDelete
  4. JFace Dialog. I mostly use the TrayDialog, which gives you the OK and Cancel buttons. And also if you use the SWT dialog, you should be running the event loop by yourself.

    ReplyDelete
  5. Hi,

    Thanks for a great post on dialogs! =)

    I'm rather new to Eclipse Plugin development and so i have been pulling my hair out to find a clue on how to make a "browse" dialog (source folder) like the "standard" one there is in eclipse ide for eg. new class.

    I've been trying to use the ElementTreeSelection dialog, but cant really get the right combination of label provider and content provider plus whichever filter/validator i might also need.

    So i guess my question is - did someone hide a "source folder selection dialog" somewhere in the api's? Or can you point me to a good place to look for source code to the one included in the normal eclipse ide?

    ReplyDelete
  6. @Peter,
    Are you talking about the Java Source folder? In the New Class Wizard provided by JDT, I could see a Browse button for the Source Folder field. Not sure whether there is an API, probably you can look into the code to get an idea of how its done.

    ReplyDelete
  7. Yeah thats the one im talking about. The goal is to make a custom wizard page, for doing some source code generation. And for that wizard page im trying to reuse the "Select source folder" dialog (the one you get when you hit browse).

    I did try to look into the code - but i couldnt find the source code. And looking at decompiled code really didnt help me much. Got any pointers to where i can find the source code for the new class wizard?

    ReplyDelete
  8. @Peter,
    Which package of Eclipse are you using? You would need the one with the sources included (else you need to get it from CVS)

    http://www.eclipse.org/downloads/packages/compare-packages

    ReplyDelete
  9. Thank you very much for this post! :)

    ReplyDelete
  10. @Peter
    Did you find a way to use "Source Folder Selection" dialog?

    ReplyDelete