]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/typicals/SyncActiveModelTypicals.java
Sync git svn branch with SVN repository r33319.
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / typicals / SyncActiveModelTypicals.java
1 package org.simantics.modeling.ui.typicals;\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 org.eclipse.core.commands.AbstractHandler;\r
8 import org.eclipse.core.commands.ExecutionEvent;\r
9 import org.eclipse.core.commands.ExecutionException;\r
10 import org.eclipse.jface.dialogs.MessageDialog;\r
11 import org.eclipse.swt.widgets.Shell;\r
12 import org.eclipse.ui.handlers.HandlerUtil;\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.operation.Layer0X;\r
25 import org.simantics.simulation.ontology.SimulationResource;\r
26 import org.simantics.utils.ui.ErrorLogger;\r
27 \r
28 /**\r
29  * @author Tuukka Lehtonen\r
30  */\r
31 public class SyncActiveModelTypicals extends AbstractHandler {\r
32 \r
33     @Override\r
34     public Object execute(ExecutionEvent event) throws ExecutionException {\r
35         Shell shell = HandlerUtil.getActiveShell(event);\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 null;\r
82 \r
83                 session.markUndoPoint();\r
84                 SyncTypicalTemplatesToInstances req = new SyncTypicalTemplatesToInstances(result.selectedRules, activeModelTypicalTemplates).logging(result.logging); \r
85                 session.syncRequest(req);\r
86                 if (result.logging) {\r
87                     for(Resource log : req.logs)\r
88                         new OpenPlainTextDocumentAdapter().openEditor(log);\r
89                 }\r
90 \r
91             } else {\r
92                 MessageDialog.openInformation(shell, "No Typical Templates", "The currently active model doesn't contain any typical diagram templates.");\r
93             }\r
94         } catch (Exception e) {\r
95             ErrorLogger.defaultLogError(e);\r
96         }\r
97         return null;\r
98     }\r
99 \r
100 }\r