]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/ChangeMappingTypeHandler.java
Enhancements to district functionalities and code
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / contributions / ChangeMappingTypeHandler.java
diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/ChangeMappingTypeHandler.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/ChangeMappingTypeHandler.java
new file mode 100644 (file)
index 0000000..2f3aae4
--- /dev/null
@@ -0,0 +1,190 @@
+package org.simantics.district.network.ui.contributions;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.inject.Named;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.e4.core.di.annotations.CanExecute;
+import org.eclipse.e4.core.di.annotations.Execute;
+import org.eclipse.e4.ui.services.IServiceConstants;
+import org.eclipse.jface.dialogs.Dialog;
+import org.eclipse.jface.layout.GridDataFactory;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Combo;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Group;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.dialogs.SelectionStatusDialog;
+import org.simantics.DatabaseJob;
+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.ReadRequest;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.SelectionHints;
+import org.simantics.db.request.Read;
+import org.simantics.district.network.ontology.DistrictNetworkResource;
+import org.simantics.district.network.ui.function.Functions;
+import org.simantics.district.network.ui.internal.Activator;
+import org.simantics.layer0.Layer0;
+import org.simantics.utils.ui.ISelectionUtils;
+
+public class ChangeMappingTypeHandler {
+
+    
+    @CanExecute
+    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) {
+        List<Resource> elements = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
+        if (elements.size() < 1)
+            return false;
+        try {
+            return Simantics.getSession().syncRequest(new Read<Boolean>() {
+
+                @Override
+                public Boolean perform(ReadGraph graph) throws DatabaseException {
+                    Layer0 L0 = Layer0.getInstance(graph);
+                    Resource instanceOf = null;
+                    for (Resource element : elements) {
+                        if (instanceOf == null) {
+                            instanceOf = graph.getSingleObject(element, L0.InstanceOf);
+                        } else {
+                            Resource currentInstanceOf = graph.getSingleObject(element, L0.InstanceOf);
+                            if (!currentInstanceOf.equals(instanceOf)) {
+                                return false;
+                            }
+                        }
+                    }
+                    return true;
+                }
+            });
+        } catch (DatabaseException e) {
+            e.printStackTrace();
+            return false;
+        }
+    }
+    
+    @Execute
+    public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) Object selection) {
+        final List<Resource> elements = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
+        if (elements.size() < 1)
+            return;
+        
+        SelectMappingDialog dialog = new SelectMappingDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), elements);
+        if (dialog.open() != Dialog.OK)
+            return;
+        
+        Resource mapping = dialog.getDefaultVertexMapping();
+        Job job = new DatabaseJob("Join selected vertices") {
+            
+            @Override
+            protected IStatus run(IProgressMonitor monitor) {
+                try {
+                    Simantics.getSession().syncRequest(new WriteRequest() {
+                        
+                        @Override
+                        public void perform(WriteGraph graph) throws DatabaseException {
+                            DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+                            for (Resource element : elements) {
+                                graph.deny(element, DN.HasMapping);
+                                graph.claim(element, DN.HasMapping, mapping);
+                            }
+                        }
+                    });
+                } catch (DatabaseException e) {
+                    return new Status(IStatus.ERROR, Activator.PLUGIN_ID, getName() + " failed.", e);
+                }
+                return Status.OK_STATUS;
+            }
+        };
+        job.setUser(true);
+        job.schedule();
+    }
+    
+    private static class SelectMappingDialog extends SelectionStatusDialog {
+
+        private Combo vertexMappingCombo;
+        
+        private Composite composite;
+        
+        private List<Resource> elements;
+        private Map<String, Resource> vertexMappings = new HashMap<>();
+        
+        private Resource defaultVertexMapping;
+
+        protected SelectMappingDialog(Shell parentShell, List<Resource> elements) {
+            super(parentShell);
+            this.elements = elements;
+            setTitle("Select mappings for new DN diagram");
+        }
+
+        public Resource getDefaultVertexMapping() {
+            return defaultVertexMapping;
+        }
+
+        @Override
+        protected Control createDialogArea(Composite parent) {
+            composite = (Composite) super.createDialogArea(parent);
+            
+            createMappingsGroup(composite);
+            
+            // compute default values
+            Simantics.getSession().asyncRequest(new ReadRequest() {
+
+                @Override
+                public void run(ReadGraph graph) throws DatabaseException {
+                    DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+                    Resource element = elements.get(0);
+                    if (graph.isInstanceOf(element, DN.Vertex)) {
+                        vertexMappings = Functions.getVertexMappings(graph, element);
+                    } else if (graph.isInstanceOf(element, DN.Edge)) {
+                        vertexMappings = Functions.getEdgeMappings(graph, element);
+                    }
+                    composite.getDisplay().asyncExec(() -> {
+                        vertexMappingCombo.setItems(vertexMappings.keySet().toArray(new String[vertexMappings.size()]));
+                        vertexMappingCombo.select(0);
+                    }); 
+                    
+                }
+            });
+            return composite;
+        }
+
+        @Override
+        protected void computeResult() {
+            defaultVertexMapping = vertexMappings.get(vertexMappingCombo.getItem(vertexMappingCombo.getSelectionIndex()));
+        }
+        
+        private void createMappingsGroup(Composite parent) {
+            Group group= new Group(parent, SWT.NONE);
+            group.setFont(parent.getFont());
+            group.setText("Default mappings");
+            GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
+            group.setLayout(new GridLayout(1, false));
+            
+            Composite cmposite = new Composite(group, SWT.NONE);
+            cmposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
+            cmposite.setLayout(new GridLayout(2, false));
+            
+            Label vertexMappingLabel = new Label(cmposite, SWT.NONE);
+            vertexMappingLabel.setText("Default vertex mapping");
+
+            vertexMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
+            GridDataFactory.fillDefaults().grab(true, false).applyTo(vertexMappingCombo);
+            
+        }
+    }
+}