1 package com.quantum.ui.dialog;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Iterator;
8 import com.quantum.model.Displayable;
10 import org.eclipse.jface.dialogs.Dialog;
11 import org.eclipse.jface.dialogs.IDialogConstants;
12 import org.eclipse.jface.viewers.ILabelProviderListener;
13 import org.eclipse.jface.viewers.ISelectionChangedListener;
14 import org.eclipse.jface.viewers.IStructuredContentProvider;
15 import org.eclipse.jface.viewers.IStructuredSelection;
16 import org.eclipse.jface.viewers.ITableLabelProvider;
17 import org.eclipse.jface.viewers.SelectionChangedEvent;
18 import org.eclipse.jface.viewers.StructuredSelection;
19 import org.eclipse.jface.viewers.TableViewer;
20 import org.eclipse.jface.viewers.Viewer;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.graphics.Image;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Shell;
34 public class SimpleSelectionDialog extends Dialog {
36 class LabelProvider implements ITableLabelProvider {
38 public Image getColumnImage(Object element, int columnIndex) {
39 return SimpleSelectionDialog.this.image;
42 public String getColumnText(Object element, int columnIndex) {
43 if (element instanceof Displayable) {
44 return ((Displayable) element).getDisplayName();
46 return element.toString();
50 public void addListener(ILabelProviderListener listener) {
53 public void dispose() {
56 public boolean isLabelProperty(Object element, String property) {
60 public void removeListener(ILabelProviderListener listener) {
64 class ContentProvider implements IStructuredContentProvider {
66 public Object[] getElements(Object inputElement) {
67 return SimpleSelectionDialog.this.objects;
70 public void dispose() {
73 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
78 private final String title;
79 private TableViewer viewer;
80 private final Image image;
81 private final Object[] objects;
83 private List selection = Collections.synchronizedList(new ArrayList());
84 private final boolean multipleSelection;
89 public SimpleSelectionDialog(Shell parentShell, String title,
90 Object[] objects, Image image) {
91 this(parentShell, title, objects, image, false);
93 public SimpleSelectionDialog(Shell parentShell, String title,
94 Object[] objects, Image image, boolean multipleSelection) {
97 this.objects = objects;
99 this.multipleSelection = multipleSelection;
102 protected void configureShell(Shell shell) {
103 super.configureShell(shell);
104 shell.setText(title);
105 GridLayout layout = new GridLayout();
106 shell.setLayout(layout);
109 protected Control createDialogArea(Composite parent) {
111 Composite composite = new Composite(parent, 0);
112 GridLayout layout = new GridLayout();
113 composite.setLayout(layout);
114 layout.numColumns = 1;
115 layout.verticalSpacing = 1;
117 int style = SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL;
118 if (this.multipleSelection) {
121 this.viewer = new TableViewer(composite, style);
122 GridData full = new GridData(GridData.FILL_HORIZONTAL);
123 full.widthHint = 200;
124 full.heightHint = 50;
125 this.viewer.getControl().setLayoutData(full);
127 this.viewer.setLabelProvider(new LabelProvider());
128 this.viewer.setContentProvider(new ContentProvider());
130 this.viewer.setInput(this);
132 this.viewer.addSelectionChangedListener(new ISelectionChangedListener() {
134 public void selectionChanged(SelectionChangedEvent event) {
135 Button okButton = getButton(IDialogConstants.OK_ID);
136 okButton.setEnabled(!event.getSelection().isEmpty());
144 protected void createButtonsForButtonBar(Composite parent) {
145 super.createButtonsForButtonBar(parent);
146 Button okButton = getButton(IDialogConstants.OK_ID);
147 okButton.setEnabled(false);
150 protected void okPressed() {
151 IStructuredSelection selection = (IStructuredSelection) this.viewer.getSelection();
152 this.selection.clear();
153 for (Iterator i = selection.iterator(); i.hasNext();) {
154 this.selection.add(i.next());
159 * @return Returns the selectedElement.
161 public IStructuredSelection getSelection() {
162 return new StructuredSelection(this.selection);