1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g2d.diagram.participant;
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;
25 * DiagramParticipant sends events to CanvasMonitors (=diagram handler)
27 * DiagramParticipant must be added to canvas context before diagram is set.
29 * @author Toni Kalajainen
31 public class DiagramParticipant extends AbstractDiagramParticipant {
33 IHintContext ctx = new HintContext();
36 * Diagram composition listener
38 CompositionListener listener = new CompositionListener() {
40 public void onElementAdded(IDiagram d, IElement e) {
44 public void onElementRemoved(IDiagram d, IElement e) {
45 // TODO Remove element variables... somehow efficiently
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());
56 for (Key key : ctx.getHints().keySet())
59 if (oldDiagram!=null) oldDiagram.removeCompositionListener(listener);
60 if (newDiagram!=null) newDiagram.addCompositionListener(listener);
63 public void setElementHint(IElement e, Key key, Object value)
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);
71 elementCtx.setHint(key, value);
74 public <E> E getElementHint(IElement e, Key key)
76 Key elementCtxKey = new ElementCtxKey(e);
77 IHintContext elementCtx = ctx.getHint(elementCtxKey);
78 if (elementCtx==null) return null;
79 return elementCtx.getHint(key);
82 public Map<Key, Object> getElementHints(IElement e)
84 Key elementCtxKey = new ElementCtxKey(e);
85 IHintContext elementCtx = ctx.getHint(elementCtxKey);
86 if (elementCtx==null) return null;
87 return elementCtx.getHints();
90 public <E> E removeElementHint(IElement e, Key key)
92 Key elementCtxKey = new ElementCtxKey(e);
93 IHintContext elementCtx = ctx.getHint(elementCtxKey);
94 if (elementCtx==null) return null;
95 return elementCtx.removeHint(key);
98 public void setDiagramHint(IDiagram d, Key key, Object value)
100 ctx.setHint(key, value);
103 public <E> E removeDiagramHint(IDiagram d, Key key)
105 return ctx.removeHint(key);
108 @SuppressWarnings("unused")
109 private class DiagramKey extends Key {
110 public final IDiagram d;
111 private final int hash;
112 public final Key key;
114 public DiagramKey(IDiagram d, Key key)
120 this.hash = key.hashCode() ^ d.hashCode();
123 public int hashCode() {
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;
135 public boolean isValueAccepted(Object value) {
136 return key.isValueAccepted(value);
141 private class ElementKey extends Key {
142 public final IElement e;
143 private final int hash;
144 public final Key key;
146 public ElementKey(IElement e, Key key)
152 this.hash = key.hashCode() ^ e.hashCode();
155 public int hashCode() {
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;
167 public boolean isValueAccepted(Object value) {
168 return key.isValueAccepted(value);
172 private class ElementCtxKey extends Key {
173 public final IElement e;
175 public ElementCtxKey(IElement e)
181 public int hashCode() {
185 public boolean equals(Object obj) {
186 if (!(obj instanceof ElementCtxKey)) return false;
187 ElementCtxKey other = (ElementCtxKey) obj;
191 public boolean isValueAccepted(Object value) {
192 return value instanceof IHintContext;