]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/NetworkElementActionMenuContribution.java
Presentation of component-specific actions.
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / contributions / NetworkElementActionMenuContribution.java
1 package org.simantics.district.network.ui.contributions;
2
3 import java.util.Collections;
4 import java.util.List;
5
6 import javax.inject.Named;
7
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;
41
42 public class NetworkElementActionMenuContribution {
43
44     static private Logger LOGGER = LoggerFactory.getLogger(NetworkElementActionMenuContribution.class);
45     
46     @AboutToShow
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)
50             return;
51         
52         Resource vertex = vertices.get(0);
53         
54         List<Tuple2> actions;
55         try {
56             actions = Simantics.getSession().syncRequest(new ActionItemRequest(vertex), TransientCacheListener.instance());
57         } catch (DatabaseException e) {
58             LOGGER.error("Error fetching action items for " + vertex, e);
59             return;
60         }
61         
62         items.add(MMenuFactory.INSTANCE.createMenuSeparator());
63         MMenu subMenu = MMenuFactory.INSTANCE.createMenu();
64         List<MMenuElement> children = subMenu.getChildren();
65         subMenu.setLabel("Component Actions");
66         items.add(subMenu);
67         
68         for (Tuple2 action : actions) {
69             String label = (String) action.c0;
70             @SuppressWarnings("rawtypes")
71             Function function = (Function) action.c1;
72
73             Object handler = new Object() {
74                 @CanExecute
75                 public boolean canExecute() {
76                     try {
77                         return Simantics.getSession().syncRequest(new ResourceRead<Boolean>(vertex) {
78                             @Override
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);
82                             }
83                         }, TransientCacheListener.instance());
84                     } catch (DatabaseException e) {
85                         return false;
86                     }
87                 }
88                 
89                 @Execute
90                 public void execute() {
91                     Simantics.getSession().asyncRequest(new WriteRequest() {
92                         @SuppressWarnings("unchecked")
93                         @Override
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)
98                                 return;
99
100                             Variable v = Variables.getVariable(graph, graph.getSingleObject(component, ModelingResources.getInstance(graph).ElementToComponent));
101                             
102                             graph.markUndoPoint();
103                             Layer0Utils.addCommentMetadata(graph, label + " for " + v.getName(graph));
104                             
105                             Simantics.applySCLWrite(graph, function, v);
106                         }
107                     }, (DatabaseException e) -> LOGGER.error("Running command " + label + " for " + vertex + " failed", e));
108                 }
109             };
110             
111             MDirectMenuItem dynamicItem = MMenuFactory.INSTANCE.createDirectMenuItem();
112             dynamicItem.setLabel(label);
113             dynamicItem.setObject(handler);
114             children.add(dynamicItem);
115         }
116     }
117     
118     @AboutToHide
119     public void aboutToHide() {
120     }
121
122     private static final class ActionItemRequest extends ResourceRead<List<Tuple2>> {
123         private ActionItemRequest(Resource resource) {
124             super(resource);
125         }
126     
127         @SuppressWarnings("unchecked")
128         @Override
129         public List<Tuple2> perform(ReadGraph graph) throws DatabaseException {
130             Layer0 L0 = Layer0.getInstance(graph);
131             DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
132             
133             if (!graph.isInstanceOf(resource, DN.Element))
134                 return Collections.emptyList();
135             
136             Resource component = graph.getPossibleObject(resource, DN.MappedComponent);
137             if (component == null)
138                 return Collections.emptyList();
139             
140             Resource mapping = graph.getPossibleObject(resource, DN.HasMapping);
141             if (mapping == null)
142                 return Collections.emptyList();
143             
144             String name = graph.getPossibleRelatedValue(mapping, DN.Mapping_ComponentType);
145             if (name == null)
146                 return Collections.emptyList();
147             
148             Resource root = graph.getSingleObject(graph.getSingleObject(mapping, L0.PartOf), L0.PartOf);
149             Resource uc = Layer0Utils.getPossibleChild(graph, root, name);
150             if (uc == null)
151                 return Collections.emptyList();
152             
153             Resource actionsModule = Layer0Utils.getPossibleChild(graph, uc, "Actions");
154             if (actionsModule == null)
155                 return Collections.emptyList();
156             
157             String uri = graph.getURI(actionsModule);
158             SCLContext sclContext = SCLContext.getCurrent();
159             Object oldGraph = sclContext.put("graph", graph);
160             try {
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);
165             }
166             catch (ValueNotFound e) {
167                 throw new NoSingleResultException("No value for " + uri + "/actions", e);
168             }
169             finally {
170                 sclContext.put("graph", oldGraph);
171             }
172         }
173     }
174 }