]> gerrit.simantics Code Review - simantics/platform.git/blob
c67558b2733323f25053b134778e8bef974f65e6
[simantics/platform.git] /
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
25         DiagramResource DIA = DiagramResource.getInstance(graph);
26                 
27         if(graph.isInstanceOf(entry, DIA.Profile)) {
28                 
29                 for(Resource child : ProfileUtils.getProfileChildren(graph, entry)) {
30                         processRecursively(graph, runtimeProfile, child, checked);
31                 }
32                 
33         } else if(graph.isInstanceOf(entry, DIA.ProfileEntry)) {
34
35             if(checked) {
36                 graph.claim(runtimeProfile, SimulationResource.getInstance(graph).IsActive, null, entry);
37             } else {
38                 graph.denyStatement(runtimeProfile, SimulationResource.getInstance(graph).IsActive, entry);
39             }
40
41                 }
42                 
43         }
44         
45     @Override
46     public void handleEvent (Event event) {
47         
48         if(event.detail == SWT.CHECK) {
49                 
50             final TreeItem item = (TreeItem)event.item;
51             Tree tree = item.getParent();
52             GraphExplorer explorer = (GraphExplorer)tree.getData(GraphExplorer.KEY_GRAPH_EXPLORER);
53             final Resource runtimeDiagram = (Resource)explorer.getRoot().getConstant(BuiltinKeys.INPUT);
54             final boolean checked = item.getChecked();
55             NodeContext context = (NodeContext)item.getData();
56             final ResourcePair entry = (ResourcePair)context.getConstant(BuiltinKeys.INPUT);
57             try {
58                 
59                 VirtualGraphSupport support = Simantics.getSession().getService(VirtualGraphSupport.class);
60                 Simantics.getSession().syncRequest(new WriteRequest(support.getWorkspacePersistent("profiles")) {
61
62                     @Override
63                     public void perform(WriteGraph graph) throws DatabaseException {
64                         
65                         DiagramResource DIA = DiagramResource.getInstance(graph);
66                         Resource runtimeProfile = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasRuntimeProfile);
67                         processRecursively(graph, runtimeProfile, entry.getSecond(), checked);
68                         
69                     }
70
71                 });
72             } catch (DatabaseException e) {
73                 e.printStackTrace();
74             }
75         }
76     }
77
78 }