]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/participant/ElementHeartbeater.java
Sync git svn branch with SVN repository r33269.
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / diagram / participant / ElementHeartbeater.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in 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  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g2d.diagram.participant;\r
13 \r
14 import org.simantics.g2d.canvas.ICanvasContext;\r
15 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
16 import org.simantics.g2d.diagram.IDiagram;\r
17 import org.simantics.g2d.element.IElement;\r
18 import org.simantics.g2d.element.handler.Heartbeat;\r
19 import org.simantics.g2d.participant.TimeParticipant;\r
20 import org.simantics.scenegraph.g2d.events.TimeEvent;\r
21 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
22 \r
23 /**\r
24  * This participant sends time events to Heartbeat element handlers.\r
25  * \r
26  * @See {@link Heartbeat}\r
27  * @author Toni Kalajainen\r
28  */\r
29 public class ElementHeartbeater extends AbstractDiagramParticipant {\r
30 \r
31     @Dependency TimeParticipant time;\r
32 \r
33     @Override\r
34     public void addedToContext(ICanvasContext ctx) {\r
35         super.addedToContext(ctx);\r
36         time.registerForEvents(getClass());\r
37     }\r
38 \r
39     @Override\r
40     public void removedFromContext(ICanvasContext ctx) {\r
41                 time.unregisterForEvents(getClass());\r
42         super.removedFromContext(ctx);\r
43     }\r
44 \r
45     @EventHandler(priority = 0)\r
46     public boolean handleHeartbeat(TimeEvent te) {\r
47         IDiagram d = diagram;\r
48         if (d==null) return false;\r
49         for (IElement e : d.getSnapshot())\r
50         {\r
51             for (Heartbeat hb : e.getElementClass().getItemsByClass(Heartbeat.class))\r
52                 hb.heartbeat(e, te.time, te.interval, getContext());\r
53         }\r
54         return false;\r
55     }\r
56 \r
57 }\r