1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
15 package org.simantics.utils.ui.dialogs;
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;
37 * TreeDialog is a dialog that contains a tree viewer.
41 * setContentProvider(<cp>);
42 * setLabelProvider(<lp>);
45 * @author Toni Kalajainen
47 public class TreeDialog extends Dialog {
52 IInputValidator validator;
53 Object initialSelection;
56 ILabelProvider labelProvider;
57 IStructuredContentProvider contentProvider;
59 boolean expandTree = false;
60 boolean multipleSelection = false;
61 boolean useDoubleClick = true;
65 public TreeDialog(Shell parentShell, String dialogTitle,
66 String dialogMessage) {
68 message = dialogMessage;
75 protected void configureShell(Shell shell) {
76 super.configureShell(shell);
85 protected void buttonPressed(int buttonId) {
87 if (buttonId == IDialogConstants.OK_ID) {
88 ISelection sel = viewer.getSelection();
89 if (sel instanceof IStructuredSelection)
91 IStructuredSelection ss = (IStructuredSelection) sel;
93 selected = ss.toArray();
96 super.buttonPressed(buttonId);
99 protected Control createDialogArea(Composite parent) {
101 Composite composite = (Composite) super.createDialogArea(parent);
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());
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);
125 viewer.addDoubleClickListener(new IDoubleClickListener() {
126 public void doubleClick(DoubleClickEvent event) {
127 //TreeDialog.this.okPressed();
128 buttonPressed(IDialogConstants.OK_ID);
135 GridData gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL |
136 GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_FILL );
139 viewer.getTree().setLayoutData(gd);
140 if (initialSelection!=null)
141 viewer.setSelection(new StructuredSelection(initialSelection));
142 applyDialogFont(composite);
146 public ILabelProvider getLabelProvider() {
147 return labelProvider;
151 * Sets label provider
153 * Must be called before dialog is opened.
155 * @param labelProvider
157 public void setLabelProvider(ILabelProvider labelProvider) {
159 throw new IllegalStateException("Dialog is already initialized");
160 this.labelProvider = labelProvider;
163 public IStructuredContentProvider getContentProvider() {
164 return contentProvider;
168 * Sets content provider
170 * Must be called before dialog is opened.
172 * @param contentProvider
174 public void setContentProvider(IStructuredContentProvider contentProvider) {
176 throw new IllegalStateException("Dialog is already initialized");
177 this.contentProvider = contentProvider;
180 public Object[] getSelected() {
184 public Object getSingleSelected() {
185 if (selected==null || selected.length==0) return null;
189 public Object getInitialSelection() {
190 return initialSelection;
194 * Sets initial selection.
196 * Must be called before dialog is opened.
198 * @param initialSelection
200 public void setInitialSelection(Object initialSelection) {
202 throw new IllegalStateException("Dialog is already initialized");
203 this.initialSelection = initialSelection;
206 public Object getInput() {
211 * Sets input object for the tree viewer.
213 * Must be called before dialog is opened.
215 * @param inputElement
217 public void setInput(Object inputElement) {
219 throw new IllegalStateException("Dialog is already initialized");
220 this.input = inputElement;
223 public String getMessage() {
228 * Sets optional message that is shown top of the tree.
230 * Must be called before dialog is opened.
234 public void setMessage(String message) {
236 throw new IllegalStateException("Dialog is already initialized");
237 this.message = message;
240 public boolean isMultipleSelection() {
241 return multipleSelection;
245 * Sets multi-selection support for the tree viewer. Single-selection is the default.
247 * Must be called before dialog is opened.
249 * @param multipleSelection
251 public void setMultipleSelection(boolean multipleSelection) {
253 throw new IllegalStateException("Dialog is already initialized");
254 this.multipleSelection = multipleSelection;
258 * Use Double-click as way to close the dialog. This enabled by default.
260 * Must be called before dialog is opened.
262 * @param useDoubleClick
264 public void setUseDoubleClick(boolean useDoubleClick) {
266 throw new IllegalStateException("Dialog is already initialized");
267 this.useDoubleClick = useDoubleClick;
270 public boolean isUseDoubleClick() {
271 return useDoubleClick;
274 public boolean isExpandTree() {
279 * Set the tree viewer to fully expand.
281 * Must be called before dialog is opened.
285 public void setExpandTree(boolean expandTree) {
287 throw new IllegalStateException("Dialog is already initialized");
288 this.expandTree = expandTree;
292 * Sets sorter for the tree viewer.
294 * Must be called before dialog is opened.
298 public void setSorter(ViewerSorter sorter) {
300 throw new IllegalStateException("Dialog is already initialized");
304 public TreeViewer getViewer() {