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 items.add(MMenuFactory.INSTANCE.createMenuSeparator());
63 MMenu subMenu = MMenuFactory.INSTANCE.createMenu();
64 List<MMenuElement> children = subMenu.getChildren();
65 subMenu.setLabel("Component Actions");
68 for (Tuple2 action : actions) {
69 String label = (String) action.c0;
70 @SuppressWarnings("rawtypes")
71 Function function = (Function) action.c1;
73 Object handler = new Object() {
75 public boolean canExecute() {
77 return Simantics.getSession().syncRequest(new ResourceRead<Boolean>(vertex) {
79 public Boolean perform(ReadGraph graph) throws DatabaseException {
80 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
81 return graph.isInstanceOf(vertex, DN.Element) && graph.hasStatement(vertex, DN.MappedComponent);
83 }, TransientCacheListener.instance());
84 } catch (DatabaseException e) {
90 public void execute() {
91 Simantics.getSession().asyncRequest(new WriteRequest() {
92 @SuppressWarnings("unchecked")
94 public void perform(WriteGraph graph) throws DatabaseException {
95 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
96 Resource component = graph.getPossibleObject(vertex, DN.MappedComponent);
97 if (component == null)
100 Variable v = Variables.getVariable(graph, graph.getSingleObject(component, ModelingResources.getInstance(graph).ElementToComponent));
102 graph.markUndoPoint();
103 Layer0Utils.addCommentMetadata(graph, label + " for " + v.getName(graph));
105 Simantics.applySCLWrite(graph, function, v);
107 }, (DatabaseException e) -> LOGGER.error("Running command " + label + " for " + vertex + " failed", e));
111 MDirectMenuItem dynamicItem = MMenuFactory.INSTANCE.createDirectMenuItem();
112 dynamicItem.setLabel(label);
113 dynamicItem.setObject(handler);
114 children.add(dynamicItem);
119 public void aboutToHide() {
122 private static final class ActionItemRequest extends ResourceRead<List<Tuple2>> {
123 private ActionItemRequest(Resource resource) {
127 @SuppressWarnings("unchecked")
129 public List<Tuple2> perform(ReadGraph graph) throws DatabaseException {
130 Layer0 L0 = Layer0.getInstance(graph);
131 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
133 if (!graph.isInstanceOf(resource, DN.Element))
134 return Collections.emptyList();
136 Resource component = graph.getPossibleObject(resource, DN.MappedComponent);
137 if (component == null)
138 return Collections.emptyList();
140 Resource mapping = graph.getPossibleObject(resource, DN.HasMapping);
142 return Collections.emptyList();
144 String name = graph.getPossibleRelatedValue(mapping, DN.Mapping_ComponentType);
146 return Collections.emptyList();
148 Resource root = graph.getSingleObject(graph.getSingleObject(mapping, L0.PartOf), L0.PartOf);
149 Resource uc = Layer0Utils.getPossibleChild(graph, root, name);
151 return Collections.emptyList();
153 Resource actionsModule = Layer0Utils.getPossibleChild(graph, uc, "Actions");
154 if (actionsModule == null)
155 return Collections.emptyList();
157 String uri = graph.getURI(actionsModule);
158 SCLContext sclContext = SCLContext.getCurrent();
159 Object oldGraph = sclContext.put("graph", graph);
161 @SuppressWarnings("rawtypes")
162 Function actionsFun = (Function) SCLOsgi.MODULE_REPOSITORY.getValue(uri, "actions");
163 Variable variable = Variables.getPossibleVariable(graph, graph.getSingleObject(component, ModelingResources.getInstance(graph).ElementToComponent));
164 return (List<Tuple2>) actionsFun.apply(variable);
166 catch (ValueNotFound e) {
167 throw new NoSingleResultException("No value for " + uri + "/actions", e);
170 sclContext.put("graph", oldGraph);