]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/NetworkElementActionMenuContribution.java
Updating of component properties when type code changes
[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.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;
44
45 public class NetworkElementActionMenuContribution {
46
47     static private Logger LOGGER = LoggerFactory.getLogger(NetworkElementActionMenuContribution.class);
48     
49     @AboutToShow
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)
53             return;
54         
55         Resource vertex = vertices.get(0);
56         
57         List<Tuple2> actions;
58         try {
59             actions = Simantics.getSession().syncRequest(new ActionItemRequest(vertex), TransientCacheListener.instance());
60         } catch (DatabaseException e) {
61             LOGGER.error("Error fetching action items for " + vertex, e);
62             return;
63         }
64         
65         if (actions == null || actions.isEmpty())
66             return;
67         
68         items.add(MMenuFactory.INSTANCE.createMenuSeparator());
69         MMenu subMenu = MMenuFactory.INSTANCE.createMenu();
70         List<MMenuElement> children = subMenu.getChildren();
71         subMenu.setLabel("Component Actions");
72         items.add(subMenu);
73
74         for (Tuple2 action : actions) {
75             String label = (String) action.c0;
76             @SuppressWarnings("rawtypes")
77             Function function = (Function) action.c1;
78
79             Object handler = new Object() {
80                 @CanExecute
81                 public boolean canExecute() {
82                     try {
83                         return Simantics.getSession().syncRequest(new ResourceRead<Boolean>(vertex) {
84                             @Override
85                             public Boolean perform(ReadGraph graph) throws DatabaseException {
86                                 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
87                                 return graph.isInstanceOf(vertex, DN.Element) && graph.hasStatement(vertex, DN.MappedComponent);
88                             }
89                         }, TransientCacheListener.instance());
90                     } catch (DatabaseException e) {
91                         return false;
92                     }
93                 }
94                 
95                 @Execute
96                 public void execute() {
97                     // Handler that only opens the SCL script console on demand
98                     SCLReportingHandler handler = new SCLReportingHandler() {
99                         
100                         private SCLReportingHandler handler;
101                         
102                         // Get a handler for the SCL script console view
103                         private SCLReportingHandler getHandler() {
104                             if (handler == null) {
105                                 handler = SCLScripts.getOrCreateConsoleCommandSession().second;
106                             }   
107                             return handler;
108                         }
109                         
110                         @Override
111                         public void printError(String error) {
112                             display.asyncExec(() -> {
113                                 getHandler().printError(error);
114                             });
115                         }
116                         
117                         @Override
118                         public void printCommand(String command) {
119                             display.asyncExec(() -> {
120                                 getHandler().printCommand(command);
121                             });
122                         }
123                         
124                         @Override
125                         public void print(String text) {
126                             display.asyncExec(() -> {
127                                 getHandler().print(text);
128                             });
129                         }
130                         
131                         @Override
132                         public void didWork(double amount) {
133                             display.asyncExec(() -> {
134                                 getHandler().didWork(amount);
135                             });
136                         }
137                     };
138                     
139                     Simantics.getSession().asyncRequest(new WriteRequest() {
140                         @SuppressWarnings("unchecked")
141                         @Override
142                         public void perform(WriteGraph graph) throws DatabaseException {
143                             DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
144                             Resource component = graph.getPossibleObject(vertex, DN.MappedComponent);
145                             if (component == null)
146                                 return;
147
148                             Variable v = Variables.getVariable(graph, graph.getSingleObject(component, ModelingResources.getInstance(graph).ElementToComponent));
149                             
150                             graph.markUndoPoint();
151                             Layer0Utils.addCommentMetadata(graph, label + " for " + v.getName(graph));
152                             
153                             SCLContext context = SCLContext.getCurrent();
154                             Object oldHandler = context.put(SCLReportingHandler.REPORTING_HANDLER, handler);
155                             try {
156                                 Simantics.applySCLWrite(graph, function, v);
157                             }
158                             finally {
159                                 context.put(SCLReportingHandler.REPORTING_HANDLER, oldHandler);
160                             }
161                         }
162                     }, (DatabaseException e) -> {
163                         if (e != null) LOGGER.error("Running command " + label + " for " + vertex + " failed", e);
164                     });
165                 }
166             };
167             
168             MDirectMenuItem dynamicItem = MMenuFactory.INSTANCE.createDirectMenuItem();
169             dynamicItem.setLabel(label);
170             dynamicItem.setObject(handler);
171             children.add(dynamicItem);
172         }
173     }
174     
175     @AboutToHide
176     public void aboutToHide() {
177     }
178
179     private static final class ActionItemRequest extends ResourceRead<List<Tuple2>> {
180         private ActionItemRequest(Resource resource) {
181             super(resource);
182         }
183     
184         @SuppressWarnings("unchecked")
185         @Override
186         public List<Tuple2> perform(ReadGraph graph) throws DatabaseException {
187             Layer0 L0 = Layer0.getInstance(graph);
188             DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
189             
190             if (!graph.isInstanceOf(resource, DN.Element))
191                 return Collections.emptyList();
192             
193             Resource component = graph.getPossibleObject(resource, DN.MappedComponent);
194             if (component == null)
195                 return Collections.emptyList();
196             
197             Resource mapping = graph.getPossibleObject(resource, DN.HasMapping);
198             if (mapping == null)
199                 return Collections.emptyList();
200             
201             String name = graph.getPossibleRelatedValue(mapping, DN.Mapping_ComponentType);
202             if (name == null)
203                 return Collections.emptyList();
204             
205             Resource root = graph.getSingleObject(graph.getSingleObject(mapping, L0.PartOf), L0.PartOf);
206             Resource uc = Layer0Utils.getPossibleChild(graph, root, name);
207             if (uc == null)
208                 return Collections.emptyList();
209             
210             Resource actionsModule = Layer0Utils.getPossibleChild(graph, uc, "Actions");
211             if (actionsModule == null)
212                 return Collections.emptyList();
213             
214             String uri = graph.getURI(actionsModule);
215             SCLContext sclContext = SCLContext.getCurrent();
216             Object oldGraph = sclContext.put("graph", graph);
217             try {
218                 @SuppressWarnings("rawtypes")
219                 Function actionsFun = (Function) SCLOsgi.MODULE_REPOSITORY.getValue(uri, "actions");
220                 Variable variable = Variables.getPossibleVariable(graph, graph.getSingleObject(component, ModelingResources.getInstance(graph).ElementToComponent));
221                 return (List<Tuple2>) actionsFun.apply(variable);
222             }
223             catch (ValueNotFound e) {
224                 throw new NoSingleResultException("No value for " + uri + "/actions", e);
225             }
226             finally {
227                 sclContext.put("graph", oldGraph);
228             }
229         }
230     }
231 }