]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/typicals/NewTypicalDiagramInstance.java
Red background color & tooltip for invalid derived property expression
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / typicals / NewTypicalDiagramInstance.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.typicals;
13
14 import java.util.Collection;
15 import java.util.concurrent.atomic.AtomicReference;
16 import java.util.function.Consumer;
17
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;
42
43 /**
44  * @author Tuukka Lehtonen
45  */
46 public class NewTypicalDiagramInstance implements ActionFactory {
47
48     @Override
49     public Runnable create(Object target_) {
50         Resource target = (Resource) target_;
51         return () -> {
52             Job job = new DatabaseJob("Instantiate Typical") {
53                 @Override
54                 protected IStatus run(IProgressMonitor monitor) {
55                     monitor.beginTask("Instantiate Typical...", IProgressMonitor.UNKNOWN);
56
57                     Session session = Simantics.getSession();
58                     session.markUndoPoint();
59                     try {
60                         AtomicReference<Resource> model = new AtomicReference<Resource>();
61                         Function2<Resource, Resource, Resource> instantiator = session.syncRequest(new UniqueRead<Function2<Resource, Resource, Resource>>() {
62                             @Override
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.");
67
68                                 ModelingResources MOD = ModelingResources.getInstance(graph);
69                                 Function2<Resource, Resource, Resource> instantiator = graph.getRelatedValue2(model.get(), MOD.StructuralModel_typicalInstantiationFunction);
70                                 return instantiator;
71                             }
72                         });
73
74                         instantiator.apply(model.get(), target);
75
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);
81                     } finally {
82                         monitor.done();
83                     }
84                 }
85             };
86             //job.setUser(true);
87             job.schedule();
88         };
89     }
90
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);
94             if (callback != null)
95                 callback.accept(result);
96         });
97     }
98
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;
103
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)
112             return null;
113
114         return (T) dialog.getFirstResult();
115     }
116
117 }