--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+/*\r
+ * 14.7.2006\r
+ */\r
+package org.simantics.utils.ui.dialogs;\r
+\r
+import org.eclipse.jface.dialogs.Dialog;\r
+import org.eclipse.jface.dialogs.IDialogConstants;\r
+import org.eclipse.jface.dialogs.IInputValidator;\r
+import org.eclipse.jface.viewers.DoubleClickEvent;\r
+import org.eclipse.jface.viewers.IDoubleClickListener;\r
+import org.eclipse.jface.viewers.ILabelProvider;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.jface.viewers.IStructuredContentProvider;\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.jface.viewers.StructuredSelection;\r
+import org.eclipse.jface.viewers.TreeViewer;\r
+import org.eclipse.jface.viewers.ViewerSorter;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.layout.GridData;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Control;\r
+import org.eclipse.swt.widgets.Label;\r
+import org.eclipse.swt.widgets.Shell;\r
+\r
+/**\r
+ * TreeDialog is a dialog that contains a tree viewer.\r
+ * <p>\r
+ * Usage:\r
+ * setInput(<input>);\r
+ * setContentProvider(<cp>);\r
+ * setLabelProvider(<lp>);\r
+ * open() == Window.OK \r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public class TreeDialog extends Dialog {\r
+\r
+ String title; \r
+ String message;\r
+ Label messageLabel; \r
+ IInputValidator validator;\r
+ Object initialSelection;\r
+ Object selected[];\r
+ TreeViewer viewer;\r
+ ILabelProvider labelProvider;\r
+ IStructuredContentProvider contentProvider;\r
+ Object input;\r
+ boolean expandTree = false;\r
+ boolean multipleSelection = false;\r
+ boolean useDoubleClick = true;\r
+ \r
+ ViewerSorter sorter;\r
+ \r
+ public TreeDialog(Shell parentShell, String dialogTitle,\r
+ String dialogMessage) {\r
+ super(parentShell);\r
+ message = dialogMessage;\r
+ title = dialogTitle;\r
+ }\r
+ \r
+ /**\r
+ * Set the title\r
+ */\r
+ protected void configureShell(Shell shell) {\r
+ super.configureShell(shell);\r
+ if (title != null) {\r
+ shell.setText(title);\r
+ }\r
+ }\r
+ \r
+ /**\r
+ * Set selection value\r
+ */\r
+ protected void buttonPressed(int buttonId) {\r
+ selected = null;\r
+ if (buttonId == IDialogConstants.OK_ID) {\r
+ ISelection sel = viewer.getSelection();\r
+ if (sel instanceof IStructuredSelection)\r
+ {\r
+ IStructuredSelection ss = (IStructuredSelection) sel;\r
+ if (!ss.isEmpty())\r
+ selected = ss.toArray();\r
+ }\r
+ }\r
+ super.buttonPressed(buttonId);\r
+ } \r
+ \r
+ protected Control createDialogArea(Composite parent) { \r
+ // create composite\r
+ Composite composite = (Composite) super.createDialogArea(parent);\r
+ // create message\r
+ if (message != null) {\r
+ messageLabel = new Label(composite, SWT.WRAP);\r
+ messageLabel.setText(message);\r
+ GridData data = new GridData(GridData.GRAB_HORIZONTAL\r
+ | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL\r
+ | GridData.VERTICAL_ALIGN_CENTER);\r
+ data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);\r
+ messageLabel.setLayoutData(data);\r
+ messageLabel.setFont(parent.getFont());\r
+ }\r
+ \r
+ assert(input!=null);\r
+ assert(labelProvider!=null);\r
+ assert(contentProvider!=null);\r
+ int flags = SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION;\r
+ if (multipleSelection) flags |= SWT.MULTI;\r
+ viewer = new TreeViewer(parent, flags);\r
+ viewer.setLabelProvider(labelProvider);\r
+ viewer.setContentProvider(contentProvider);\r
+ viewer.setSorter(sorter);\r
+ viewer.setInput(input);\r
+ if (useDoubleClick)\r
+ viewer.addDoubleClickListener(new IDoubleClickListener() {\r
+ public void doubleClick(DoubleClickEvent event) {\r
+ //TreeDialog.this.okPressed();\r
+ buttonPressed(IDialogConstants.OK_ID);\r
+ }});\r
+ \r
+ \r
+ if (expandTree)\r
+ viewer.expandAll(); \r
+ \r
+ GridData gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL | \r
+ GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_FILL );\r
+ gd.heightHint = 250;\r
+ gd.widthHint = 200;\r
+ viewer.getTree().setLayoutData(gd);\r
+ if (initialSelection!=null) \r
+ viewer.setSelection(new StructuredSelection(initialSelection)); \r
+ applyDialogFont(composite);\r
+ return composite;\r
+ }\r
+\r
+ public ILabelProvider getLabelProvider() {\r
+ return labelProvider;\r
+ }\r
+\r
+ /**\r
+ * Sets label provider\r
+ * \r
+ * Must be called before dialog is opened.\r
+ * \r
+ * @param labelProvider\r
+ */\r
+ public void setLabelProvider(ILabelProvider labelProvider) {\r
+ if (viewer != null)\r
+ throw new IllegalStateException("Dialog is already initialized");\r
+ this.labelProvider = labelProvider;\r
+ }\r
+\r
+ public IStructuredContentProvider getContentProvider() {\r
+ return contentProvider;\r
+ }\r
+\r
+ /**\r
+ * Sets content provider\r
+ * \r
+ * Must be called before dialog is opened.\r
+ * \r
+ * @param contentProvider\r
+ */\r
+ public void setContentProvider(IStructuredContentProvider contentProvider) {\r
+ if (viewer != null)\r
+ throw new IllegalStateException("Dialog is already initialized");\r
+ this.contentProvider = contentProvider;\r
+ }\r
+\r
+ public Object[] getSelected() {\r
+ return selected;\r
+ }\r
+\r
+ public Object getSingleSelected() {\r
+ if (selected==null || selected.length==0) return null;\r
+ return selected[0];\r
+ }\r
+\r
+ public Object getInitialSelection() {\r
+ return initialSelection;\r
+ }\r
+\r
+ /**\r
+ * Sets initial selection.\r
+ * \r
+ * Must be called before dialog is opened.\r
+ * \r
+ * @param initialSelection\r
+ */\r
+ public void setInitialSelection(Object initialSelection) {\r
+ if (viewer != null)\r
+ throw new IllegalStateException("Dialog is already initialized");\r
+ this.initialSelection = initialSelection;\r
+ }\r
+\r
+ public Object getInput() {\r
+ return input;\r
+ }\r
+\r
+ /**\r
+ * Sets input object for the tree viewer.\r
+ * \r
+ * Must be called before dialog is opened.\r
+ * \r
+ * @param inputElement\r
+ */\r
+ public void setInput(Object inputElement) {\r
+ if (viewer != null)\r
+ throw new IllegalStateException("Dialog is already initialized");\r
+ this.input = inputElement;\r
+ }\r
+\r
+ public String getMessage() {\r
+ return message;\r
+ }\r
+\r
+ /**\r
+ * Sets optional message that is shown top of the tree.\r
+ * \r
+ * Must be called before dialog is opened.\r
+ * \r
+ * @param message\r
+ */\r
+ public void setMessage(String message) {\r
+ if (viewer != null)\r
+ throw new IllegalStateException("Dialog is already initialized");\r
+ this.message = message;\r
+ }\r
+\r
+ public boolean isMultipleSelection() {\r
+ return multipleSelection;\r
+ }\r
+\r
+ /**\r
+ * Sets multi-selection support for the tree viewer. Single-selection is the default. \r
+ * \r
+ * Must be called before dialog is opened.\r
+ * \r
+ * @param multipleSelection\r
+ */\r
+ public void setMultipleSelection(boolean multipleSelection) {\r
+ if (viewer != null)\r
+ throw new IllegalStateException("Dialog is already initialized");\r
+ this.multipleSelection = multipleSelection;\r
+ }\r
+ \r
+ /**\r
+ * Use Double-click as way to close the dialog. This enabled by default.\r
+ * \r
+ * Must be called before dialog is opened.\r
+ * \r
+ * @param useDoubleClick\r
+ */\r
+ public void setUseDoubleClick(boolean useDoubleClick) {\r
+ if (viewer != null)\r
+ throw new IllegalStateException("Dialog is already initialized");\r
+ this.useDoubleClick = useDoubleClick;\r
+ }\r
+ \r
+ public boolean isUseDoubleClick() {\r
+ return useDoubleClick;\r
+ }\r
+\r
+ public boolean isExpandTree() {\r
+ return expandTree;\r
+ }\r
+\r
+ /**\r
+ * Set the tree viewer to fully expand.\r
+ * \r
+ * Must be called before dialog is opened.\r
+ * \r
+ * @param expandTree\r
+ */\r
+ public void setExpandTree(boolean expandTree) {\r
+ if (viewer != null)\r
+ throw new IllegalStateException("Dialog is already initialized");\r
+ this.expandTree = expandTree;\r
+ }\r
+ \r
+ /**\r
+ * Sets sorter for the tree viewer.\r
+ * \r
+ * Must be called before dialog is opened.\r
+ * \r
+ * @param sorter\r
+ */\r
+ public void setSorter(ViewerSorter sorter) {\r
+ if (viewer != null)\r
+ throw new IllegalStateException("Dialog is already initialized");\r
+ this.sorter =sorter;\r
+ }\r
+ \r
+ public TreeViewer getViewer() {\r
+ return viewer;\r
+ }\r
+ \r
+}\r