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