1 package org.simantics.utils.ui.dialogs;
\r
3 import java.util.Collection;
\r
5 import org.eclipse.jface.dialogs.Dialog;
\r
6 import org.eclipse.jface.dialogs.IDialogSettings;
\r
7 import org.eclipse.jface.viewers.ArrayContentProvider;
\r
8 import org.eclipse.jface.viewers.LabelProvider;
\r
9 import org.eclipse.jface.viewers.StructuredViewer;
\r
10 import org.eclipse.jface.viewers.TableViewer;
\r
11 import org.eclipse.jface.viewers.Viewer;
\r
12 import org.eclipse.jface.viewers.ViewerSorter;
\r
13 import org.eclipse.swt.SWT;
\r
14 import org.eclipse.swt.graphics.Point;
\r
15 import org.eclipse.swt.layout.GridData;
\r
16 import org.eclipse.swt.widgets.Composite;
\r
17 import org.eclipse.swt.widgets.Control;
\r
18 import org.eclipse.swt.widgets.Label;
\r
19 import org.eclipse.swt.widgets.Shell;
\r
20 import org.simantics.utils.strings.AlphanumComparator;
\r
21 import org.simantics.utils.ui.internal.Activator;
\r
25 * @author Tuukka Lehtonen
\r
27 public class ListDialog<T> extends Dialog {
\r
29 private static final String DIALOG = "ListDialog"; //$NON-NLS-1$
\r
31 private final Collection<T> selectables;
\r
33 private final String title;
\r
35 private final String description;
\r
37 private StructuredViewer viewer;
\r
39 private IDialogSettings dialogBoundsSettings;
\r
41 public ListDialog(Shell parent, Collection<T> selectables, String title, String description) {
\r
43 this.selectables = selectables;
\r
45 this.description = description;
\r
47 IDialogSettings settings = Activator.getDefault().getDialogSettings();
\r
48 dialogBoundsSettings = settings.getSection(DIALOG);
\r
49 if (dialogBoundsSettings == null)
\r
50 dialogBoundsSettings = settings.addNewSection(DIALOG);
\r
54 protected IDialogSettings getDialogBoundsSettings() {
\r
55 return dialogBoundsSettings;
\r
59 protected void configureShell(Shell newShell) {
\r
60 if (title != null) {
\r
61 newShell.setText(title);
\r
63 newShell.setText("Select");
\r
65 super.configureShell(newShell);
\r
69 protected int getShellStyle() {
\r
70 return super.getShellStyle() | SWT.RESIZE;
\r
74 protected Point getInitialSize() {
\r
75 Point defaultSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
\r
76 Point result = super.getInitialSize();
\r
77 if (defaultSize.equals(result))
\r
78 return new Point(500, 300);
\r
82 protected StructuredViewer createViewer(Composite composite) {
\r
83 return new TableViewer(composite, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION);
\r
87 protected Control createDialogArea(Composite parent) {
\r
88 Composite composite = (Composite) super.createDialogArea(parent);
\r
90 if (description != null) {
\r
91 Label label = new Label(composite, 0);
\r
92 label.setText(description);
\r
93 label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
\r
96 viewer = createViewer(composite);
\r
97 viewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
\r
98 viewer.setContentProvider(new ArrayContentProvider());
\r
99 viewer.setLabelProvider(new SelectableLabelProvider());
\r
100 viewer.setSorter(sorter);
\r
101 viewer.setInput(selectables);
\r
103 applyDialogFont(composite);
\r
108 protected void okPressed() {
\r
112 private String toLabel(T t) {
\r
113 return t.toString();
\r
116 private final ViewerSorter sorter = new ViewerSorter() {
\r
117 @SuppressWarnings("unchecked")
\r
119 public int compare(Viewer viewer, Object e1, Object e2) {
\r
120 return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(toLabel((T) e1), toLabel((T) e2));
\r
124 class SelectableLabelProvider extends LabelProvider {
\r
125 @SuppressWarnings("unchecked")
\r
127 public String getText(Object element) {
\r
128 return toLabel((T) element);
\r