1 package org.simantics.modeling.ui.handlers.e4;
3 import java.util.Collection;
4 import java.util.HashSet;
7 import javax.inject.Named;
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;
30 * @author Tuukka Lehtonen
32 public class SyncActiveModelTypicals {
35 public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell) {
36 Session session = Simantics.getSession();
38 Resource[] activeModelTypicalTemplates = session.syncRequest(new Read<Resource[]>() {
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);
45 Collection<Resource> activeModels = graph.syncRequest(new ObjectsWithType(Simantics.getProjectResource(), L0X.Activates, SIMU.Model));
46 Instances query = graph.adapt(MOD.MasterTypicalCompositeType, Instances.class);
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);
57 return result.toArray(Resource.NONE);
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?");
68 // msg.append(" to all its instances?");
69 // if (!MessageDialog.openConfirm(shell, "Synchronize All Typical Templates with Instances", msg.toString()))
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.");
78 msg.append(" to all its instances.");
80 RuleChooserDialog.RuleResult result = RuleChooserDialog.choose(shell, msg.toString(), activeModelTypicalTemplates);
81 if(result == null) return;
83 session.markUndoPoint();
84 SyncTypicalTemplatesToInstances req = new SyncTypicalTemplatesToInstances(result.selectedRules, activeModelTypicalTemplates).logging(result.logging);
85 session.syncRequest(req);
87 for(Resource log : req.logs)
88 new OpenPlainTextDocumentAdapter().openEditor(log);
92 MessageDialog.openInformation(shell, "No Typical Templates", "The currently active model doesn't contain any typical diagram templates.");
94 } catch (Exception e) {
95 ErrorLogger.defaultLogError(e);