]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
f610101524c04269a1f7a10e3b1e1b1c624a809b
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2013 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     Semantum Oy - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.editor.participant;\r
13 \r
14 import java.util.Collection;\r
15 \r
16 import org.simantics.db.Resource;\r
17 import org.simantics.db.Session;\r
18 import org.simantics.db.VirtualGraph;\r
19 import org.simantics.db.WriteGraph;\r
20 import org.simantics.db.common.request.WriteRequest;\r
21 import org.simantics.db.exception.DatabaseException;\r
22 import org.simantics.diagram.stubs.DiagramResource;\r
23 import org.simantics.diagram.ui.DiagramModelHints;\r
24 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
25 import org.simantics.g2d.canvas.impl.HintReflection.HintListener;\r
26 import org.simantics.g2d.diagram.DiagramHints;\r
27 import org.simantics.g2d.diagram.IDiagram;\r
28 import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;\r
29 import org.simantics.g2d.diagram.participant.Selection;\r
30 import org.simantics.g2d.element.ElementHints;\r
31 import org.simantics.g2d.element.IElement;\r
32 import org.simantics.sysdyn.SysdynResource;\r
33 import org.simantics.ui.SimanticsUI;\r
34 import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
35 import org.simantics.utils.datastructures.hints.IHintObservable;\r
36 import org.simantics.utils.ui.AdaptionUtils;\r
37 \r
38 \r
39 public class SelectionUpdaterParticipant extends AbstractDiagramParticipant {\r
40 \r
41     @Dependency Selection selection;\r
42 \r
43     @HintListener(Class = Selection.class, Field = "SELECTION0")\r
44     public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {\r
45         IDiagram diagram = sender.getHint(DiagramHints.KEY_DIAGRAM);\r
46         Collection<IElement> elements = AdaptionUtils.adaptToCollection(newValue, IElement.class);\r
47         if(diagram != null) {\r
48             Session session = SimanticsUI.getSession();\r
49             session.asyncRequest(new ModifyDiagramSelection(session.getService(VirtualGraph.class), diagram, elements));\r
50         }\r
51     }\r
52     \r
53     @HintListener(Class = Selection.class, Field = "SELECTION0")\r
54     public void hintRemoved(IHintObservable sender, Key key, Object oldValue) {\r
55         IDiagram diagram = sender.getHint(DiagramHints.KEY_DIAGRAM);\r
56         if(diagram != null) {\r
57             Session session = SimanticsUI.getSession();\r
58             session.asyncRequest(new ModifyDiagramSelection(session.getService(VirtualGraph.class), diagram, null));\r
59         }\r
60     }\r
61     \r
62     \r
63     private class ModifyDiagramSelection extends WriteRequest {\r
64         IDiagram diagram;\r
65         Collection<IElement> elements;\r
66         \r
67         public ModifyDiagramSelection(VirtualGraph provider, IDiagram diagram, Collection<IElement> elements) {\r
68             super(provider);\r
69             this.elements = elements;\r
70             this.diagram = diagram;\r
71         }\r
72         \r
73         @Override\r
74         public void perform(WriteGraph graph) throws DatabaseException {\r
75             Resource diagramRuntime = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE);\r
76             SysdynResource SR = SysdynResource.getInstance(graph);\r
77             DiagramResource DR = DiagramResource.getInstance(graph);\r
78 \r
79             graph.deny(diagramRuntime, SR.ConfigurationDiagram_selection);\r
80 \r
81             if(elements != null) {\r
82                 for(IElement e : elements) {\r
83                     Resource object = e.getHint(ElementHints.KEY_OBJECT);\r
84                     if(object != null && graph.isInstanceOf(object, DR.Element)) {\r
85                         graph.claim(diagramRuntime, SR.ConfigurationDiagram_selection, object);\r
86                     }\r
87                 }\r
88             }\r
89         }\r
90     }\r
91 }\r