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