From f43fbe51d130e91cc05e5cd6632f33456b6b94c7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Hannu=20Niemist=C3=B6?= Date: Sun, 27 Aug 2017 19:10:02 +0300 Subject: [PATCH] (refs #7453) Switch component type of the component This change adds ontology definitions for declaring a group of similar component types and an action to switch between them. Change-Id: I15f26d97e9eec81ff9ff1ec65e5f6be1782ba24b --- .../graph/Switching.pgraph | 20 ++ bundles/org.simantics.modeling.ui/plugin.xml | 4 + .../SwitchComponentTypeContribution.java | 171 ++++++++++++++++++ .../org/simantics/modeling/MigrateModel.java | 6 + 4 files changed, 201 insertions(+) create mode 100644 bundles/org.simantics.modeling.ontology/graph/Switching.pgraph create mode 100644 bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/SwitchComponentTypeContribution.java diff --git a/bundles/org.simantics.modeling.ontology/graph/Switching.pgraph b/bundles/org.simantics.modeling.ontology/graph/Switching.pgraph new file mode 100644 index 000000000..0382e25f1 --- /dev/null +++ b/bundles/org.simantics.modeling.ontology/graph/Switching.pgraph @@ -0,0 +1,20 @@ +L0 = +STR = +MOD = + +MOD.ComponentSwitchGroup -- MOD.ComponentSwitchGroup.alternative --> MOD.ComponentSwitchAlternative -- MOD.ComponentSwitchAlternative.componentType --> STR.ComponentType + + diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/SwitchComponentTypeContribution.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/SwitchComponentTypeContribution.java new file mode 100644 index 000000000..61e81194e --- /dev/null +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/SwitchComponentTypeContribution.java @@ -0,0 +1,171 @@ +package org.simantics.modeling.ui.actions; + + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.eclipse.jface.action.ContributionItem; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.widgets.Menu; +import org.eclipse.swt.widgets.MenuItem; +import org.simantics.Simantics; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.UnaryRead; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; +import org.simantics.modeling.MigrateModel; +import org.simantics.modeling.ModelingResources; +import org.simantics.structural.stubs.StructuralResource2; +import org.simantics.ui.utils.ResourceAdaptionUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SwitchComponentTypeContribution extends ContributionItem { + private static final Logger LOGGER = LoggerFactory.getLogger(SwitchComponentTypeContribution.class); + + private static class SwitchGroup implements Comparable { + final String label; + final ArrayList alternatives = new ArrayList(); + + public SwitchGroup(String label) { + this.label = label; + } + + @Override + public int compareTo(SwitchGroup o) { + return label.compareTo(o.label); + } + } + + private static class Alternative implements Comparable { + final String label; + final Resource componentType; + final boolean isCurrent; + + public Alternative(String label, Resource componentType, boolean isCurrent) { + this.label = label; + this.componentType = componentType; + this.isCurrent = isCurrent; + } + + @Override + public int compareTo(Alternative o) { + return label.compareTo(o.label); + } + } + + @Override + public void fill(Menu menu, int index) { + Resource resource = ResourceAdaptionUtils.toSingleWorkbenchResource(); + if(resource == null) + return; + + List groups; + try { + groups = Simantics.getSession().syncRequest(new ComponentSwitchGroupQuery(resource)); + } catch (DatabaseException e) { + LOGGER.error("Retrieval of switch groups failed.", e); + return; + } + + if(!groups.isEmpty()) { + SelectionAdapter switchAction = new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + Resource newComponentType = (Resource)e.widget.getData(); + Simantics.getSession().asyncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + MigrateModel.changeComponentType(graph, elementToComponent(graph, resource), newComponentType); + } + }); + } + }; + for(SwitchGroup group : groups) { + MenuItem item = new MenuItem(menu, SWT.CASCADE); + item.setText(group.label); + + Menu subMenu = new Menu(menu); + item.setMenu(subMenu); + + for(Alternative alternative : group.alternatives) { + MenuItem subItem = new MenuItem(subMenu, SWT.PUSH); + subItem.setText(alternative.label); + subItem.setData(alternative.componentType); + if(alternative.isCurrent) + subItem.setEnabled(false); + else + subItem.addSelectionListener(switchAction); + } + } + } + } + + private static class ComponentSwitchGroupQuery extends UnaryRead> { + public ComponentSwitchGroupQuery(Resource parameter) { + super(parameter); + } + + @Override + public List perform(ReadGraph graph) throws DatabaseException { + StructuralResource2 STR = StructuralResource2.getInstance(graph); + ModelingResources MOD = ModelingResources.getInstance(graph); + Resource component = elementToComponent(graph, parameter); + + Resource componentType = graph.getPossibleType(component, STR.Component); + if(componentType == null) + return Collections.emptyList(); + + Layer0 L0 = Layer0.getInstance(graph); + ArrayList result = new ArrayList(); + for(Resource myAlt : graph.getObjects(componentType, MOD.BelongsToComponentSwitchGroup)) { + Resource group = graph.getSingleObject(myAlt, MOD.ComponentSwitchGroup_alternative_Inverse); + + SwitchGroup groupObj; + { + String label = graph.getPossibleRelatedValue(group, L0.HasLabel); + if(label == null) { + label = graph.getPossibleRelatedValue(group, L0.HasName); + if(label == null) + label = "Alternative types"; + } + groupObj = new SwitchGroup(label); + } + + for(Resource alt : graph.getObjects(group, MOD.ComponentSwitchGroup_alternative)) { + Resource altComponentType = graph.getSingleObject(alt, MOD.ComponentSwitchAlternative_componentType); + String label = graph.getPossibleRelatedValue(alt, L0.HasLabel); + if(label == null) { + label = graph.getPossibleRelatedValue(alt, L0.HasName); + if(label == null) { + label = graph.getPossibleRelatedValue(altComponentType, L0.HasLabel); + if(label == null) + label = graph.getRelatedValue(altComponentType, L0.HasName); + } + } + groupObj.alternatives.add(new Alternative(label, altComponentType, altComponentType.equals(componentType))); + } + Collections.sort(groupObj.alternatives); + result.add(groupObj); + } + Collections.sort(result); + return result; + } + } + + private static Resource elementToComponent(ReadGraph graph, Resource element) throws DatabaseException { + ModelingResources MOD = ModelingResources.getInstance(graph); + Resource component = graph.getPossibleObject(element, MOD.ElementToComponent); + if(component != null) + return component; + else + return element; + } + +} diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/MigrateModel.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/MigrateModel.java index 16b8ae92a..dee74b0e1 100644 --- a/bundles/org.simantics.modeling/src/org/simantics/modeling/MigrateModel.java +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/MigrateModel.java @@ -216,4 +216,10 @@ public class MigrateModel { public List>> instances = new ArrayList<>(); public List sortedShownInstances = Collections.emptyList(); + public static void changeComponentType(WriteGraph graph, Resource instance, Resource newComponentType) throws DatabaseException { + ModelingResources MOD = ModelingResources.getInstance(graph); + Resource newSymbol = graph.getSingleObject(newComponentType, MOD.ComponentTypeToSymbol); + new MigrationOperation(new NamedResource("", instance), new NamedResource("", newComponentType), new NamedResource("", newSymbol)) + .perform(graph); + } } -- 2.43.2