]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/typicals/NewTypicalDiagramInstance.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / typicals / NewTypicalDiagramInstance.java
index 25fd46e69dd4fb914a18c78b989dcb34feacac10..514131b3814b6b46987f0fdb1726bec46d7a444f 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2012 Association for Decentralized Information Management in\r
- * 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.ui.typicals;\r
-\r
-import java.util.Collection;\r
-import java.util.concurrent.atomic.AtomicReference;\r
-import java.util.function.Consumer;\r
-\r
-import org.eclipse.core.runtime.IProgressMonitor;\r
-import org.eclipse.core.runtime.IStatus;\r
-import org.eclipse.core.runtime.Status;\r
-import org.eclipse.core.runtime.jobs.Job;\r
-import org.eclipse.jface.viewers.LabelProvider;\r
-import org.eclipse.jface.window.Window;\r
-import org.eclipse.swt.widgets.Shell;\r
-import org.eclipse.ui.IWorkbenchWindow;\r
-import org.eclipse.ui.PlatformUI;\r
-import org.simantics.DatabaseJob;\r
-import org.simantics.Simantics;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.Session;\r
-import org.simantics.db.common.request.UniqueRead;\r
-import org.simantics.db.exception.CancelTransactionException;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.adapter.ActionFactory;\r
-import org.simantics.db.layer0.request.PossibleModel;\r
-import org.simantics.modeling.ModelingResources;\r
-import org.simantics.modeling.ui.Activator;\r
-import org.simantics.scl.runtime.function.Function2;\r
-import org.simantics.ui.workbench.dialogs.ElementListSelectionDialog;\r
-import org.simantics.utils.ui.SWTUtils;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
-public class NewTypicalDiagramInstance implements ActionFactory {\r
-\r
-    @Override\r
-    public Runnable create(Object target_) {\r
-        Resource target = (Resource) target_;\r
-        return () -> {\r
-            Job job = new DatabaseJob("Instantiate Typical") {\r
-                @Override\r
-                protected IStatus run(IProgressMonitor monitor) {\r
-                    monitor.beginTask("Instantiate Typical...", IProgressMonitor.UNKNOWN);\r
-\r
-                    Session session = Simantics.getSession();\r
-                    session.markUndoPoint();\r
-                    try {\r
-                        AtomicReference<Resource> model = new AtomicReference<Resource>();\r
-                        Function2<Resource, Resource, Resource> instantiator = session.syncRequest(new UniqueRead<Function2<Resource, Resource, Resource>>() {\r
-                            @Override\r
-                            public Function2<Resource, Resource, Resource> perform(ReadGraph graph) throws DatabaseException {\r
-                                model.set( graph.sync(new PossibleModel(target)) );\r
-                                if (model.get() == null)\r
-                                    throw new CancelTransactionException("Cannot find a model from the input selection. Typical diagram instantiation not possible.");\r
-\r
-                                ModelingResources MOD = ModelingResources.getInstance(graph);\r
-                                Function2<Resource, Resource, Resource> instantiator = graph.getRelatedValue2(model.get(), MOD.StructuralModel_typicalInstantiationFunction);\r
-                                return instantiator;\r
-                            }\r
-                        });\r
-\r
-                        instantiator.apply(model.get(), target);\r
-\r
-                        return Status.OK_STATUS;\r
-                    } catch (CancelTransactionException e) {\r
-                        return new Status(IStatus.INFO , Activator.PLUGIN_ID, e.getMessage());\r
-                    } catch (DatabaseException e) {\r
-                        return new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e);\r
-                    } finally {\r
-                        monitor.done();\r
-                    }\r
-                }\r
-            };\r
-            //job.setUser(true);\r
-            job.schedule();\r
-        };\r
-    }\r
-\r
-    public static <T> void asyncQueryFromList(final Collection<T> elements, final Consumer<T> callback) {\r
-        SWTUtils.asyncExec(PlatformUI.getWorkbench().getDisplay(), () -> {\r
-            T result = queryFromList(elements);\r
-            if (callback != null)\r
-                callback.accept(result);\r
-        });\r
-    }\r
-\r
-    @SuppressWarnings("unchecked")\r
-       public static <T> T queryFromList(final Collection<T> elements) {\r
-        IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();\r
-        Shell shell = window != null ? window.getShell() : null;\r
-\r
-        ElementListSelectionDialog dialog = new ElementListSelectionDialog(shell, new LabelProvider());\r
-        dialog.setBlockOnOpen(true);\r
-        dialog.setTitle("Select Template Typical Diagram");\r
-        dialog.setMessage("Select typical diagram to copy (? = any character, * = any string):");\r
-        dialog.setMultipleSelection(false);\r
-        dialog.setMatchEmptyString(true);\r
-        dialog.setElements(elements);\r
-        if (dialog.open() != Window.OK)\r
-            return null;\r
-\r
-        return (T) dialog.getFirstResult();\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2012 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
+ *******************************************************************************/
+package org.simantics.modeling.ui.typicals;
+
+import java.util.Collection;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.Consumer;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.jface.viewers.LabelProvider;
+import org.eclipse.jface.window.Window;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PlatformUI;
+import org.simantics.DatabaseJob;
+import org.simantics.Simantics;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.exception.CancelTransactionException;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.adapter.ActionFactory;
+import org.simantics.db.layer0.request.PossibleModel;
+import org.simantics.modeling.ModelingResources;
+import org.simantics.modeling.ui.Activator;
+import org.simantics.scl.runtime.function.Function2;
+import org.simantics.ui.workbench.dialogs.ElementListSelectionDialog;
+import org.simantics.utils.ui.SWTUtils;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class NewTypicalDiagramInstance implements ActionFactory {
+
+    @Override
+    public Runnable create(Object target_) {
+        Resource target = (Resource) target_;
+        return () -> {
+            Job job = new DatabaseJob("Instantiate Typical") {
+                @Override
+                protected IStatus run(IProgressMonitor monitor) {
+                    monitor.beginTask("Instantiate Typical...", IProgressMonitor.UNKNOWN);
+
+                    Session session = Simantics.getSession();
+                    session.markUndoPoint();
+                    try {
+                        AtomicReference<Resource> model = new AtomicReference<Resource>();
+                        Function2<Resource, Resource, Resource> instantiator = session.syncRequest(new UniqueRead<Function2<Resource, Resource, Resource>>() {
+                            @Override
+                            public Function2<Resource, Resource, Resource> perform(ReadGraph graph) throws DatabaseException {
+                                model.set( graph.sync(new PossibleModel(target)) );
+                                if (model.get() == null)
+                                    throw new CancelTransactionException("Cannot find a model from the input selection. Typical diagram instantiation not possible.");
+
+                                ModelingResources MOD = ModelingResources.getInstance(graph);
+                                Function2<Resource, Resource, Resource> instantiator = graph.getRelatedValue2(model.get(), MOD.StructuralModel_typicalInstantiationFunction);
+                                return instantiator;
+                            }
+                        });
+
+                        instantiator.apply(model.get(), target);
+
+                        return Status.OK_STATUS;
+                    } catch (CancelTransactionException e) {
+                        return new Status(IStatus.INFO , Activator.PLUGIN_ID, e.getMessage());
+                    } catch (DatabaseException e) {
+                        return new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e);
+                    } finally {
+                        monitor.done();
+                    }
+                }
+            };
+            //job.setUser(true);
+            job.schedule();
+        };
+    }
+
+    public static <T> void asyncQueryFromList(final Collection<T> elements, final Consumer<T> callback) {
+        SWTUtils.asyncExec(PlatformUI.getWorkbench().getDisplay(), () -> {
+            T result = queryFromList(elements);
+            if (callback != null)
+                callback.accept(result);
+        });
+    }
+
+    @SuppressWarnings("unchecked")
+       public static <T> T queryFromList(final Collection<T> elements) {
+        IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
+        Shell shell = window != null ? window.getShell() : null;
+
+        ElementListSelectionDialog dialog = new ElementListSelectionDialog(shell, new LabelProvider());
+        dialog.setBlockOnOpen(true);
+        dialog.setTitle("Select Template Typical Diagram");
+        dialog.setMessage("Select typical diagram to copy (? = any character, * = any string):");
+        dialog.setMultipleSelection(false);
+        dialog.setMatchEmptyString(true);
+        dialog.setElements(elements);
+        if (dialog.open() != Window.OK)
+            return null;
+
+        return (T) dialog.getFirstResult();
+    }
+
+}