]> gerrit.simantics Code Review - simantics/district.git/blob - 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
1 package org.simantics.district.network.ui.contributions;
2
3 import java.util.HashMap;
4 import java.util.List;
5 import java.util.Map;
6
7 import javax.inject.Named;
8
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;
45
46 public class ChangeMappingTypeHandler {
47
48     
49     @CanExecute
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)
53             return false;
54         try {
55             return Simantics.getSession().syncRequest(new Read<Boolean>() {
56
57                 @Override
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);
64                         } else {
65                             Resource currentInstanceOf = graph.getSingleObject(element, L0.InstanceOf);
66                             if (!currentInstanceOf.equals(instanceOf)) {
67                                 return false;
68                             }
69                         }
70                     }
71                     return true;
72                 }
73             });
74         } catch (DatabaseException e) {
75             e.printStackTrace();
76             return false;
77         }
78     }
79     
80     @Execute
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)
84             return;
85         
86         SelectMappingDialog dialog = new SelectMappingDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), elements);
87         if (dialog.open() != Dialog.OK)
88             return;
89         
90         Resource mapping = dialog.getDefaultVertexMapping();
91         Job job = new DatabaseJob("Join selected vertices") {
92             
93             @Override
94             protected IStatus run(IProgressMonitor monitor) {
95                 try {
96                     Simantics.getSession().syncRequest(new WriteRequest() {
97                         
98                         @Override
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);
104                             }
105                         }
106                     });
107                 } catch (DatabaseException e) {
108                     return new Status(IStatus.ERROR, Activator.PLUGIN_ID, getName() + " failed.", e);
109                 }
110                 return Status.OK_STATUS;
111             }
112         };
113         job.setUser(true);
114         job.schedule();
115     }
116     
117     private static class SelectMappingDialog extends SelectionStatusDialog {
118
119         private Combo vertexMappingCombo;
120         
121         private Composite composite;
122         
123         private List<Resource> elements;
124         private Map<String, Resource> vertexMappings = new HashMap<>();
125         
126         private Resource defaultVertexMapping;
127
128         protected SelectMappingDialog(Shell parentShell, List<Resource> elements) {
129             super(parentShell);
130             this.elements = elements;
131             setTitle("Select mappings for new DN diagram");
132         }
133
134         public Resource getDefaultVertexMapping() {
135             return defaultVertexMapping;
136         }
137
138         @Override
139         protected Control createDialogArea(Composite parent) {
140             composite = (Composite) super.createDialogArea(parent);
141             
142             createMappingsGroup(composite);
143             
144             // compute default values
145             Simantics.getSession().asyncRequest(new ReadRequest() {
146
147                 @Override
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);
155                     }
156                     composite.getDisplay().asyncExec(() -> {
157                         vertexMappingCombo.setItems(vertexMappings.keySet().toArray(new String[vertexMappings.size()]));
158                         vertexMappingCombo.select(0);
159                     }); 
160                     
161                 }
162             });
163             return composite;
164         }
165
166         @Override
167         protected void computeResult() {
168             defaultVertexMapping = vertexMappings.get(vertexMappingCombo.getItem(vertexMappingCombo.getSelectionIndex()));
169         }
170         
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));
177             
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));
181             
182             Label vertexMappingLabel = new Label(cmposite, SWT.NONE);
183             vertexMappingLabel.setText("Default vertex mapping");
184
185             vertexMappingCombo = new Combo(cmposite, SWT.READ_ONLY | SWT.BORDER);
186             GridDataFactory.fillDefaults().grab(true, false).applyTo(vertexMappingCombo);
187             
188         }
189     }
190 }