]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/dialogs/ListDialog.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / dialogs / ListDialog.java
1 package org.simantics.utils.ui.dialogs;
2
3 import java.util.Collection;
4
5 import org.eclipse.jface.dialogs.Dialog;
6 import org.eclipse.jface.dialogs.IDialogSettings;
7 import org.eclipse.jface.viewers.ArrayContentProvider;
8 import org.eclipse.jface.viewers.LabelProvider;
9 import org.eclipse.jface.viewers.StructuredViewer;
10 import org.eclipse.jface.viewers.TableViewer;
11 import org.eclipse.jface.viewers.Viewer;
12 import org.eclipse.jface.viewers.ViewerSorter;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.graphics.Point;
15 import org.eclipse.swt.layout.GridData;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.swt.widgets.Control;
18 import org.eclipse.swt.widgets.Label;
19 import org.eclipse.swt.widgets.Shell;
20 import org.simantics.utils.strings.AlphanumComparator;
21 import org.simantics.utils.ui.internal.Activator;
22
23
24 /**
25  * @author Tuukka Lehtonen
26  */
27 public class ListDialog<T> extends Dialog {
28
29     private static final String DIALOG = "ListDialog"; //$NON-NLS-1$
30
31     private final Collection<T> selectables;
32
33     private final String        title;
34
35     private final String        description;
36
37     private StructuredViewer         viewer;
38
39     private IDialogSettings     dialogBoundsSettings;
40
41     public ListDialog(Shell parent, Collection<T> selectables, String title, String description) {
42         super(parent);
43         this.selectables = selectables;
44         this.title = title;
45         this.description = description;
46
47         IDialogSettings settings = Activator.getDefault().getDialogSettings();
48         dialogBoundsSettings = settings.getSection(DIALOG);
49         if (dialogBoundsSettings == null)
50             dialogBoundsSettings = settings.addNewSection(DIALOG);
51     }
52
53     @Override
54     protected IDialogSettings getDialogBoundsSettings() {
55         return dialogBoundsSettings;
56     }
57
58     @Override
59     protected void configureShell(Shell newShell) {
60         if (title != null) {
61             newShell.setText(title);
62         } else {
63             newShell.setText("Select");
64         }
65         super.configureShell(newShell);
66     }
67
68     @Override
69     protected int getShellStyle() {
70         return super.getShellStyle() | SWT.RESIZE;
71     }
72
73     @Override
74     protected Point getInitialSize() {
75         Point defaultSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
76         Point result = super.getInitialSize();
77         if (defaultSize.equals(result))
78             return new Point(500, 300);
79         return result;
80     }
81     
82     protected StructuredViewer createViewer(Composite composite) {
83         return new TableViewer(composite, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION);
84     }
85
86     @Override
87     protected Control createDialogArea(Composite parent) {
88         Composite composite = (Composite) super.createDialogArea(parent);
89
90         if (description != null) {
91             Label label = new Label(composite, 0);
92             label.setText(description);
93             label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
94         }
95
96         viewer = createViewer(composite);
97         viewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
98         viewer.setContentProvider(new ArrayContentProvider());
99         viewer.setLabelProvider(new SelectableLabelProvider());
100         viewer.setSorter(sorter);
101         viewer.setInput(selectables);
102
103         applyDialogFont(composite);
104         return composite;
105     }
106
107     @Override
108     protected void okPressed() {
109         super.okPressed();
110     }
111
112     private String toLabel(T t) {
113         return t.toString();
114     }
115
116     private final ViewerSorter sorter = new ViewerSorter() {
117         @SuppressWarnings("unchecked")
118         @Override
119         public int compare(Viewer viewer, Object e1, Object e2) {
120             return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(toLabel((T) e1), toLabel((T) e2));
121         }
122     };
123
124     class SelectableLabelProvider extends LabelProvider {
125         @SuppressWarnings("unchecked")
126         @Override
127         public String getText(Object element) {
128             return toLabel((T) element);
129         }
130     }
131
132 }