]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/tooltip/TooltipParticipant.java
G2DParentNode handles "undefined" child bounds separately
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / tooltip / TooltipParticipant.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.tooltip;
13
14 import org.simantics.g2d.diagram.IDiagram;
15 import org.simantics.g2d.diagram.IDiagram.CompositionListener;
16 import org.simantics.g2d.element.IElement;
17 import org.simantics.g2d.element.handler.Clickable.PressStatus;
18 import org.simantics.g2d.element.handler.impl.AbstractClickable;
19 import org.simantics.utils.datastructures.hints.IHintContext.Key;
20 import org.simantics.utils.datastructures.hints.IHintListener;
21 import org.simantics.utils.datastructures.hints.IHintObservable;
22
23 /**
24  * Tooltip support
25  * 
26  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
27  *
28  */
29 public class TooltipParticipant extends BaseTooltipParticipant implements CompositionListener {
30
31     private final ElementHoverListener listener = new ElementHoverListener();
32
33     public TooltipParticipant() {
34     }
35
36     @Override
37     protected void onDiagramSet(IDiagram newValue, IDiagram oldValue) {
38         if (oldValue == newValue)
39             return;
40         if (oldValue != null) {
41             for (IElement e : oldValue.getElements()) {
42                 removeElement(e);
43             }
44             oldValue.removeCompositionListener(this);
45         }
46         if (newValue != null) {
47             for (IElement e : newValue.getElements()) {
48                 addElement(e);
49             }
50             newValue.addCompositionListener(this);
51         }
52     }
53
54     @Override
55     public void onElementAdded(IDiagram d, IElement e) {
56         addElement(e);
57     }
58
59     @Override
60     public void onElementRemoved(IDiagram d, IElement e) {
61         removeElement(e);
62     }
63
64     private void addElement(IElement e) {
65         e.addKeyHintListener(AbstractClickable.PRESS_STATUS_KEY, listener);
66     }
67
68     private void removeElement(IElement e) {
69         e.removeKeyHintListener(AbstractClickable.PRESS_STATUS_KEY, listener);
70     }
71
72
73
74     class ElementHoverListener implements IHintListener {
75
76         @Override
77         public void hintChanged(IHintObservable sender, Key key,
78                 Object oldValue, Object newValue) {
79             IElement element = (IElement)sender;
80             PressStatus status = (PressStatus)newValue;
81             if (status == PressStatus.HOVER) {
82                 showTooltipFor(element);
83             } else {
84                 hideTooltip();
85             }
86             //System.out.println("ttp element changed " + element + " " + status);
87         }
88
89         @Override
90         public void hintRemoved(IHintObservable sender, Key key, Object oldValue) {
91
92         }
93     }
94
95 }