]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/dialogs/TreeDialog.java
Sync git svn branch with SVN repository r33269.
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / dialogs / TreeDialog.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 /*\r
13  * 14.7.2006\r
14  */\r
15 package org.simantics.utils.ui.dialogs;\r
16 \r
17 import org.eclipse.jface.dialogs.Dialog;\r
18 import org.eclipse.jface.dialogs.IDialogConstants;\r
19 import org.eclipse.jface.dialogs.IInputValidator;\r
20 import org.eclipse.jface.viewers.DoubleClickEvent;\r
21 import org.eclipse.jface.viewers.IDoubleClickListener;\r
22 import org.eclipse.jface.viewers.ILabelProvider;\r
23 import org.eclipse.jface.viewers.ISelection;\r
24 import org.eclipse.jface.viewers.IStructuredContentProvider;\r
25 import org.eclipse.jface.viewers.IStructuredSelection;\r
26 import org.eclipse.jface.viewers.StructuredSelection;\r
27 import org.eclipse.jface.viewers.TreeViewer;\r
28 import org.eclipse.jface.viewers.ViewerSorter;\r
29 import org.eclipse.swt.SWT;\r
30 import org.eclipse.swt.layout.GridData;\r
31 import org.eclipse.swt.widgets.Composite;\r
32 import org.eclipse.swt.widgets.Control;\r
33 import org.eclipse.swt.widgets.Label;\r
34 import org.eclipse.swt.widgets.Shell;\r
35 \r
36 /**\r
37  * TreeDialog is a dialog that contains a tree viewer.\r
38  * <p>\r
39  * Usage:\r
40  *  setInput(<input>);\r
41  *  setContentProvider(<cp>);\r
42  *  setLabelProvider(<lp>);\r
43  *  open() == Window.OK \r
44  * \r
45  * @author Toni Kalajainen\r
46  */\r
47 public class TreeDialog extends Dialog {\r
48 \r
49     String title;    \r
50     String message;\r
51     Label messageLabel; \r
52     IInputValidator validator;\r
53     Object initialSelection;\r
54     Object selected[];\r
55     TreeViewer viewer;\r
56     ILabelProvider labelProvider;\r
57     IStructuredContentProvider contentProvider;\r
58     Object input;\r
59     boolean expandTree = false;\r
60     boolean multipleSelection = false;\r
61     boolean useDoubleClick = true;\r
62     \r
63     ViewerSorter sorter;\r
64     \r
65     public TreeDialog(Shell parentShell, String dialogTitle,\r
66             String dialogMessage) {\r
67         super(parentShell);\r
68         message = dialogMessage;\r
69         title = dialogTitle;\r
70     }\r
71     \r
72     /**\r
73      * Set the title\r
74      */\r
75     protected void configureShell(Shell shell) {\r
76         super.configureShell(shell);\r
77         if (title != null) {\r
78             shell.setText(title);\r
79         }\r
80     }\r
81     \r
82     /**\r
83      * Set selection value\r
84      */\r
85     protected void buttonPressed(int buttonId) {\r
86         selected = null;\r
87         if (buttonId == IDialogConstants.OK_ID) {\r
88             ISelection sel = viewer.getSelection();\r
89             if (sel instanceof IStructuredSelection)\r
90             {\r
91                 IStructuredSelection ss = (IStructuredSelection) sel;\r
92                 if (!ss.isEmpty())\r
93                     selected = ss.toArray();\r
94             }\r
95         }\r
96         super.buttonPressed(buttonId);\r
97     }   \r
98     \r
99     protected Control createDialogArea(Composite parent) {        \r
100         // create composite\r
101         Composite composite = (Composite) super.createDialogArea(parent);\r
102         // create message\r
103         if (message != null) {\r
104             messageLabel = new Label(composite, SWT.WRAP);\r
105             messageLabel.setText(message);\r
106             GridData data = new GridData(GridData.GRAB_HORIZONTAL\r
107                     | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL\r
108                     | GridData.VERTICAL_ALIGN_CENTER);\r
109             data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);\r
110             messageLabel.setLayoutData(data);\r
111             messageLabel.setFont(parent.getFont());\r
112         }\r
113         \r
114         assert(input!=null);\r
115         assert(labelProvider!=null);\r
116         assert(contentProvider!=null);\r
117         int flags = SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION;\r
118         if (multipleSelection) flags |= SWT.MULTI;\r
119         viewer = new TreeViewer(parent, flags);\r
120         viewer.setLabelProvider(labelProvider);\r
121         viewer.setContentProvider(contentProvider);\r
122         viewer.setSorter(sorter);\r
123         viewer.setInput(input);\r
124         if (useDoubleClick)\r
125                 viewer.addDoubleClickListener(new IDoubleClickListener() {\r
126                     public void doubleClick(DoubleClickEvent event) {\r
127                         //TreeDialog.this.okPressed();\r
128                         buttonPressed(IDialogConstants.OK_ID);\r
129                     }});\r
130         \r
131     \r
132         if (expandTree)\r
133             viewer.expandAll();              \r
134         \r
135         GridData gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL | \r
136                                            GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_FILL );\r
137         gd.heightHint = 250;\r
138         gd.widthHint = 200;\r
139         viewer.getTree().setLayoutData(gd);\r
140         if (initialSelection!=null) \r
141             viewer.setSelection(new StructuredSelection(initialSelection));                \r
142         applyDialogFont(composite);\r
143         return composite;\r
144     }\r
145 \r
146     public ILabelProvider getLabelProvider() {\r
147         return labelProvider;\r
148     }\r
149 \r
150     /**\r
151      * Sets label provider\r
152      * \r
153      * Must be called before dialog is opened.\r
154      * \r
155      * @param labelProvider\r
156      */\r
157     public void setLabelProvider(ILabelProvider labelProvider) {\r
158         if (viewer != null)\r
159                 throw new IllegalStateException("Dialog is already initialized");\r
160         this.labelProvider = labelProvider;\r
161     }\r
162 \r
163     public IStructuredContentProvider getContentProvider() {\r
164         return contentProvider;\r
165     }\r
166 \r
167     /**\r
168      * Sets content provider\r
169      * \r
170      * Must be called before dialog is opened.\r
171      * \r
172      * @param contentProvider\r
173      */\r
174     public void setContentProvider(IStructuredContentProvider contentProvider) {\r
175         if (viewer != null)\r
176                 throw new IllegalStateException("Dialog is already initialized");\r
177         this.contentProvider = contentProvider;\r
178     }\r
179 \r
180     public Object[] getSelected() {\r
181         return selected;\r
182     }\r
183 \r
184     public Object getSingleSelected() {\r
185         if (selected==null || selected.length==0) return null;\r
186         return selected[0];\r
187     }\r
188 \r
189     public Object getInitialSelection() {\r
190         return initialSelection;\r
191     }\r
192 \r
193     /**\r
194      * Sets initial selection.\r
195      * \r
196      * Must be called before dialog is opened.\r
197      * \r
198      * @param initialSelection\r
199      */\r
200     public void setInitialSelection(Object initialSelection) {\r
201         if (viewer != null)\r
202                 throw new IllegalStateException("Dialog is already initialized");\r
203         this.initialSelection = initialSelection;\r
204     }\r
205 \r
206     public Object getInput() {\r
207         return input;\r
208     }\r
209 \r
210     /**\r
211      * Sets input object for the tree viewer.\r
212      * \r
213      * Must be called before dialog is opened.\r
214      * \r
215      * @param inputElement\r
216      */\r
217     public void setInput(Object inputElement) {\r
218         if (viewer != null)\r
219                 throw new IllegalStateException("Dialog is already initialized");\r
220         this.input = inputElement;\r
221     }\r
222 \r
223     public String getMessage() {\r
224         return message;\r
225     }\r
226 \r
227     /**\r
228      * Sets optional message that is shown top of the tree.\r
229      * \r
230      * Must be called before dialog is opened.\r
231      * \r
232      * @param message\r
233      */\r
234     public void setMessage(String message) {\r
235         if (viewer != null)\r
236                 throw new IllegalStateException("Dialog is already initialized");\r
237         this.message = message;\r
238     }\r
239 \r
240     public boolean isMultipleSelection() {\r
241         return multipleSelection;\r
242     }\r
243 \r
244     /**\r
245      * Sets multi-selection support for the tree viewer. Single-selection is the default. \r
246      * \r
247      * Must be called before dialog is opened.\r
248      * \r
249      * @param multipleSelection\r
250      */\r
251     public void setMultipleSelection(boolean multipleSelection) {\r
252         if (viewer != null)\r
253                 throw new IllegalStateException("Dialog is already initialized");\r
254         this.multipleSelection = multipleSelection;\r
255     }\r
256     \r
257     /**\r
258      * Use Double-click as way to close the dialog. This enabled by default.\r
259      * \r
260      * Must be called before dialog is opened.\r
261      * \r
262      * @param useDoubleClick\r
263      */\r
264     public void setUseDoubleClick(boolean useDoubleClick) {\r
265         if (viewer != null)\r
266                 throw new IllegalStateException("Dialog is already initialized");\r
267                 this.useDoubleClick = useDoubleClick;\r
268         }\r
269     \r
270     public boolean isUseDoubleClick() {\r
271                 return useDoubleClick;\r
272         }\r
273 \r
274     public boolean isExpandTree() {\r
275         return expandTree;\r
276     }\r
277 \r
278     /**\r
279      * Set the tree viewer to fully expand.\r
280      * \r
281      * Must be called before dialog is opened.\r
282      * \r
283      * @param expandTree\r
284      */\r
285     public void setExpandTree(boolean expandTree) {\r
286         if (viewer != null)\r
287                 throw new IllegalStateException("Dialog is already initialized");\r
288         this.expandTree = expandTree;\r
289     }\r
290     \r
291     /**\r
292      * Sets sorter for the tree viewer.\r
293      * \r
294      * Must be called before dialog is opened.\r
295      * \r
296      * @param sorter\r
297      */\r
298     public void setSorter(ViewerSorter sorter) {\r
299         if (viewer != null)\r
300                 throw new IllegalStateException("Dialog is already initialized");\r
301         this.sorter =sorter;\r
302     }\r
303     \r
304     public TreeViewer getViewer() {\r
305                 return viewer;\r
306         }\r
307     \r
308 }\r