1 package org.simantics.district.network.ui.contributions;
3 import java.util.Collections;
6 import javax.inject.Named;
8 import org.eclipse.e4.core.di.annotations.CanExecute;
9 import org.eclipse.e4.core.di.annotations.Execute;
10 import org.eclipse.e4.ui.di.AboutToHide;
11 import org.eclipse.e4.ui.di.AboutToShow;
12 import org.eclipse.e4.ui.model.application.ui.menu.MDirectMenuItem;
13 import org.eclipse.e4.ui.model.application.ui.menu.MMenu;
14 import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement;
15 import org.eclipse.e4.ui.model.application.ui.menu.MMenuFactory;
16 import org.eclipse.e4.ui.services.IServiceConstants;
17 import org.eclipse.swt.widgets.Display;
18 import org.simantics.Simantics;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
23 import org.simantics.db.common.request.ResourceRead;
24 import org.simantics.db.common.request.WriteRequest;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.exception.NoSingleResultException;
27 import org.simantics.db.layer0.SelectionHints;
28 import org.simantics.db.layer0.util.Layer0Utils;
29 import org.simantics.db.layer0.variable.Variable;
30 import org.simantics.db.layer0.variable.Variables;
31 import org.simantics.district.network.ontology.DistrictNetworkResource;
32 import org.simantics.layer0.Layer0;
33 import org.simantics.modeling.ModelingResources;
34 import org.simantics.modeling.ui.scl.SCLScripts;
35 import org.simantics.scl.compiler.top.ValueNotFound;
36 import org.simantics.scl.osgi.SCLOsgi;
37 import org.simantics.scl.runtime.SCLContext;
38 import org.simantics.scl.runtime.function.Function;
39 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
40 import org.simantics.scl.runtime.tuple.Tuple2;
41 import org.simantics.utils.ui.ISelectionUtils;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 public class NetworkElementActionMenuContribution {
47 static private Logger LOGGER = LoggerFactory.getLogger(NetworkElementActionMenuContribution.class);
50 public void aboutToShow(@Named(IServiceConstants.ACTIVE_SELECTION) Object selection, Display display, List<MMenuElement> items) {
51 final List<Resource> vertices = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
52 if (vertices.size() != 1)
55 Resource vertex = vertices.get(0);
59 actions = Simantics.getSession().syncRequest(new ActionItemRequest(vertex), TransientCacheListener.instance());
60 } catch (DatabaseException e) {
61 LOGGER.error("Error fetching action items for " + vertex, e);
65 items.add(MMenuFactory.INSTANCE.createMenuSeparator());
66 MMenu subMenu = MMenuFactory.INSTANCE.createMenu();
67 List<MMenuElement> children = subMenu.getChildren();
68 subMenu.setLabel("Component Actions");
71 for (Tuple2 action : actions) {
72 String label = (String) action.c0;
73 @SuppressWarnings("rawtypes")
74 Function function = (Function) action.c1;
76 Object handler = new Object() {
78 public boolean canExecute() {
80 return Simantics.getSession().syncRequest(new ResourceRead<Boolean>(vertex) {
82 public Boolean perform(ReadGraph graph) throws DatabaseException {
83 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
84 return graph.isInstanceOf(vertex, DN.Element) && graph.hasStatement(vertex, DN.MappedComponent);
86 }, TransientCacheListener.instance());
87 } catch (DatabaseException e) {
93 public void execute() {
94 // Handler that only opens the SCL script console on demand
95 SCLReportingHandler handler = new SCLReportingHandler() {
97 private SCLReportingHandler handler;
99 // Get a handler for the SCL script console view
100 private SCLReportingHandler getHandler() {
101 if (handler == null) {
102 handler = SCLScripts.getOrCreateConsoleCommandSession().second;
108 public void printError(String error) {
109 display.asyncExec(() -> {
110 getHandler().printError(error);
115 public void printCommand(String command) {
116 display.asyncExec(() -> {
117 getHandler().printCommand(command);
122 public void print(String text) {
123 display.asyncExec(() -> {
124 getHandler().print(text);
129 public void didWork(double amount) {
130 display.asyncExec(() -> {
131 getHandler().didWork(amount);
136 Simantics.getSession().asyncRequest(new WriteRequest() {
137 @SuppressWarnings("unchecked")
139 public void perform(WriteGraph graph) throws DatabaseException {
140 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
141 Resource component = graph.getPossibleObject(vertex, DN.MappedComponent);
142 if (component == null)
145 Variable v = Variables.getVariable(graph, graph.getSingleObject(component, ModelingResources.getInstance(graph).ElementToComponent));
147 graph.markUndoPoint();
148 Layer0Utils.addCommentMetadata(graph, label + " for " + v.getName(graph));
150 SCLContext context = SCLContext.getCurrent();
151 Object oldHandler = context.put(SCLReportingHandler.REPORTING_HANDLER, handler);
153 Simantics.applySCLWrite(graph, function, v);
156 context.put(SCLReportingHandler.REPORTING_HANDLER, oldHandler);
159 }, (DatabaseException e) -> {
160 if (e != null) LOGGER.error("Running command " + label + " for " + vertex + " failed", e);
165 MDirectMenuItem dynamicItem = MMenuFactory.INSTANCE.createDirectMenuItem();
166 dynamicItem.setLabel(label);
167 dynamicItem.setObject(handler);
168 children.add(dynamicItem);
173 public void aboutToHide() {
176 private static final class ActionItemRequest extends ResourceRead<List<Tuple2>> {
177 private ActionItemRequest(Resource resource) {
181 @SuppressWarnings("unchecked")
183 public List<Tuple2> perform(ReadGraph graph) throws DatabaseException {
184 Layer0 L0 = Layer0.getInstance(graph);
185 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
187 if (!graph.isInstanceOf(resource, DN.Element))
188 return Collections.emptyList();
190 Resource component = graph.getPossibleObject(resource, DN.MappedComponent);
191 if (component == null)
192 return Collections.emptyList();
194 Resource mapping = graph.getPossibleObject(resource, DN.HasMapping);
196 return Collections.emptyList();
198 String name = graph.getPossibleRelatedValue(mapping, DN.Mapping_ComponentType);
200 return Collections.emptyList();
202 Resource root = graph.getSingleObject(graph.getSingleObject(mapping, L0.PartOf), L0.PartOf);
203 Resource uc = Layer0Utils.getPossibleChild(graph, root, name);
205 return Collections.emptyList();
207 Resource actionsModule = Layer0Utils.getPossibleChild(graph, uc, "Actions");
208 if (actionsModule == null)
209 return Collections.emptyList();
211 String uri = graph.getURI(actionsModule);
212 SCLContext sclContext = SCLContext.getCurrent();
213 Object oldGraph = sclContext.put("graph", graph);
215 @SuppressWarnings("rawtypes")
216 Function actionsFun = (Function) SCLOsgi.MODULE_REPOSITORY.getValue(uri, "actions");
217 Variable variable = Variables.getPossibleVariable(graph, graph.getSingleObject(component, ModelingResources.getInstance(graph).ElementToComponent));
218 return (List<Tuple2>) actionsFun.apply(variable);
220 catch (ValueNotFound e) {
221 throw new NoSingleResultException("No value for " + uri + "/actions", e);
224 sclContext.put("graph", oldGraph);