1 package org.simantics.district.network.ui.contributions;
3 import java.util.HashMap;
7 import javax.inject.Named;
9 import org.eclipse.core.runtime.IProgressMonitor;
10 import org.eclipse.core.runtime.IStatus;
11 import org.eclipse.core.runtime.Status;
12 import org.eclipse.core.runtime.jobs.Job;
13 import org.eclipse.e4.core.di.annotations.CanExecute;
14 import org.eclipse.e4.core.di.annotations.Execute;
15 import org.eclipse.e4.ui.services.IServiceConstants;
16 import org.eclipse.jface.dialogs.Dialog;
17 import org.eclipse.jface.layout.GridDataFactory;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Combo;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Group;
26 import org.eclipse.swt.widgets.Label;
27 import org.eclipse.swt.widgets.Shell;
28 import org.eclipse.ui.PlatformUI;
29 import org.eclipse.ui.dialogs.SelectionStatusDialog;
30 import org.simantics.DatabaseJob;
31 import org.simantics.Simantics;
32 import org.simantics.db.ReadGraph;
33 import org.simantics.db.Resource;
34 import org.simantics.db.WriteGraph;
35 import org.simantics.db.common.request.ReadRequest;
36 import org.simantics.db.common.request.WriteRequest;
37 import org.simantics.db.exception.DatabaseException;
38 import org.simantics.db.layer0.SelectionHints;
39 import org.simantics.db.request.Read;
40 import org.simantics.district.network.ontology.DistrictNetworkResource;
41 import org.simantics.district.network.ui.function.Functions;
42 import org.simantics.district.network.ui.internal.Activator;
43 import org.simantics.layer0.Layer0;
44 import org.simantics.utils.ui.ISelectionUtils;
46 public class ChangeMappingTypeHandler {
50 public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) {
51 List<Resource> elements = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
52 if (elements.size() < 1)
55 return Simantics.getSession().syncRequest(new Read<Boolean>() {
58 public Boolean perform(ReadGraph graph) throws DatabaseException {
59 Layer0 L0 = Layer0.getInstance(graph);
60 Resource instanceOf = null;
61 for (Resource element : elements) {
62 if (instanceOf == null) {
63 instanceOf = graph.getSingleObject(element, L0.InstanceOf);
65 Resource currentInstanceOf = graph.getSingleObject(element, L0.InstanceOf);
66 if (!currentInstanceOf.equals(instanceOf)) {
74 } catch (DatabaseException e) {
81 public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) Object selection) {
82 final List<Resource> elements = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
83 if (elements.size() < 1)
86 SelectMappingDialog dialog = new SelectMappingDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), elements);
87 if (dialog.open() != Dialog.OK)
90 Resource mapping = dialog.getDefaultVertexMapping();
91 Job job = new DatabaseJob("Join selected vertices") {
94 protected IStatus run(IProgressMonitor monitor) {
96 Simantics.getSession().syncRequest(new WriteRequest() {
99 public void perform(WriteGraph graph) throws DatabaseException {
100 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
101 for (Resource element : elements) {
102 graph.deny(element, DN.HasMapping);
103 graph.claim(element, DN.HasMapping, mapping);
107 } catch (DatabaseException e) {
108 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, getName() + " failed.", e);
110 return Status.OK_STATUS;
117 private static class SelectMappingDialog extends SelectionStatusDialog {
119 private Combo vertexMappingCombo;
121 private Composite composite;
123 private List<Resource> elements;
124 private Map<String, Resource> vertexMappings = new HashMap<>();
126 private Resource defaultVertexMapping;
128 protected SelectMappingDialog(Shell parentShell, List<Resource> elements) {
130 this.elements = elements;
131 setTitle("Select mappings for new DN diagram");
134 public Resource getDefaultVertexMapping() {
135 return defaultVertexMapping;
139 protected Control createDialogArea(Composite parent) {
140 composite = (Composite) super.createDialogArea(parent);
142 createMappingsGroup(composite);
144 // compute default values
145 Simantics.getSession().asyncRequest(new ReadRequest() {
148 public void run(ReadGraph graph) throws DatabaseException {
149 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
150 Resource element = elements.get(0);
151 if (graph.isInstanceOf(element, DN.Vertex)) {
152 vertexMappings = Functions.getVertexMappings(graph, element);
153 } else if (graph.isInstanceOf(element, DN.Edge)) {
154 vertexMappings = Functions.getEdgeMappings(graph, element);
156 composite.getDisplay().asyncExec(() -> {
157 vertexMappingCombo.setItems(vertexMappings.keySet().toArray(new String[vertexMappings.size()]));
158 vertexMappingCombo.select(0);
167 protected void computeResult() {
168 defaultVertexMapping = vertexMappings.get(vertexMappingCombo.getItem(vertexMappingCombo.getSelectionIndex()));
171 private void createMappingsGroup(Composite parent) {
172 Group group= new Group(parent, SWT.NONE);
173 group.setFont(parent.getFont());
174 group.setText("Default mappings");
175 GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
176 group.setLayout(new GridLayout(1, false));
178 Composite cmposite = new Composite(group, SWT.NONE);
179 cmposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
180 cmposite.setLayout(new GridLayout(2, false));
182 Label vertexMappingLabel = new Label(cmposite, SWT.NONE);
183 vertexMappingLabel.setText("Default vertex mapping");
185 vertexMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
186 GridDataFactory.fillDefaults().grab(true, false).applyTo(vertexMappingCombo);