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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
15 package org.simantics.utils.ui.dialogs;
\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
37 * TreeDialog is a dialog that contains a tree viewer.
\r
40 * setInput(<input>);
\r
41 * setContentProvider(<cp>);
\r
42 * setLabelProvider(<lp>);
\r
43 * open() == Window.OK
\r
45 * @author Toni Kalajainen
\r
47 public class TreeDialog extends Dialog {
\r
51 Label messageLabel;
\r
52 IInputValidator validator;
\r
53 Object initialSelection;
\r
56 ILabelProvider labelProvider;
\r
57 IStructuredContentProvider contentProvider;
\r
59 boolean expandTree = false;
\r
60 boolean multipleSelection = false;
\r
61 boolean useDoubleClick = true;
\r
63 ViewerSorter sorter;
\r
65 public TreeDialog(Shell parentShell, String dialogTitle,
\r
66 String dialogMessage) {
\r
68 message = dialogMessage;
\r
69 title = dialogTitle;
\r
75 protected void configureShell(Shell shell) {
\r
76 super.configureShell(shell);
\r
77 if (title != null) {
\r
78 shell.setText(title);
\r
83 * Set selection value
\r
85 protected void buttonPressed(int buttonId) {
\r
87 if (buttonId == IDialogConstants.OK_ID) {
\r
88 ISelection sel = viewer.getSelection();
\r
89 if (sel instanceof IStructuredSelection)
\r
91 IStructuredSelection ss = (IStructuredSelection) sel;
\r
93 selected = ss.toArray();
\r
96 super.buttonPressed(buttonId);
\r
99 protected Control createDialogArea(Composite parent) {
\r
100 // create composite
\r
101 Composite composite = (Composite) super.createDialogArea(parent);
\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
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
133 viewer.expandAll();
\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
146 public ILabelProvider getLabelProvider() {
\r
147 return labelProvider;
\r
151 * Sets label provider
\r
153 * Must be called before dialog is opened.
\r
155 * @param labelProvider
\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
163 public IStructuredContentProvider getContentProvider() {
\r
164 return contentProvider;
\r
168 * Sets content provider
\r
170 * Must be called before dialog is opened.
\r
172 * @param contentProvider
\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
180 public Object[] getSelected() {
\r
184 public Object getSingleSelected() {
\r
185 if (selected==null || selected.length==0) return null;
\r
186 return selected[0];
\r
189 public Object getInitialSelection() {
\r
190 return initialSelection;
\r
194 * Sets initial selection.
\r
196 * Must be called before dialog is opened.
\r
198 * @param initialSelection
\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
206 public Object getInput() {
\r
211 * Sets input object for the tree viewer.
\r
213 * Must be called before dialog is opened.
\r
215 * @param inputElement
\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
223 public String getMessage() {
\r
228 * Sets optional message that is shown top of the tree.
\r
230 * Must be called before dialog is opened.
\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
240 public boolean isMultipleSelection() {
\r
241 return multipleSelection;
\r
245 * Sets multi-selection support for the tree viewer. Single-selection is the default.
\r
247 * Must be called before dialog is opened.
\r
249 * @param multipleSelection
\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
258 * Use Double-click as way to close the dialog. This enabled by default.
\r
260 * Must be called before dialog is opened.
\r
262 * @param useDoubleClick
\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
270 public boolean isUseDoubleClick() {
\r
271 return useDoubleClick;
\r
274 public boolean isExpandTree() {
\r
279 * Set the tree viewer to fully expand.
\r
281 * Must be called before dialog is opened.
\r
283 * @param expandTree
\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
292 * Sets sorter for the tree viewer.
\r
294 * Must be called before dialog is opened.
\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
304 public TreeViewer getViewer() {
\r