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%2FDefaultMappingsDialog.java;fp=org.simantics.district.network.ui%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Fui%2Ffunction%2FDefaultMappingsDialog.java;h=efe7b85e4ae7ae8bb30954ca092308a9c70b903a;hb=81cb57a515193f9d1167d35a7cfde321e4e7a993;hp=0000000000000000000000000000000000000000;hpb=63ca3304ae8ef41b9bae9a97b8ff04adc6ad0371;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/function/DefaultMappingsDialog.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/function/DefaultMappingsDialog.java new file mode 100644 index 00000000..efe7b85e --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/function/DefaultMappingsDialog.java @@ -0,0 +1,263 @@ +package org.simantics.district.network.ui.function; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.eclipse.jface.layout.GridDataFactory; +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.dialogs.SelectionStatusDialog; +import org.simantics.Simantics; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.IndexRoot; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.exception.ServiceException; +import org.simantics.district.network.ontology.DistrictNetworkResource; +import org.simantics.layer0.Layer0; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DefaultMappingsDialog extends SelectionStatusDialog { + + private static final Logger LOGGER = LoggerFactory.getLogger(DefaultMappingsDialog.class); + + private Combo vertexMappingCombo; + private Combo rightClickMappingCombo; + private Combo leftClickMappingCombo; + 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 rightClickVertexMapping; + private Resource leftClickVertexMapping; + //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 getRightClickVertexMapping() { + return rightClickVertexMapping; + } + + public Resource getLeftClickVertexMapping() { + return leftClickVertexMapping; + } + + 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 { + Resource indexRoot = graph.sync(new IndexRoot(configuration)); + vertexMappings = Functions.getVertexMappings(graph, indexRoot); + edgeMappings = Functions.getEdgeMappings(graph, indexRoot); + + composites = getComposites(graph, configuration); + + crss = Functions.getCRSs(graph, configuration); + + composite.getDisplay().asyncExec(() -> { + + vertexMappingCombo.setItems(vertexMappings.keySet().toArray(new String[vertexMappings.size()])); + rightClickMappingCombo.setItems(vertexMappings.keySet().toArray(new String[vertexMappings.size()])); + leftClickMappingCombo.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); + rightClickMappingCombo.select(0); + leftClickMappingCombo.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 rightClickMappingLabel = new Label(cmposite, SWT.NONE); + rightClickMappingLabel.setText("Default right click mapping"); + + rightClickMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER); + GridDataFactory.fillDefaults().grab(true, false).applyTo(vertexMappingCombo); + + Label leftClickMappingLabel = new Label(cmposite, SWT.NONE); + leftClickMappingLabel.setText("Default left click mapping"); + + leftClickMappingCombo = 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())); + rightClickVertexMapping = vertexMappings.get(rightClickMappingCombo.getItem(rightClickMappingCombo.getSelectionIndex())); + leftClickVertexMapping = vertexMappings.get(leftClickMappingCombo.getItem(leftClickMappingCombo.getSelectionIndex())); + //defaultCRS = crss.get(crsCombo.getItem(crsCombo.getSelectionIndex())); + } + + public Resource getCRS() { + return crss.get("EPSG_4326"); // this is only supported + } + +} \ No newline at end of file