]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/NetworkElementActionMenuContribution.java
Merge "Open SCL script output console on demand for context menu commands."
[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         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                     // Handler that only opens the SCL script console on demand
95                     SCLReportingHandler handler = new SCLReportingHandler() {
96                         
97                         private SCLReportingHandler handler;
98                         
99                         // Get a handler for the SCL script console view
100                         private SCLReportingHandler getHandler() {
101                             if (handler == null) {
102                                 handler = SCLScripts.getOrCreateConsoleCommandSession().second;
103                             }   
104                             return handler;
105                         }
106                         
107                         @Override
108                         public void printError(String error) {
109                             display.asyncExec(() -> {
110                                 getHandler().printError(error);
111                             });
112                         }
113                         
114                         @Override
115                         public void printCommand(String command) {
116                             display.asyncExec(() -> {
117                                 getHandler().printCommand(command);
118                             });
119                         }
120                         
121                         @Override
122                         public void print(String text) {
123                             display.asyncExec(() -> {
124                                 getHandler().print(text);
125                             });
126                         }
127                         
128                         @Override
129                         public void didWork(double amount) {
130                             display.asyncExec(() -> {
131                                 getHandler().didWork(amount);
132                             });
133                         }
134                     };
135                     
136                     Simantics.getSession().asyncRequest(new WriteRequest() {
137                         @SuppressWarnings("unchecked")
138                         @Override
139                         public void perform(WriteGraph graph) throws DatabaseException {
140                             DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
141                             Resource component = graph.getPossibleObject(vertex, DN.MappedComponent);
142                             if (component == null)
143                                 return;
144
145                             Variable v = Variables.getVariable(graph, graph.getSingleObject(component, ModelingResources.getInstance(graph).ElementToComponent));
146                             
147                             graph.markUndoPoint();
148                             Layer0Utils.addCommentMetadata(graph, label + " for " + v.getName(graph));
149                             
150                             SCLContext context = SCLContext.getCurrent();
151                             Object oldHandler = context.put(SCLReportingHandler.REPORTING_HANDLER, handler);
152                             try {
153                                 Simantics.applySCLWrite(graph, function, v);
154                             }
155                             finally {
156                                 context.put(SCLReportingHandler.REPORTING_HANDLER, oldHandler);
157                             }
158                         }
159                     }, (DatabaseException e) -> {
160                         if (e != null) LOGGER.error("Running command " + label + " for " + vertex + " failed", e);
161                     });
162                 }
163             };
164             
165             MDirectMenuItem dynamicItem = MMenuFactory.INSTANCE.createDirectMenuItem();
166             dynamicItem.setLabel(label);
167             dynamicItem.setObject(handler);
168             children.add(dynamicItem);
169         }
170     }
171     
172     @AboutToHide
173     public void aboutToHide() {
174     }
175
176     private static final class ActionItemRequest extends ResourceRead<List<Tuple2>> {
177         private ActionItemRequest(Resource resource) {
178             super(resource);
179         }
180     
181         @SuppressWarnings("unchecked")
182         @Override
183         public List<Tuple2> perform(ReadGraph graph) throws DatabaseException {
184             Layer0 L0 = Layer0.getInstance(graph);
185             DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
186             
187             if (!graph.isInstanceOf(resource, DN.Element))
188                 return Collections.emptyList();
189             
190             Resource component = graph.getPossibleObject(resource, DN.MappedComponent);
191             if (component == null)
192                 return Collections.emptyList();
193             
194             Resource mapping = graph.getPossibleObject(resource, DN.HasMapping);
195             if (mapping == null)
196                 return Collections.emptyList();
197             
198             String name = graph.getPossibleRelatedValue(mapping, DN.Mapping_ComponentType);
199             if (name == null)
200                 return Collections.emptyList();
201             
202             Resource root = graph.getSingleObject(graph.getSingleObject(mapping, L0.PartOf), L0.PartOf);
203             Resource uc = Layer0Utils.getPossibleChild(graph, root, name);
204             if (uc == null)
205                 return Collections.emptyList();
206             
207             Resource actionsModule = Layer0Utils.getPossibleChild(graph, uc, "Actions");
208             if (actionsModule == null)
209                 return Collections.emptyList();
210             
211             String uri = graph.getURI(actionsModule);
212             SCLContext sclContext = SCLContext.getCurrent();
213             Object oldGraph = sclContext.put("graph", graph);
214             try {
215                 @SuppressWarnings("rawtypes")
216                 Function actionsFun = (Function) SCLOsgi.MODULE_REPOSITORY.getValue(uri, "actions");
217                 Variable variable = Variables.getPossibleVariable(graph, graph.getSingleObject(component, ModelingResources.getInstance(graph).ElementToComponent));
218                 return (List<Tuple2>) actionsFun.apply(variable);
219             }
220             catch (ValueNotFound e) {
221                 throw new NoSingleResultException("No value for " + uri + "/actions", e);
222             }
223             finally {
224                 sclContext.put("graph", oldGraph);
225             }
226         }
227     }
228 }