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.simantics.Simantics;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
22 import org.simantics.db.common.request.ResourceRead;
23 import org.simantics.db.common.request.WriteRequest;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.exception.NoSingleResultException;
26 import org.simantics.db.layer0.SelectionHints;
27 import org.simantics.db.layer0.util.Layer0Utils;
28 import org.simantics.db.layer0.variable.Variable;
29 import org.simantics.db.layer0.variable.Variables;
30 import org.simantics.district.network.ontology.DistrictNetworkResource;
31 import org.simantics.layer0.Layer0;
32 import org.simantics.modeling.ModelingResources;
33 import org.simantics.scl.compiler.top.ValueNotFound;
34 import org.simantics.scl.osgi.SCLOsgi;
35 import org.simantics.scl.runtime.SCLContext;
36 import org.simantics.scl.runtime.function.Function;
37 import org.simantics.scl.runtime.tuple.Tuple2;
38 import org.simantics.utils.ui.ISelectionUtils;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class NetworkElementActionMenuContribution {
44 static private Logger LOGGER = LoggerFactory.getLogger(NetworkElementActionMenuContribution.class);
47 public void aboutToShow(@Named(IServiceConstants.ACTIVE_SELECTION) Object selection, List<MMenuElement> items) {
48 final List<Resource> vertices = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
49 if (vertices.size() != 1)
52 Resource vertex = vertices.get(0);
56 actions = Simantics.getSession().syncRequest(new ActionItemRequest(vertex), TransientCacheListener.instance());
57 } catch (DatabaseException e) {
58 LOGGER.error("Error fetching action items for " + vertex, e);
62 if (actions == null || actions.isEmpty())
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 Simantics.getSession().asyncRequest(new WriteRequest() {
95 @SuppressWarnings("unchecked")
97 public void perform(WriteGraph graph) throws DatabaseException {
98 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
99 Resource component = graph.getPossibleObject(vertex, DN.MappedComponent);
100 if (component == null)
103 Variable v = Variables.getVariable(graph, graph.getSingleObject(component, ModelingResources.getInstance(graph).ElementToComponent));
105 graph.markUndoPoint();
106 Layer0Utils.addCommentMetadata(graph, label + " for " + v.getName(graph));
108 Simantics.applySCLWrite(graph, function, v);
110 }, (DatabaseException e) -> LOGGER.error("Running command " + label + " for " + vertex + " failed", e));
114 MDirectMenuItem dynamicItem = MMenuFactory.INSTANCE.createDirectMenuItem();
115 dynamicItem.setLabel(label);
116 dynamicItem.setObject(handler);
117 children.add(dynamicItem);
122 public void aboutToHide() {
125 private static final class ActionItemRequest extends ResourceRead<List<Tuple2>> {
126 private ActionItemRequest(Resource resource) {
130 @SuppressWarnings("unchecked")
132 public List<Tuple2> perform(ReadGraph graph) throws DatabaseException {
133 Layer0 L0 = Layer0.getInstance(graph);
134 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
136 if (!graph.isInstanceOf(resource, DN.Element))
137 return Collections.emptyList();
139 Resource component = graph.getPossibleObject(resource, DN.MappedComponent);
140 if (component == null)
141 return Collections.emptyList();
143 Resource mapping = graph.getPossibleObject(resource, DN.HasMapping);
145 return Collections.emptyList();
147 String name = graph.getPossibleRelatedValue(mapping, DN.Mapping_ComponentType);
149 return Collections.emptyList();
151 Resource root = graph.getSingleObject(graph.getSingleObject(mapping, L0.PartOf), L0.PartOf);
152 Resource uc = Layer0Utils.getPossibleChild(graph, root, name);
154 return Collections.emptyList();
156 Resource actionsModule = Layer0Utils.getPossibleChild(graph, uc, "Actions");
157 if (actionsModule == null)
158 return Collections.emptyList();
160 String uri = graph.getURI(actionsModule);
161 SCLContext sclContext = SCLContext.getCurrent();
162 Object oldGraph = sclContext.put("graph", graph);
164 @SuppressWarnings("rawtypes")
165 Function actionsFun = (Function) SCLOsgi.MODULE_REPOSITORY.getValue(uri, "actions");
166 Variable variable = Variables.getPossibleVariable(graph, graph.getSingleObject(component, ModelingResources.getInstance(graph).ElementToComponent));
167 return (List<Tuple2>) actionsFun.apply(variable);
169 catch (ValueNotFound e) {
170 throw new NoSingleResultException("No value for " + uri + "/actions", e);
173 sclContext.put("graph", oldGraph);