]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/dialogs/ListDialog.java
added org.jfree.feature from svn
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / dialogs / ListDialog.java
1 package org.simantics.utils.ui.dialogs;\r
2 \r
3 import java.util.Collection;\r
4 \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
22 \r
23 \r
24 /**\r
25  * @author Tuukka Lehtonen\r
26  */\r
27 public class ListDialog<T> extends Dialog {\r
28 \r
29     private static final String DIALOG = "ListDialog"; //$NON-NLS-1$\r
30 \r
31     private final Collection<T> selectables;\r
32 \r
33     private final String        title;\r
34 \r
35     private final String        description;\r
36 \r
37     private StructuredViewer         viewer;\r
38 \r
39     private IDialogSettings     dialogBoundsSettings;\r
40 \r
41     public ListDialog(Shell parent, Collection<T> selectables, String title, String description) {\r
42         super(parent);\r
43         this.selectables = selectables;\r
44         this.title = title;\r
45         this.description = description;\r
46 \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
51     }\r
52 \r
53     @Override\r
54     protected IDialogSettings getDialogBoundsSettings() {\r
55         return dialogBoundsSettings;\r
56     }\r
57 \r
58     @Override\r
59     protected void configureShell(Shell newShell) {\r
60         if (title != null) {\r
61             newShell.setText(title);\r
62         } else {\r
63             newShell.setText("Select");\r
64         }\r
65         super.configureShell(newShell);\r
66     }\r
67 \r
68     @Override\r
69     protected int getShellStyle() {\r
70         return super.getShellStyle() | SWT.RESIZE;\r
71     }\r
72 \r
73     @Override\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
79         return result;\r
80     }\r
81     \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
84     }\r
85 \r
86     @Override\r
87     protected Control createDialogArea(Composite parent) {\r
88         Composite composite = (Composite) super.createDialogArea(parent);\r
89 \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
94         }\r
95 \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
102 \r
103         applyDialogFont(composite);\r
104         return composite;\r
105     }\r
106 \r
107     @Override\r
108     protected void okPressed() {\r
109         super.okPressed();\r
110     }\r
111 \r
112     private String toLabel(T t) {\r
113         return t.toString();\r
114     }\r
115 \r
116     private final ViewerSorter sorter = new ViewerSorter() {\r
117         @SuppressWarnings("unchecked")\r
118         @Override\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
121         }\r
122     };\r
123 \r
124     class SelectableLabelProvider extends LabelProvider {\r
125         @SuppressWarnings("unchecked")\r
126         @Override\r
127         public String getText(Object element) {\r
128             return toLabel((T) element);\r
129         }\r
130     }\r
131 \r
132 }\r