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();
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();
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();
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();
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();
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();
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();
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.
[Update]: This has been added to the Eclipse User Interface Guidelines.









4 comments:
Did you notice that not all of those selection dialogs feature a filter field? I often find myself overwehlemed by the health of information in selection dialogs and would like to filter...
Prakash, this is a really nice summary. Would you consider converting it into a section for the UI Guidelines?
Kevin,
Sure will be happy to add it. Just let me know how to contribute it to the wiki. Will do it. You can contact me over my gmail id as well (grprakash)
Another nice blog entry about the most common dialogs provided by JFace is located under sureshkrishna .
Post a Comment