--- /dev/null
+package org.simantics.modeling.ui.handlers.e4;\r
+\r
+import java.util.Collection;\r
+import java.util.HashSet;\r
+import java.util.Set;\r
+\r
+import javax.inject.Named;\r
+\r
+import org.eclipse.e4.core.di.annotations.Execute;\r
+import org.eclipse.e4.ui.services.IServiceConstants;\r
+import org.eclipse.jface.dialogs.MessageDialog;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.simantics.Simantics;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.common.request.ObjectsWithType;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.Instances;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.modeling.typicals.SyncTypicalTemplatesToInstances;\r
+import org.simantics.modeling.ui.documents.OpenPlainTextDocumentAdapter;\r
+import org.simantics.modeling.ui.typicals.RuleChooserDialog;\r
+import org.simantics.operation.Layer0X;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class SyncActiveModelTypicals {\r
+\r
+ @Execute\r
+ public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell) {\r
+ Session session = Simantics.getSession();\r
+ try {\r
+ Resource[] activeModelTypicalTemplates = session.syncRequest(new Read<Resource[]>() {\r
+ @Override\r
+ public Resource[] perform(ReadGraph graph) throws DatabaseException {\r
+ Layer0X L0X = Layer0X.getInstance(graph);\r
+ ModelingResources MOD = ModelingResources.getInstance(graph);\r
+ SimulationResource SIMU = SimulationResource.getInstance(graph);\r
+\r
+ Collection<Resource> activeModels = graph.syncRequest(new ObjectsWithType(Simantics.getProjectResource(), L0X.Activates, SIMU.Model));\r
+ Instances query = graph.adapt(MOD.MasterTypicalCompositeType, Instances.class);\r
+\r
+ Set<Resource> result = new HashSet<Resource>();\r
+ for (Resource activeModel : activeModels) {\r
+ Collection<Resource> typicalComposites = query.find(graph, activeModel);\r
+ for (Resource typicalComposite : typicalComposites) {\r
+ for (Resource typicalDiagram : graph.getObjects(typicalComposite, MOD.CompositeToDiagram)) {\r
+ result.add(typicalDiagram);\r
+ }\r
+ }\r
+ }\r
+ return result.toArray(Resource.NONE);\r
+ }\r
+ });\r
+\r
+ if (activeModelTypicalTemplates.length > 0) {\r
+// StringBuilder msg = new StringBuilder("Are you sure you want to synchronize ")\r
+// .append(activeModelTypicalTemplates.length)\r
+// .append(" typical template");\r
+// if (activeModelTypicalTemplates.length > 1)\r
+// msg.append("s to all their instances?");\r
+// else\r
+// msg.append(" to all its instances?");\r
+// if (!MessageDialog.openConfirm(shell, "Synchronize All Typical Templates with Instances", msg.toString()))\r
+// return null;\r
+\r
+ StringBuilder msg = new StringBuilder("Synchronizing ")\r
+ .append(activeModelTypicalTemplates.length)\r
+ .append(" typical template");\r
+ if (activeModelTypicalTemplates.length > 1)\r
+ msg.append("s to all their instances.");\r
+ else\r
+ msg.append(" to all its instances.");\r
+\r
+ RuleChooserDialog.RuleResult result = RuleChooserDialog.choose(shell, msg.toString(), activeModelTypicalTemplates);\r
+ if(result == null) return;\r
+\r
+ SyncTypicalTemplatesToInstances req = new SyncTypicalTemplatesToInstances(result.selectedRules, activeModelTypicalTemplates).logging(result.logging); \r
+ session.syncRequest(req);\r
+ if (result.logging) {\r
+ for(Resource log : req.logs)\r
+ new OpenPlainTextDocumentAdapter().openEditor(log);\r
+ }\r
+\r
+ } else {\r
+ MessageDialog.openInformation(shell, "No Typical Templates", "The currently active model doesn't contain any typical diagram templates.");\r
+ }\r
+ } catch (Exception e) {\r
+ ErrorLogger.defaultLogError(e);\r
+ }\r
+ return;\r
+ }\r
+\r
+}\r