X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2Ftypicals%2FSyncActiveModelTypicals.java;h=1f1cc7c0fe0df97df603fff429417cef4090cb3e;hb=0475b9e2a9331ef8f3dcd61567e5d071ae8ef561;hp=441702dd564ad375c92d5e851f0977d8ad13db8a;hpb=a5e69e900dcfcf54a9f39ab754f20467d99b64d3;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/typicals/SyncActiveModelTypicals.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/typicals/SyncActiveModelTypicals.java index 441702dd5..1f1cc7c0f 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/typicals/SyncActiveModelTypicals.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/typicals/SyncActiveModelTypicals.java @@ -1,100 +1,100 @@ -package org.simantics.modeling.ui.typicals; - -import java.util.Collection; -import java.util.HashSet; -import java.util.Set; - -import org.eclipse.core.commands.AbstractHandler; -import org.eclipse.core.commands.ExecutionEvent; -import org.eclipse.core.commands.ExecutionException; -import org.eclipse.jface.dialogs.MessageDialog; -import org.eclipse.swt.widgets.Shell; -import org.eclipse.ui.handlers.HandlerUtil; -import org.simantics.Simantics; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.Session; -import org.simantics.db.common.request.ObjectsWithType; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.adapter.Instances; -import org.simantics.db.request.Read; -import org.simantics.modeling.ModelingResources; -import org.simantics.modeling.typicals.SyncTypicalTemplatesToInstances; -import org.simantics.modeling.ui.documents.OpenPlainTextDocumentAdapter; -import org.simantics.operation.Layer0X; -import org.simantics.simulation.ontology.SimulationResource; -import org.simantics.utils.ui.ErrorLogger; - -/** - * @author Tuukka Lehtonen - */ -public class SyncActiveModelTypicals extends AbstractHandler { - - @Override - public Object execute(ExecutionEvent event) throws ExecutionException { - Shell shell = HandlerUtil.getActiveShell(event); - Session session = Simantics.getSession(); - try { - Resource[] activeModelTypicalTemplates = session.syncRequest(new Read() { - @Override - public Resource[] perform(ReadGraph graph) throws DatabaseException { - Layer0X L0X = Layer0X.getInstance(graph); - ModelingResources MOD = ModelingResources.getInstance(graph); - SimulationResource SIMU = SimulationResource.getInstance(graph); - - Collection activeModels = graph.syncRequest(new ObjectsWithType(Simantics.getProjectResource(), L0X.Activates, SIMU.Model)); - Instances query = graph.adapt(MOD.MasterTypicalCompositeType, Instances.class); - - Set result = new HashSet(); - for (Resource activeModel : activeModels) { - Collection typicalComposites = query.find(graph, activeModel); - for (Resource typicalComposite : typicalComposites) { - for (Resource typicalDiagram : graph.getObjects(typicalComposite, MOD.CompositeToDiagram)) { - result.add(typicalDiagram); - } - } - } - return result.toArray(Resource.NONE); - } - }); - - if (activeModelTypicalTemplates.length > 0) { -// StringBuilder msg = new StringBuilder("Are you sure you want to synchronize ") -// .append(activeModelTypicalTemplates.length) -// .append(" typical template"); -// if (activeModelTypicalTemplates.length > 1) -// msg.append("s to all their instances?"); -// else -// msg.append(" to all its instances?"); -// if (!MessageDialog.openConfirm(shell, "Synchronize All Typical Templates with Instances", msg.toString())) -// return null; - - StringBuilder msg = new StringBuilder("Synchronizing ") - .append(activeModelTypicalTemplates.length) - .append(" typical template"); - if (activeModelTypicalTemplates.length > 1) - msg.append("s to all their instances."); - else - msg.append(" to all its instances."); - - RuleChooserDialog.RuleResult result = RuleChooserDialog.choose(shell, msg.toString(), activeModelTypicalTemplates); - if(result == null) return null; - - session.markUndoPoint(); - SyncTypicalTemplatesToInstances req = new SyncTypicalTemplatesToInstances(result.selectedRules, activeModelTypicalTemplates).logging(result.logging); - session.syncRequest(req); - if (result.logging) { - for(Resource log : req.logs) - new OpenPlainTextDocumentAdapter().openEditor(log); - } - - } else { - MessageDialog.openInformation(shell, "No Typical Templates", "The currently active model doesn't contain any typical diagram templates."); - } - } catch (Exception e) { - ErrorLogger.defaultLogError(e); - } - return null; - } - -} +package org.simantics.modeling.ui.typicals; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.handlers.HandlerUtil; +import org.simantics.Simantics; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.adapter.Instances; +import org.simantics.db.request.Read; +import org.simantics.modeling.ModelingResources; +import org.simantics.modeling.typicals.SyncTypicalTemplatesToInstances; +import org.simantics.modeling.ui.documents.OpenPlainTextDocumentAdapter; +import org.simantics.operation.Layer0X; +import org.simantics.simulation.ontology.SimulationResource; +import org.simantics.utils.ui.ErrorLogger; + +/** + * @author Tuukka Lehtonen + */ +public class SyncActiveModelTypicals extends AbstractHandler { + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + Shell shell = HandlerUtil.getActiveShell(event); + Session session = Simantics.getSession(); + try { + Resource[] activeModelTypicalTemplates = session.syncRequest(new Read() { + @Override + public Resource[] perform(ReadGraph graph) throws DatabaseException { + Layer0X L0X = Layer0X.getInstance(graph); + ModelingResources MOD = ModelingResources.getInstance(graph); + SimulationResource SIMU = SimulationResource.getInstance(graph); + + Collection activeModels = graph.syncRequest(new ObjectsWithType(Simantics.getProjectResource(), L0X.Activates, SIMU.Model)); + Instances query = graph.adapt(MOD.MasterTypicalCompositeType, Instances.class); + + Set result = new HashSet(); + for (Resource activeModel : activeModels) { + Collection typicalComposites = query.find(graph, activeModel); + for (Resource typicalComposite : typicalComposites) { + for (Resource typicalDiagram : graph.getObjects(typicalComposite, MOD.CompositeToDiagram)) { + result.add(typicalDiagram); + } + } + } + return result.toArray(Resource.NONE); + } + }); + + if (activeModelTypicalTemplates.length > 0) { +// StringBuilder msg = new StringBuilder("Are you sure you want to synchronize ") +// .append(activeModelTypicalTemplates.length) +// .append(" typical template"); +// if (activeModelTypicalTemplates.length > 1) +// msg.append("s to all their instances?"); +// else +// msg.append(" to all its instances?"); +// if (!MessageDialog.openConfirm(shell, "Synchronize All Typical Templates with Instances", msg.toString())) +// return null; + + StringBuilder msg = new StringBuilder("Synchronizing ") + .append(activeModelTypicalTemplates.length) + .append(" typical template"); + if (activeModelTypicalTemplates.length > 1) + msg.append("s to all their instances."); + else + msg.append(" to all its instances."); + + RuleChooserDialog.RuleResult result = RuleChooserDialog.choose(shell, msg.toString(), activeModelTypicalTemplates); + if(result == null) return null; + + session.markUndoPoint(); + SyncTypicalTemplatesToInstances req = new SyncTypicalTemplatesToInstances(result.selectedRules, activeModelTypicalTemplates).logging(result.logging); + session.syncRequest(req); + if (result.logging) { + for(Resource log : req.logs) + new OpenPlainTextDocumentAdapter().openEditor(log); + } + + } else { + MessageDialog.openInformation(shell, "No Typical Templates", "The currently active model doesn't contain any typical diagram templates."); + } + } catch (Exception e) { + ErrorLogger.defaultLogError(e); + } + return null; + } + +}