X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.network.ui%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Fui%2Ffunction%2FFunctions.java;h=eafb6f380088b87a5cefcf29772a0c0f46ff11a4;hb=a995d5d46f03f25bd77fb3f6d8349839886b2ec2;hp=cc9b336f69a796680b8fc8bd1c972900f9d35ece;hpb=04d29917ceb4e34d2d9fc32ac4c7c8bdd2f4a732;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/function/Functions.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/function/Functions.java index cc9b336f..eafb6f38 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/function/Functions.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/function/Functions.java @@ -12,20 +12,7 @@ import java.util.Set; import java.util.stream.Collectors; import org.eclipse.jface.dialogs.Dialog; -import org.eclipse.jface.layout.GridDataFactory; -import org.eclipse.swt.SWT; -import org.eclipse.swt.events.SelectionAdapter; -import org.eclipse.swt.events.SelectionEvent; -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.NameLabelUtil; import org.simantics.Simantics; import org.simantics.browsing.ui.common.modifiers.EnumeratedValue; @@ -35,26 +22,21 @@ import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Session; -import org.simantics.db.WriteGraph; import org.simantics.db.common.request.IndexRoot; import org.simantics.db.common.request.ObjectsWithType; -import org.simantics.db.common.request.ReadRequest; -import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.DatabaseException; import org.simantics.db.exception.RuntimeDatabaseException; -import org.simantics.db.exception.ServiceException; import org.simantics.db.layer0.QueryIndexUtils; import org.simantics.db.layer0.util.Layer0Utils; import org.simantics.db.layer0.variable.Variable; import org.simantics.db.layer0.variable.Variables; import org.simantics.db.layer0.variable.Variables.Role; import org.simantics.db.procedure.Procedure; +import org.simantics.db.request.WriteResult; +import org.simantics.district.network.DistrictNetworkUtil; import org.simantics.district.network.ontology.DistrictNetworkResource; import org.simantics.layer0.Layer0; -import org.simantics.modeling.ModelingResources; -import org.simantics.modeling.adapters.NewCompositeActionFactory; import org.simantics.modeling.typicals.TypicalUtil; -import org.simantics.operation.Layer0X; import org.simantics.scl.compiler.commands.CommandSession; import org.simantics.scl.compiler.commands.CommandSessionImportEntry; import org.simantics.scl.compiler.errors.CompilationError; @@ -93,14 +75,25 @@ public class Functions { @SCLValue(type = "ReadGraph -> Resource -> Variable -> b") public static Object defaultVertexMappingModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException { - System.out.println(graph.getURI(resource)); - System.out.println(context.getURI(graph)); - Resource diagram = resolveElement(graph, context); DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); return baseMappingModifier(graph, diagram, DN.VertexDefaultMapping, DN.Mapping_VertexMapping, context); } - + + @SCLValue(type = "ReadGraph -> Resource -> Variable -> b") + public static Object rightClickVertexMappingModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException { + Resource diagram = resolveElement(graph, context); + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + return baseMappingModifier(graph, diagram, DN.RightClickDefaultMapping, DN.Mapping_VertexMapping, context); + } + + @SCLValue(type = "ReadGraph -> Resource -> Variable -> b") + public static Object leftClickVertexMappingModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException { + Resource diagram = resolveElement(graph, context); + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + return baseMappingModifier(graph, diagram, DN.LeftClickDefaultMapping, DN.Mapping_VertexMapping, context); + } + @SCLValue(type = "ReadGraph -> Resource -> Variable -> b") public static Object mappingModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException { @@ -109,24 +102,23 @@ public class Functions { return baseMappingModifier(graph, element, DistrictNetworkResource.getInstance(graph).HasMapping, mappingType, context); } - public static Map getVertexMappings(ReadGraph graph, Resource resource) throws DatabaseException { - Map second = getNetworkMappingsByType(graph, resource , DistrictNetworkResource.getInstance(graph).Mapping_VertexMapping); + public static Map getVertexMappings(ReadGraph graph, Resource indexRoot) throws DatabaseException { + Map second = getNetworkMappingsByType(graph, indexRoot, DistrictNetworkResource.getInstance(graph).Mapping_VertexMapping); return second; } - public static Map getEdgeMappings(ReadGraph graph, Resource resource) throws DatabaseException { - Map second = getNetworkMappingsByType(graph, resource , DistrictNetworkResource.getInstance(graph).Mapping_EdgeMapping); + public static Map getEdgeMappings(ReadGraph graph, Resource indexRoot) throws DatabaseException { + Map second = getNetworkMappingsByType(graph, indexRoot, DistrictNetworkResource.getInstance(graph).Mapping_EdgeMapping); return second; } public static Map getCRSs(ReadGraph graph, Resource resource) throws DatabaseException { - Map result = getNetworkMappingsByType(graph, resource, DistrictNetworkResource.getInstance(graph).SpatialRefSystem); + Map result = getNetworkMappingsByType(graph, graph.sync(new IndexRoot(resource)), DistrictNetworkResource.getInstance(graph).SpatialRefSystem); return result; } - public static Map getNetworkMappingsByType(ReadGraph graph, Resource element, Resource mappingType) throws DatabaseException { - Resource indexRoot = graph.sync(new IndexRoot(element)); + public static Map getNetworkMappingsByType(ReadGraph graph, Resource indexRoot, Resource mappingType) throws DatabaseException { List mappings = QueryIndexUtils.searchByType(graph, indexRoot, mappingType); Map result = new HashMap<>(mappings.size()); Layer0 L0 = Layer0.getInstance(graph); @@ -183,9 +175,7 @@ public class Functions { @SCLValue(type = "ReadGraph -> Resource -> a -> b") public static Object enumerationValues(ReadGraph graph, Resource resource, Object context) throws DatabaseException { - Variable var = (Variable) context; - System.out.println(graph.getURI(resource)); - System.out.println(var.getURI(graph)); + //Variable var = (Variable) context; return Collections.emptyList(); } @@ -201,7 +191,6 @@ public class Functions { @SCLValue(type = "Resource -> String -> Resource -> Resource") public static Resource compositeInstantiator(final Resource compositeType, final String defaultName, final Resource target) throws DatabaseException { - return TypicalUtil.syncExec(procedure -> { if (!SWTUtils.asyncExec(PlatformUI.getWorkbench().getDisplay(), () -> { try { @@ -215,209 +204,7 @@ public class Functions { }); } - private static class DefaultMappingsDialog extends SelectionStatusDialog { - - private Combo vertexMappingCombo; - private Combo edgeMappingCombo; - private Combo crsCombo; - private Composite composite; - - private Resource configuration; - private Map vertexMappings = new HashMap<>(); - private Map edgeMappings = new HashMap<>(); - private Map composites = new HashMap<>(); - private Map crss = new HashMap<>(); - - private Resource defaultVertexMapping; - private Resource defaultEdgeMapping; - private Resource defaultCRS; - - private Combo compositeMappingCombo; - private Combo componentMappingCombo; - - protected DefaultMappingsDialog(Shell parentShell, Resource configuration) { - super(parentShell); - this.configuration = configuration; - setTitle("Select mappings for new DN diagram"); - } - - public Resource getDefaultVertexMapping() { - return defaultVertexMapping; - } - - public Resource getDefaultEdgeMapping() { - return defaultEdgeMapping; - } - - @Override - protected Control createDialogArea(Composite parent) { - composite = (Composite) super.createDialogArea(parent); - - createMappingsGroup(composite); - createExistingCompositeGroup(composite); - createCRSSettingsGroup(composite); - - // compute default values - Simantics.getSession().asyncRequest(new ReadRequest() { - - @Override - public void run(ReadGraph graph) throws DatabaseException { - - vertexMappings = getVertexMappings(graph, configuration); - edgeMappings = getEdgeMappings(graph, configuration); - - composites = getComposites(graph, configuration); - - crss = getCRSs(graph, configuration); - - composite.getDisplay().asyncExec(() -> { - - vertexMappingCombo.setItems(vertexMappings.keySet().toArray(new String[vertexMappings.size()])); - edgeMappingCombo.setItems(edgeMappings.keySet().toArray(new String[edgeMappings.size()])); - - crsCombo.setItems(crss.keySet().toArray(new String[crss.size()])); - - compositeMappingCombo.setItems(composites.keySet().toArray(new String[composites.size()])); - vertexMappingCombo.select(0); - edgeMappingCombo.select(0); - - crsCombo.select(0); - - if (!composites.isEmpty()) - compositeMappingCombo.select(0); - }); - - } - }); - return composite; - } - - protected Map getComposites(ReadGraph graph, Resource element) throws DatabaseException { - List nonDistrictComposites = composites.values().stream().filter(comp -> { - try { - return !graph.isInstanceOf(comp, DistrictNetworkResource.getInstance(graph).Composite); - } catch (ServiceException e1) { - LOGGER.error("Could not check if composite " + comp + " is instanceOf DistrictNetwork.composite"); - return false; - } - }).collect(Collectors.toList()); - Map result = new HashMap<>(nonDistrictComposites.size()); - Layer0 L0 = Layer0.getInstance(graph); - nonDistrictComposites.forEach(mapping -> { - try { - String name = graph.getRelatedValue2(mapping, L0.HasName); - result.put(name, mapping); - } catch (DatabaseException e) { - LOGGER.error("Could not read name of " + mapping, e); - } - }); - return result; - } - - 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); - - Label edgeMappingLabel = new Label(cmposite, SWT.NONE); - edgeMappingLabel.setText("Default edge mapping"); - - edgeMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER); - GridDataFactory.fillDefaults().grab(true, false).applyTo(edgeMappingCombo); - } - - private void createExistingCompositeGroup(Composite parent) { - Group group= new Group(parent, SWT.NONE); - group.setFont(parent.getFont()); - group.setText("Mapped composite"); - 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 compositeMappingLabel = new Label(cmposite, SWT.NONE); - compositeMappingLabel.setText("Select composite"); - - compositeMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER); - GridDataFactory.fillDefaults().grab(true, false).applyTo(compositeMappingCombo); - compositeMappingCombo.addSelectionListener(new SelectionAdapter() { - - @Override - public void widgetSelected(SelectionEvent e) { - super.widgetSelected(e); - recalculateMappapleComponents(); - } - }); - - Label compojnentMappingLabel = new Label(cmposite, SWT.NONE); - compojnentMappingLabel.setText("Select component"); - - componentMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER); - GridDataFactory.fillDefaults().grab(true, false).applyTo(componentMappingCombo); - } - - protected void recalculateMappapleComponents() { - Simantics.getSession().asyncRequest(new ReadRequest() { - - @Override - public void run(ReadGraph graph) throws DatabaseException { - - - composite.getDisplay().asyncExec(() -> { - - }); - } - }); - } - - private void createCRSSettingsGroup(Composite parent) { - Group group= new Group(parent, SWT.NONE); - group.setFont(parent.getFont()); - group.setText("CRS settings"); - 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 CRS"); - - crsCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER); - GridData textData = new GridData(SWT.FILL, SWT.CENTER, true, false); - crsCombo.setLayoutData(textData); - } - - - @Override - protected void computeResult() { - defaultVertexMapping = vertexMappings.get(vertexMappingCombo.getItem(vertexMappingCombo.getSelectionIndex())); - defaultEdgeMapping = edgeMappings.get(edgeMappingCombo.getItem(edgeMappingCombo.getSelectionIndex())); - defaultCRS = crss.get(crsCombo.getItem(crsCombo.getSelectionIndex())); - } - - public Resource getCRS() { - return defaultCRS; - } - - } - - private static void queryInitialValuesAndCreateComposite(final Resource compositeType, final Resource target, + public static void queryInitialValuesAndCreateComposite(final Resource compositeType, final Resource target, String defaultName, final Procedure procedure) { DefaultMappingsDialog dialog = new DefaultMappingsDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), target); @@ -425,36 +212,29 @@ public class Functions { procedure.execute(null); return; } - Simantics.getSession().asyncRequest( - NewCompositeActionFactory.createCompositeRequest(target, defaultName, compositeType), - new Procedure() { - @Override - public void execute(Resource composite) { - Simantics.getSession().asyncRequest(new WriteRequest() { - - @Override - public void perform(WriteGraph graph) throws DatabaseException { - DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); - Resource diagram = graph.getSingleObject(composite, ModelingResources.getInstance(graph).CompositeToDiagram); - graph.claim(diagram, DN.EdgeDefaultMapping, dialog.getDefaultEdgeMapping()); - graph.claim(diagram, DN.VertexDefaultMapping, dialog.getDefaultVertexMapping()); - graph.claim(diagram, DN.HasSpatialRefSystem, dialog.getCRS()); - - // Generated name prefix from composite name - String compositeName = graph.getRelatedValue2(composite, Layer0.getInstance(graph).HasName, Bindings.STRING); - graph.claimLiteral(diagram, Layer0X.getInstance(graph).HasGeneratedNamePrefix, "N" + compositeName.substring(compositeName.length() - 1, compositeName.length())); - } - }); - DefaultActions.asyncPerformDefaultAction(Simantics.getSession(), composite, false, false, true); - procedure.execute(composite); - } - - @Override - public void exception(Throwable t) { - LOGGER.error("Failed to create composite, see exception for details.", t); - procedure.exception(t); - } - }); + + Simantics.getSession().asyncRequest((WriteResult) graph -> { + return DistrictNetworkUtil.createNetworkDiagram(graph, target, compositeType, defaultName, + dialog.getDefaultEdgeMapping(), + dialog.getDefaultVertexMapping(), + dialog.getRightClickVertexMapping(), + dialog.getLeftClickVertexMapping(), + dialog.getCRS() + ); + }, new Procedure() { + + @Override + public void execute(Resource composite) { + DefaultActions.asyncPerformDefaultAction(Simantics.getSession(), composite, false, false, true); + procedure.execute(composite); + } + + @Override + public void exception(Throwable t) { + LOGGER.error("Failed to create composite, see exception for details.", t); + procedure.exception(t); + } + }); } public static Collection getDistrictDiagrams(ReadGraph graph) throws DatabaseException { @@ -469,42 +249,6 @@ public class Functions { return results; } - private static List listInstanceNames(ReadGraph graph, Variable context, Resource type) throws DatabaseException { - Resource indexRoot = Variables.getIndexRoot(graph, context); - DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); - List properties = QueryIndexUtils.searchByType(graph, indexRoot, DN.Vertex_ScaleProperty); - return properties.stream() - .map(m -> createEnumeratedValue(graph, m)) - .map(EnumeratedValue::getName) - .collect(Collectors.toList()); - } - - @SCLValue(type = "ReadGraph -> Resource -> Variable -> b") - public static Object edgeThicknessPropertyEnumerationValues(ReadGraph graph, Resource resource, Variable context) throws DatabaseException { - DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); - return listInstanceNames(graph, context, DN.Edge_ThicknessProperty); - } - - @SCLValue(type = "ReadGraph -> Resource -> Variable -> b") - public static Object nodeScalePropertyEnumerationValues(ReadGraph graph, Resource resource, Variable context) throws DatabaseException { - DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); - return listInstanceNames(graph, context, DN.Vertex_ScaleProperty); - } - - @SCLValue(type = "ReadGraph -> Resource -> Variable -> b") - public static Object edgeThicknessPropertyModifier(ReadGraph graph, Resource resource, Variable context) throws DatabaseException { - Resource diagram = resolveElement(graph, context); - DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); - return baseMappingModifier(graph, diagram, DN.Diagram_edgeThicknessProperty, DN.Edge_ThicknessProperty, context); - } - - @SCLValue(type = "ReadGraph -> Resource -> Variable -> b") - public static Object nodeScalePropertyModifier(ReadGraph graph, Resource resource, Variable context) throws DatabaseException { - Resource diagram = resolveElement(graph, context); - DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); - return baseMappingModifier(graph, diagram, DN.Diagram_nodeScaleProperty, DN.Vertex_ScaleProperty, context); - } - @SCLValue(type = "ReadGraph -> Resource -> Variable -> b") public static Function1 hasDiameterValue(ReadGraph graph, Resource resource, Variable context) throws DatabaseException { return directPropertyValueFunction(DistrictNetworkResource.getInstance(graph).Edge_HasDiameter, 0);