]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram.profile/src/org/simantics/diagram/profile/view/ProfileEntrySelectionListener.java
Better support for ontological profiles
[simantics/platform.git] / bundles / org.simantics.diagram.profile / src / org / simantics / diagram / profile / view / ProfileEntrySelectionListener.java
1 package org.simantics.diagram.profile.view;
2
3 import org.eclipse.swt.SWT;
4 import org.eclipse.swt.widgets.Event;
5 import org.eclipse.swt.widgets.Listener;
6 import org.eclipse.swt.widgets.Tree;
7 import org.eclipse.swt.widgets.TreeItem;
8 import org.simantics.Simantics;
9 import org.simantics.browsing.ui.BuiltinKeys;
10 import org.simantics.browsing.ui.GraphExplorer;
11 import org.simantics.browsing.ui.NodeContext;
12 import org.simantics.db.Resource;
13 import org.simantics.db.WriteGraph;
14 import org.simantics.db.common.request.WriteRequest;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.service.VirtualGraphSupport;
17 import org.simantics.diagram.stubs.DiagramResource;
18 import org.simantics.scenegraph.profile.ProfileUtils;
19 import org.simantics.simulation.ontology.SimulationResource;
20
21 public class ProfileEntrySelectionListener implements Listener {
22
23     public void processRecursively(WriteGraph graph, Resource runtimeProfile, Resource entry, boolean checked) throws DatabaseException {
24         DiagramResource DIA = DiagramResource.getInstance(graph);
25
26         if (graph.isInstanceOf(entry, DIA.Profile)) {
27             for (Resource child : ProfileUtils.getProfileChildren(graph, entry)) {
28                 processRecursively(graph, runtimeProfile, child, checked);
29             }
30         } else if (graph.isInstanceOf(entry, DIA.ProfileEntry)) {
31             if (checked) {
32                 graph.claim(runtimeProfile, SimulationResource.getInstance(graph).IsActive, null, entry);
33             } else {
34                 graph.denyStatement(runtimeProfile, SimulationResource.getInstance(graph).IsActive, entry);
35             }
36         }
37     }
38
39     @Override
40     public void handleEvent (Event event) {
41         if(event.detail == SWT.CHECK) {
42             final TreeItem item = (TreeItem)event.item;
43             Tree tree = item.getParent();
44             GraphExplorer explorer = (GraphExplorer)tree.getData(GraphExplorer.KEY_GRAPH_EXPLORER);
45             final Resource runtimeDiagram = (Resource)explorer.getRoot().getConstant(BuiltinKeys.INPUT);
46             final boolean checked = item.getChecked();
47             NodeContext context = (NodeContext)item.getData();
48             final ProfileTuple entry = (ProfileTuple)context.getConstant(BuiltinKeys.INPUT);
49             try {
50                 VirtualGraphSupport support = Simantics.getSession().getService(VirtualGraphSupport.class);
51                 Simantics.getSession().syncRequest(new WriteRequest(support.getWorkspacePersistent("profiles")) {
52
53                     @Override
54                     public void perform(WriteGraph graph) throws DatabaseException {
55                         DiagramResource DIA = DiagramResource.getInstance(graph);
56                         Resource runtimeProfile = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasRuntimeProfile);
57                         processRecursively(graph, runtimeProfile, entry.getEntry(), checked);
58                     }
59
60                 });
61             } catch (DatabaseException e) {
62                 e.printStackTrace();
63             }
64         }
65     }
66
67 }