]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/participant/DiagramParticipant.java
G2DParentNode handles "undefined" child bounds separately
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / diagram / participant / DiagramParticipant.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g2d.diagram.participant;
13
14 import java.util.Map;
15
16 import org.simantics.g2d.diagram.IDiagram;
17 import org.simantics.g2d.diagram.IDiagram.CompositionListener;
18 import org.simantics.g2d.diagram.handler.CanvasListener;
19 import org.simantics.g2d.element.IElement;
20 import org.simantics.utils.datastructures.hints.HintContext;
21 import org.simantics.utils.datastructures.hints.IHintContext;
22 import org.simantics.utils.datastructures.hints.IHintContext.Key;
23
24 /**
25  * DiagramParticipant sends events to CanvasMonitors (=diagram handler)
26  * 
27  * DiagramParticipant must be added to canvas context before diagram is set.
28  * 
29  * @author Toni Kalajainen
30  */
31 public class DiagramParticipant extends AbstractDiagramParticipant {
32
33     IHintContext ctx = new HintContext();
34
35     /**
36      * Diagram composition listener
37      */
38     CompositionListener listener = new CompositionListener() {
39         @Override
40         public void onElementAdded(IDiagram d, IElement e) {
41
42         }
43         @Override
44         public void onElementRemoved(IDiagram d, IElement e) {
45             // TODO Remove element variables...  somehow efficiently
46         }
47     };
48
49     @Override
50     protected void onDiagramSet(IDiagram newDiagram, IDiagram oldDiagram) {
51         if (clazz==null) return;
52         for (CanvasListener cm : clazz.getItemsByClass(CanvasListener.class))
53             cm.onAddedToCanvas(newDiagram, getContext());
54
55         if (newDiagram==null)
56             for (Key key : ctx.getHints().keySet())
57                 ctx.removeHint(key);
58
59         if (oldDiagram!=null) oldDiagram.removeCompositionListener(listener);
60         if (newDiagram!=null) newDiagram.addCompositionListener(listener);
61     }
62
63     public void setElementHint(IElement e, Key key, Object value)
64     {
65         Key elementCtxKey = new ElementCtxKey(e);
66         IHintContext elementCtx = ctx.getHint(elementCtxKey);
67         if (elementCtx==null) {
68             elementCtx = new HintContext();
69             ctx.setHint(elementCtxKey, elementCtx);
70         }
71         elementCtx.setHint(key, value);
72     }
73
74     public <E> E getElementHint(IElement e, Key key)
75     {
76         Key elementCtxKey = new ElementCtxKey(e);
77         IHintContext elementCtx = ctx.getHint(elementCtxKey);
78         if (elementCtx==null) return null;
79         return elementCtx.getHint(key);
80     }
81
82     public Map<Key, Object> getElementHints(IElement e)
83     {
84         Key elementCtxKey = new ElementCtxKey(e);
85         IHintContext elementCtx = ctx.getHint(elementCtxKey);
86         if (elementCtx==null) return null;
87         return elementCtx.getHints();
88     }
89
90     public <E> E removeElementHint(IElement e, Key key)
91     {
92         Key elementCtxKey = new ElementCtxKey(e);
93         IHintContext elementCtx = ctx.getHint(elementCtxKey);
94         if (elementCtx==null) return null;
95         return elementCtx.removeHint(key);
96     }
97
98     public void setDiagramHint(IDiagram d, Key key, Object value)
99     {
100         ctx.setHint(key, value);
101     }
102
103     public <E> E removeDiagramHint(IDiagram d, Key key)
104     {
105         return ctx.removeHint(key);
106     }
107
108     @SuppressWarnings("unused")
109     private class DiagramKey extends Key {
110         public final IDiagram d;
111         private final int hash;
112         public final Key key;
113
114         public DiagramKey(IDiagram d, Key key)
115         {
116             super();
117             assert(key!=null);
118             this.key = key;
119             this.d = d;
120             this.hash = key.hashCode() ^ d.hashCode();
121         }
122         @Override
123         public int hashCode() {
124             return hash;
125         }
126         @Override
127         public boolean equals(Object obj) {
128             if (!(obj instanceof DiagramKey)) return false;
129             DiagramKey other = (DiagramKey) obj;
130             if (other.key.equals(key)) return false;
131             if (other.d != d) return false;
132             return true;
133         }
134         @Override
135         public boolean isValueAccepted(Object value) {
136             return key.isValueAccepted(value);
137         }
138
139     }
140
141     private class ElementKey extends Key {
142         public final IElement e;
143         private final int hash;
144         public final Key key;
145
146         public ElementKey(IElement e, Key key)
147         {
148             super();
149             assert(key!=null);
150             this.key = key;
151             this.e = e;
152             this.hash = key.hashCode() ^ e.hashCode();
153         }
154         @Override
155         public int hashCode() {
156             return hash;
157         }
158         @Override
159         public boolean equals(Object obj) {
160             if (!(obj instanceof ElementKey)) return false;
161             ElementKey other = (ElementKey) obj;
162             if (other.e != e) return false;
163             if (!other.key.equals(key)) return false;
164             return true;
165         }
166         @Override
167         public boolean isValueAccepted(Object value) {
168             return key.isValueAccepted(value);
169         }
170     }
171
172     private class ElementCtxKey extends Key {
173         public final IElement e;
174
175         public ElementCtxKey(IElement e)
176         {
177             super();
178             this.e = e;
179         }
180         @Override
181         public int hashCode() {
182             return e.hashCode();
183         }
184         @Override
185         public boolean equals(Object obj) {
186             if (!(obj instanceof ElementCtxKey)) return false;
187             ElementCtxKey other = (ElementCtxKey) obj;
188             return other.e == e;
189         }
190         @Override
191         public boolean isValueAccepted(Object value) {
192             return value instanceof IHintContext;
193         }
194     }
195
196
197 }