]> gerrit.simantics Code Review - simantics/district.git/blob
ec03ab87ffe5020c22d6106640ec53a204137025
[simantics/district.git] /
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         if (actions == null || actions.isEmpty())
63             return;
64         
65         items.add(MMenuFactory.INSTANCE.createMenuSeparator());
66         MMenu subMenu = MMenuFactory.INSTANCE.createMenu();
67         List<MMenuElement> children = subMenu.getChildren();
68         subMenu.setLabel("Component Actions");
69         items.add(subMenu);
70         
71         for (Tuple2 action : actions) {
72             String label = (String) action.c0;
73             @SuppressWarnings("rawtypes")
74             Function function = (Function) action.c1;
75
76             Object handler = new Object() {
77                 @CanExecute
78                 public boolean canExecute() {
79                     try {
80                         return Simantics.getSession().syncRequest(new ResourceRead<Boolean>(vertex) {
81                             @Override
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);
85                             }
86                         }, TransientCacheListener.instance());
87                     } catch (DatabaseException e) {
88                         return false;
89                     }
90                 }
91                 
92                 @Execute
93                 public void execute() {
94                     Simantics.getSession().asyncRequest(new WriteRequest() {
95                         @SuppressWarnings("unchecked")
96                         @Override
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)
101                                 return;
102
103                             Variable v = Variables.getVariable(graph, graph.getSingleObject(component, ModelingResources.getInstance(graph).ElementToComponent));
104                             
105                             graph.markUndoPoint();
106                             Layer0Utils.addCommentMetadata(graph, label + " for " + v.getName(graph));
107                             
108                             Simantics.applySCLWrite(graph, function, v);
109                         }
110                     }, (DatabaseException e) -> LOGGER.error("Running command " + label + " for " + vertex + " failed", e));
111                 }
112             };
113             
114             MDirectMenuItem dynamicItem = MMenuFactory.INSTANCE.createDirectMenuItem();
115             dynamicItem.setLabel(label);
116             dynamicItem.setObject(handler);
117             children.add(dynamicItem);
118         }
119     }
120     
121     @AboutToHide
122     public void aboutToHide() {
123     }
124
125     private static final class ActionItemRequest extends ResourceRead<List<Tuple2>> {
126         private ActionItemRequest(Resource resource) {
127             super(resource);
128         }
129     
130         @SuppressWarnings("unchecked")
131         @Override
132         public List<Tuple2> perform(ReadGraph graph) throws DatabaseException {
133             Layer0 L0 = Layer0.getInstance(graph);
134             DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
135             
136             if (!graph.isInstanceOf(resource, DN.Element))
137                 return Collections.emptyList();
138             
139             Resource component = graph.getPossibleObject(resource, DN.MappedComponent);
140             if (component == null)
141                 return Collections.emptyList();
142             
143             Resource mapping = graph.getPossibleObject(resource, DN.HasMapping);
144             if (mapping == null)
145                 return Collections.emptyList();
146             
147             String name = graph.getPossibleRelatedValue(mapping, DN.Mapping_ComponentType);
148             if (name == null)
149                 return Collections.emptyList();
150             
151             Resource root = graph.getSingleObject(graph.getSingleObject(mapping, L0.PartOf), L0.PartOf);
152             Resource uc = Layer0Utils.getPossibleChild(graph, root, name);
153             if (uc == null)
154                 return Collections.emptyList();
155             
156             Resource actionsModule = Layer0Utils.getPossibleChild(graph, uc, "Actions");
157             if (actionsModule == null)
158                 return Collections.emptyList();
159             
160             String uri = graph.getURI(actionsModule);
161             SCLContext sclContext = SCLContext.getCurrent();
162             Object oldGraph = sclContext.put("graph", graph);
163             try {
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);
168             }
169             catch (ValueNotFound e) {
170                 throw new NoSingleResultException("No value for " + uri + "/actions", e);
171             }
172             finally {
173                 sclContext.put("graph", oldGraph);
174             }
175         }
176     }
177 }