]> gerrit.simantics Code Review - simantics/platform.git/blob
bbe8111e54ceb46168a94a6a5e0a0c3954303f94
[simantics/platform.git] /
1 package org.simantics.modeling.ui.handlers.e4;\r
2 \r
3 import java.util.Collection;\r
4 import java.util.HashSet;\r
5 import java.util.Set;\r
6 \r
7 import javax.inject.Named;\r
8 \r
9 import org.eclipse.e4.core.di.annotations.Execute;\r
10 import org.eclipse.e4.ui.services.IServiceConstants;\r
11 import org.eclipse.jface.dialogs.MessageDialog;\r
12 import org.eclipse.swt.widgets.Shell;\r
13 import org.simantics.Simantics;\r
14 import org.simantics.db.ReadGraph;\r
15 import org.simantics.db.Resource;\r
16 import org.simantics.db.Session;\r
17 import org.simantics.db.common.request.ObjectsWithType;\r
18 import org.simantics.db.exception.DatabaseException;\r
19 import org.simantics.db.layer0.adapter.Instances;\r
20 import org.simantics.db.request.Read;\r
21 import org.simantics.modeling.ModelingResources;\r
22 import org.simantics.modeling.typicals.SyncTypicalTemplatesToInstances;\r
23 import org.simantics.modeling.ui.documents.OpenPlainTextDocumentAdapter;\r
24 import org.simantics.modeling.ui.typicals.RuleChooserDialog;\r
25 import org.simantics.operation.Layer0X;\r
26 import org.simantics.simulation.ontology.SimulationResource;\r
27 import org.simantics.utils.ui.ErrorLogger;\r
28 \r
29 /**\r
30  * @author Tuukka Lehtonen\r
31  */\r
32 public class SyncActiveModelTypicals {\r
33 \r
34     @Execute\r
35     public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell) {\r
36         Session session = Simantics.getSession();\r
37         try {\r
38             Resource[] activeModelTypicalTemplates = session.syncRequest(new Read<Resource[]>() {\r
39                 @Override\r
40                 public Resource[] perform(ReadGraph graph) throws DatabaseException {\r
41                     Layer0X L0X = Layer0X.getInstance(graph);\r
42                     ModelingResources MOD = ModelingResources.getInstance(graph);\r
43                     SimulationResource SIMU = SimulationResource.getInstance(graph);\r
44 \r
45                     Collection<Resource> activeModels = graph.syncRequest(new ObjectsWithType(Simantics.getProjectResource(), L0X.Activates, SIMU.Model));\r
46                     Instances query = graph.adapt(MOD.MasterTypicalCompositeType, Instances.class);\r
47 \r
48                     Set<Resource> result = new HashSet<Resource>();\r
49                     for (Resource activeModel : activeModels) {\r
50                         Collection<Resource> typicalComposites = query.find(graph, activeModel);\r
51                         for (Resource typicalComposite : typicalComposites) {\r
52                             for (Resource typicalDiagram : graph.getObjects(typicalComposite, MOD.CompositeToDiagram)) {\r
53                                 result.add(typicalDiagram);\r
54                             }\r
55                         }\r
56                     }\r
57                     return result.toArray(Resource.NONE);\r
58                 }\r
59             });\r
60 \r
61             if (activeModelTypicalTemplates.length > 0) {\r
62 //                StringBuilder msg = new StringBuilder("Are you sure you want to synchronize ")\r
63 //                .append(activeModelTypicalTemplates.length)\r
64 //                .append(" typical template");\r
65 //                if (activeModelTypicalTemplates.length > 1)\r
66 //                    msg.append("s to all their instances?");\r
67 //                else\r
68 //                    msg.append(" to all its instances?");\r
69 //                if (!MessageDialog.openConfirm(shell, "Synchronize All Typical Templates with Instances", msg.toString()))\r
70 //                    return null;\r
71 \r
72                 StringBuilder msg = new StringBuilder("Synchronizing ")\r
73                 .append(activeModelTypicalTemplates.length)\r
74                 .append(" typical template");\r
75                 if (activeModelTypicalTemplates.length > 1)\r
76                     msg.append("s to all their instances.");\r
77                 else\r
78                     msg.append(" to all its instances.");\r
79 \r
80                 RuleChooserDialog.RuleResult result = RuleChooserDialog.choose(shell, msg.toString(), activeModelTypicalTemplates);\r
81                 if(result == null) return;\r
82 \r
83                 SyncTypicalTemplatesToInstances req = new SyncTypicalTemplatesToInstances(result.selectedRules, activeModelTypicalTemplates).logging(result.logging); \r
84                 session.syncRequest(req);\r
85                 if (result.logging) {\r
86                     for(Resource log : req.logs)\r
87                         new OpenPlainTextDocumentAdapter().openEditor(log);\r
88                 }\r
89 \r
90             } else {\r
91                 MessageDialog.openInformation(shell, "No Typical Templates", "The currently active model doesn't contain any typical diagram templates.");\r
92             }\r
93         } catch (Exception e) {\r
94             ErrorLogger.defaultLogError(e);\r
95         }\r
96         return;\r
97     }\r
98 \r
99 }\r