]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
(refs #7453) Switch component type of the component 93/893/1
authorHannu Niemistö <hannu.niemisto@semantum.fi>
Sun, 27 Aug 2017 16:10:02 +0000 (19:10 +0300)
committerHannu Niemistö <hannu.niemisto@semantum.fi>
Sun, 27 Aug 2017 16:10:02 +0000 (19:10 +0300)
This change adds ontology definitions for declaring a group of similar
component types and an action to switch between them.

Change-Id: I15f26d97e9eec81ff9ff1ec65e5f6be1782ba24b

bundles/org.simantics.modeling.ontology/graph/Switching.pgraph [new file with mode: 0644]
bundles/org.simantics.modeling.ui/plugin.xml
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/SwitchComponentTypeContribution.java [new file with mode: 0644]
bundles/org.simantics.modeling/src/org/simantics/modeling/MigrateModel.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 (file)
index 0000000..0382e25
--- /dev/null
@@ -0,0 +1,20 @@
+L0 = <http://www.simantics.org/Layer0-1.1>
+STR = <http://www.simantics.org/Structural-1.2>
+MOD = <http://www.simantics.org/Modeling-1.2>
+
+MOD.ComponentSwitchGroup <T L0.Entity
+    // L0.HasLabel
+    >-- MOD.ComponentSwitchGroup.alternative --> MOD.ComponentSwitchAlternative <R L0.IsComposedOf
+
+MOD.ComponentSwitchAlternative <T L0.Entity
+    // L0.HasLabel
+    >-- MOD.ComponentSwitchAlternative.componentType --> STR.ComponentType <R L0.IsRelatedTo : L0.TotalFunction
+        L0.InverseOf MOD.BelongsToComponentSwitchGroup <R L0.IsRelatedTo
+
+MOD.ComponentSwitchAlternative.withLabel : L0.Template 
+    @template %group %label %componentType
+        %group
+            MOD.ComponentSwitchGroup.alternative _ : MOD.ComponentSwitchAlternative
+                L0.HasLabel %label
+                MOD.ComponentSwitchAlternative.componentType %componentType
+            
\ No newline at end of file
index 7a29cd7864f0f8a1c4f0b00861ea2a25715acfb3..2612bb742fc4b54dea1b1c1d175d76923e07814d 100644 (file)
                class="org.simantics.diagram.handler.ConnectionRoutingMenuContribution"
                id="connectionRouting">
          </dynamic>
+         <dynamic
+               class="org.simantics.modeling.ui.actions.SwitchComponentTypeContribution"
+               id="org.simantics.modeling.ui.switchComponentType">
+         </dynamic>
       </menuContribution>
       <menuContribution
             locationURI="toolbar:org.simantics.browsing.ui.graph.propertyView">
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 (file)
index 0000000..61e8119
--- /dev/null
@@ -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<SwitchGroup> {
+        final String label;
+        final ArrayList<Alternative> alternatives = new ArrayList<Alternative>();
+        
+        public SwitchGroup(String label) {
+            this.label = label;
+        }
+
+        @Override
+        public int compareTo(SwitchGroup o) {
+            return label.compareTo(o.label);
+        }
+    }
+    
+    private static class Alternative implements Comparable<Alternative> {
+        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<SwitchGroup> 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<Resource, List<SwitchGroup>> {
+        public ComponentSwitchGroupQuery(Resource parameter) {
+            super(parameter);
+        }
+
+        @Override
+        public List<SwitchGroup> 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<SwitchGroup> result = new ArrayList<SwitchGroup>();
+            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;
+    }
+    
+}
index 16b8ae92aeca27e2e407f3e0bf911a5fc7c4e2ba..dee74b0e1d1b98bf9d499d48c84a0b43650774b3 100644 (file)
@@ -216,4 +216,10 @@ public class MigrateModel {
        public List<Triple<String, NamedResource, Collection<MigrationOperation>>> instances = new ArrayList<>();
        public List<MigrationOperation> 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);
+       }
 }