]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/actions/NavigationTargetChooserDialog.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / actions / NavigationTargetChooserDialog.java
diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/actions/NavigationTargetChooserDialog.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/actions/NavigationTargetChooserDialog.java
new file mode 100644 (file)
index 0000000..2253c4b
--- /dev/null
@@ -0,0 +1,192 @@
+/*******************************************************************************\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
+package org.simantics.modeling.actions;\r
+\r
+import org.eclipse.jface.dialogs.Dialog;\r
+import org.eclipse.jface.dialogs.IDialogConstants;\r
+import org.eclipse.jface.dialogs.IDialogSettings;\r
+import org.eclipse.jface.viewers.ArrayContentProvider;\r
+import org.eclipse.jface.viewers.DoubleClickEvent;\r
+import org.eclipse.jface.viewers.IDoubleClickListener;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.jface.viewers.ISelectionChangedListener;\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.jface.viewers.LabelProvider;\r
+import org.eclipse.jface.viewers.SelectionChangedEvent;\r
+import org.eclipse.jface.viewers.StructuredSelection;\r
+import org.eclipse.jface.viewers.TableViewer;\r
+import org.eclipse.jface.viewers.Viewer;\r
+import org.eclipse.jface.viewers.ViewerSorter;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.graphics.Image;\r
+import org.eclipse.swt.graphics.Point;\r
+import org.eclipse.swt.layout.GridData;\r
+import org.eclipse.swt.widgets.Button;\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
+import org.simantics.db.common.NamedResource;\r
+import org.simantics.ui.internal.Activator;\r
+import org.simantics.utils.strings.AlphanumComparator;\r
+\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class NavigationTargetChooserDialog extends Dialog {\r
+\r
+    private static final String DIALOG = "NavigationTargetChooserDialog"; //$NON-NLS-1$\r
+\r
+    private final NamedResource[] options;\r
+\r
+    private final String          title;\r
+\r
+    private final String          description;\r
+\r
+    private NamedResource         selected;\r
+\r
+    private TableViewer           viewer;\r
+\r
+    private IDialogSettings       dialogBoundsSettings;\r
+\r
+    public NavigationTargetChooserDialog(Shell parent, NamedResource[] options, String title, String description) {\r
+        super(parent);\r
+        this.options = options;\r
+        this.title = title;\r
+        this.description = description;\r
+\r
+        IDialogSettings settings = Activator.getDefault().getDialogSettings();\r
+        dialogBoundsSettings = settings.getSection(DIALOG);\r
+        if (dialogBoundsSettings == null)\r
+            dialogBoundsSettings = settings.addNewSection(DIALOG);\r
+    }\r
+\r
+    @Override\r
+    protected IDialogSettings getDialogBoundsSettings() {\r
+        return dialogBoundsSettings;\r
+    }\r
+\r
+    @Override\r
+    protected void configureShell(Shell newShell) {\r
+        if (title != null) {\r
+            newShell.setText(title);\r
+        } else {\r
+            newShell.setText("Choose Navigation Target");\r
+        }\r
+        super.configureShell(newShell);\r
+    }\r
+\r
+    @Override\r
+    protected int getShellStyle() {\r
+        return super.getShellStyle() | SWT.RESIZE;\r
+    }\r
+\r
+    @Override\r
+    protected Point getInitialSize() {\r
+        Point defaultSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT, true);\r
+        Point result = super.getInitialSize();\r
+        if (defaultSize.equals(result))\r
+            return new Point(400, 500);\r
+        return result;\r
+    }\r
+\r
+    @Override\r
+    protected Control createDialogArea(Composite parent) {\r
+        Composite composite = (Composite) super.createDialogArea(parent);\r
+\r
+        if (description != null) {\r
+            Label label = new Label(composite, 0);\r
+            label.setText(description);\r
+            label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));\r
+        }\r
+\r
+        viewer = new TableViewer(composite, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER | SWT.SINGLE);\r
+        viewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));\r
+        viewer.setContentProvider(new ArrayContentProvider());\r
+        viewer.setLabelProvider(new TargetLabelProvider());\r
+        viewer.setSorter(sorter);\r
+        viewer.setInput(options);\r
+\r
+        viewer.addDoubleClickListener(new IDoubleClickListener() {\r
+            @Override\r
+            public void doubleClick(DoubleClickEvent event) {\r
+                okPressed();\r
+            }\r
+        });\r
+\r
+        viewer.addSelectionChangedListener(new ISelectionChangedListener() {\r
+            @Override\r
+            public void selectionChanged(SelectionChangedEvent event) {\r
+                NavigationTargetChooserDialog.this.selectionChanged(event.getSelection());\r
+            }\r
+        });\r
+\r
+        if (options.length > 0) {\r
+            viewer.setSelection(new StructuredSelection(options[0]), true);\r
+        }\r
+\r
+        applyDialogFont(composite);\r
+        return composite;\r
+    }\r
+\r
+    private void selectionChanged(ISelection s) {\r
+        Button ok = getButton(IDialogConstants.OK_ID);\r
+        IStructuredSelection iss = (IStructuredSelection) s;\r
+        if (iss == null || iss.isEmpty()) {\r
+            if (ok != null)\r
+                ok.setEnabled(false);\r
+            return;\r
+        }\r
+\r
+        if (ok != null)\r
+            ok.setEnabled(true);\r
+        return;\r
+    }\r
+\r
+    @Override\r
+    protected void okPressed() {\r
+        selected = (NamedResource) ((IStructuredSelection) viewer.getSelection()).getFirstElement();\r
+        super.okPressed();\r
+    }\r
+\r
+    public NamedResource getSelection() {\r
+        return selected;\r
+    }\r
+\r
+    private final ViewerSorter sorter = new ViewerSorter() {\r
+        @Override\r
+        public int category(Object element) {\r
+            return 0;\r
+        }\r
+        @Override\r
+        public int compare(Viewer viewer, Object e1, Object e2) {\r
+            NamedResource a1 = (NamedResource) e1;\r
+            NamedResource a2 = (NamedResource) e2;\r
+            return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(a1.getName(), a2.getName());\r
+        }\r
+    };\r
+\r
+    class TargetLabelProvider extends LabelProvider {\r
+        @Override\r
+        public Image getImage(Object element) {\r
+            return null;\r
+        }\r
+\r
+        @Override\r
+        public String getText(Object element) {\r
+            NamedResource a = (NamedResource) element;\r
+            return a.getName();\r
+        }\r
+    }\r
+\r
+}\r