1 /*******************************************************************************
\r
2 * Copyright (c) 2012 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.modeling.ui.typicals;
\r
14 import java.util.Collection;
\r
15 import java.util.concurrent.atomic.AtomicReference;
\r
16 import java.util.function.Consumer;
\r
18 import org.eclipse.core.runtime.IProgressMonitor;
\r
19 import org.eclipse.core.runtime.IStatus;
\r
20 import org.eclipse.core.runtime.Status;
\r
21 import org.eclipse.core.runtime.jobs.Job;
\r
22 import org.eclipse.jface.viewers.LabelProvider;
\r
23 import org.eclipse.jface.window.Window;
\r
24 import org.eclipse.swt.widgets.Shell;
\r
25 import org.eclipse.ui.IWorkbenchWindow;
\r
26 import org.eclipse.ui.PlatformUI;
\r
27 import org.simantics.DatabaseJob;
\r
28 import org.simantics.Simantics;
\r
29 import org.simantics.db.ReadGraph;
\r
30 import org.simantics.db.Resource;
\r
31 import org.simantics.db.Session;
\r
32 import org.simantics.db.common.request.UniqueRead;
\r
33 import org.simantics.db.exception.CancelTransactionException;
\r
34 import org.simantics.db.exception.DatabaseException;
\r
35 import org.simantics.db.layer0.adapter.ActionFactory;
\r
36 import org.simantics.db.layer0.request.PossibleModel;
\r
37 import org.simantics.modeling.ModelingResources;
\r
38 import org.simantics.modeling.ui.Activator;
\r
39 import org.simantics.scl.runtime.function.Function2;
\r
40 import org.simantics.ui.workbench.dialogs.ElementListSelectionDialog;
\r
41 import org.simantics.utils.ui.SWTUtils;
\r
44 * @author Tuukka Lehtonen
\r
46 public class NewTypicalDiagramInstance implements ActionFactory {
\r
49 public Runnable create(Object target_) {
\r
50 final Resource target = (Resource) target_;
\r
51 return new Runnable() {
\r
54 Job job = new DatabaseJob("Instantiate Typical") {
\r
56 protected IStatus run(IProgressMonitor monitor) {
\r
57 monitor.beginTask("Instantiate Typical...", IProgressMonitor.UNKNOWN);
\r
59 final Session session = Simantics.getSession();
\r
60 session.markUndoPoint();
\r
62 final AtomicReference<Resource> model = new AtomicReference<Resource>();
\r
63 Function2<Resource, Resource, Resource> instantiator = session.syncRequest(new UniqueRead<Function2<Resource, Resource, Resource>>() {
\r
65 public Function2<Resource, Resource, Resource> perform(ReadGraph graph) throws DatabaseException {
\r
66 model.set( graph.sync(new PossibleModel(target)) );
\r
67 if (model.get() == null)
\r
68 throw new CancelTransactionException("Cannot find a model from the input selection. Typical diagram instantiation not possible.");
\r
70 ModelingResources MOD = ModelingResources.getInstance(graph);
\r
71 Function2<Resource, Resource, Resource> instantiator = graph.getRelatedValue2(model.get(), MOD.StructuralModel_typicalInstantiationFunction);
\r
72 return instantiator;
\r
76 instantiator.apply(model.get(), target);
\r
78 return Status.OK_STATUS;
\r
79 } catch (CancelTransactionException e) {
\r
80 return new Status(IStatus.INFO , Activator.PLUGIN_ID, e.getMessage());
\r
81 } catch (DatabaseException e) {
\r
82 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e);
\r
88 //job.setUser(true);
\r
94 public static <T> void asyncQueryFromList(final Collection<T> elements, final Consumer<T> callback) {
\r
95 SWTUtils.asyncExec(PlatformUI.getWorkbench().getDisplay(), () -> {
\r
96 T result = queryFromList(elements);
\r
97 if (callback != null)
\r
98 callback.accept(result);
\r
102 @SuppressWarnings("unchecked")
\r
103 public static <T> T queryFromList(final Collection<T> elements) {
\r
104 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
\r
105 Shell shell = window != null ? window.getShell() : null;
\r
107 ElementListSelectionDialog dialog = new ElementListSelectionDialog(shell, new LabelProvider());
\r
108 dialog.setBlockOnOpen(true);
\r
109 dialog.setTitle("Select Template Typical Diagram");
\r
110 dialog.setMessage("Select typical diagram to copy (? = any character, * = any string):");
\r
111 dialog.setMultipleSelection(false);
\r
112 dialog.setMatchEmptyString(true);
\r
113 dialog.setElements(elements);
\r
114 if (dialog.open() != Window.OK)
\r
117 return (T) dialog.getFirstResult();
\r