1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
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 *******************************************************************************/
12 package org.simantics.modeling.ui.typicals;
14 import java.util.Collection;
15 import java.util.concurrent.atomic.AtomicReference;
16 import java.util.function.Consumer;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.core.runtime.jobs.Job;
22 import org.eclipse.jface.viewers.LabelProvider;
23 import org.eclipse.jface.window.Window;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.ui.IWorkbenchWindow;
26 import org.eclipse.ui.PlatformUI;
27 import org.simantics.DatabaseJob;
28 import org.simantics.Simantics;
29 import org.simantics.db.ReadGraph;
30 import org.simantics.db.Resource;
31 import org.simantics.db.Session;
32 import org.simantics.db.common.request.UniqueRead;
33 import org.simantics.db.exception.CancelTransactionException;
34 import org.simantics.db.exception.DatabaseException;
35 import org.simantics.db.layer0.adapter.ActionFactory;
36 import org.simantics.db.layer0.request.PossibleModel;
37 import org.simantics.modeling.ModelingResources;
38 import org.simantics.modeling.ui.Activator;
39 import org.simantics.scl.runtime.function.Function2;
40 import org.simantics.ui.workbench.dialogs.ElementListSelectionDialog;
41 import org.simantics.utils.ui.SWTUtils;
44 * @author Tuukka Lehtonen
46 public class NewTypicalDiagramInstance implements ActionFactory {
49 public Runnable create(Object target_) {
50 Resource target = (Resource) target_;
52 Job job = new DatabaseJob("Instantiate Typical") {
54 protected IStatus run(IProgressMonitor monitor) {
55 monitor.beginTask("Instantiate Typical...", IProgressMonitor.UNKNOWN);
57 Session session = Simantics.getSession();
58 session.markUndoPoint();
60 AtomicReference<Resource> model = new AtomicReference<Resource>();
61 Function2<Resource, Resource, Resource> instantiator = session.syncRequest(new UniqueRead<Function2<Resource, Resource, Resource>>() {
63 public Function2<Resource, Resource, Resource> perform(ReadGraph graph) throws DatabaseException {
64 model.set( graph.sync(new PossibleModel(target)) );
65 if (model.get() == null)
66 throw new CancelTransactionException("Cannot find a model from the input selection. Typical diagram instantiation not possible.");
68 ModelingResources MOD = ModelingResources.getInstance(graph);
69 Function2<Resource, Resource, Resource> instantiator = graph.getRelatedValue2(model.get(), MOD.StructuralModel_typicalInstantiationFunction);
74 instantiator.apply(model.get(), target);
76 return Status.OK_STATUS;
77 } catch (CancelTransactionException e) {
78 return new Status(IStatus.INFO , Activator.PLUGIN_ID, e.getMessage());
79 } catch (DatabaseException e) {
80 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e);
91 public static <T> void asyncQueryFromList(final Collection<T> elements, final Consumer<T> callback) {
92 SWTUtils.asyncExec(PlatformUI.getWorkbench().getDisplay(), () -> {
93 T result = queryFromList(elements);
95 callback.accept(result);
99 @SuppressWarnings("unchecked")
100 public static <T> T queryFromList(final Collection<T> elements) {
101 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
102 Shell shell = window != null ? window.getShell() : null;
104 ElementListSelectionDialog dialog = new ElementListSelectionDialog(shell, new LabelProvider());
105 dialog.setBlockOnOpen(true);
106 dialog.setTitle("Select Template Typical Diagram");
107 dialog.setMessage("Select typical diagram to copy (? = any character, * = any string):");
108 dialog.setMultipleSelection(false);
109 dialog.setMatchEmptyString(true);
110 dialog.setElements(elements);
111 if (dialog.open() != Window.OK)
114 return (T) dialog.getFirstResult();