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