X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.g2d%2Fsrc%2Forg%2Fsimantics%2Fg2d%2Fparticipant%2FTimeParticipant.java;fp=bundles%2Forg.simantics.g2d%2Fsrc%2Forg%2Fsimantics%2Fg2d%2Fparticipant%2FTimeParticipant.java;h=3b7f5ae2347f845d2a61bb8e6cf047a9bca8c992;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/participant/TimeParticipant.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/participant/TimeParticipant.java new file mode 100644 index 000000000..3b7f5ae23 --- /dev/null +++ b/bundles/org.simantics.g2d/src/org/simantics/g2d/participant/TimeParticipant.java @@ -0,0 +1,220 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.g2d.participant; + +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + +import org.simantics.g2d.canvas.ICanvasContext; +import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant; +import org.simantics.g2d.diagram.participant.ElementHeartbeater; +import org.simantics.g2d.element.handler.Heartbeat; +import org.simantics.scenegraph.g2d.events.Event; +import org.simantics.scenegraph.g2d.events.TimeEvent; +import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler; +import org.simantics.utils.datastructures.disposable.DisposeState; +import org.simantics.utils.datastructures.hints.HintListenerAdapter; +import org.simantics.utils.datastructures.hints.IHintListener; +import org.simantics.utils.datastructures.hints.IHintObservable; +import org.simantics.utils.datastructures.hints.IHintContext.Key; +import org.simantics.utils.datastructures.hints.IHintContext.KeyOf; +import org.simantics.utils.threads.ThreadUtils; + +/** + * Time Participant sends time pulses by setting KEY_TIME hint. The value of + * KEY_TIME is system current time in milliseconds. + * + * The hint KEY_TIME_PULSE_INTERVAL controls the interval of time pulse events. + * + * Time pulse events can be listened by listening modifications of KEY_TIME + * hint. + * + * KEY_TIMER_ENABLED controls the enabled state of the timer, if false, no + * events will be sent even if there are recipients. + * + * To be able receive timer events, add a handler like this to your canvas + * participant:
+ * @EventHandler(priority = 0) + * public boolean handleTimeEvent(TimeEvent e) { + * // do something + * // Don't eat the event, let others see it too. + * return false; + * }+ * When you need to receive time events, inform TimeParticipant by invoking + *
timeParticipant.registerForEvents(getClass());
and when you no
+ * longer need the events, use
+ * timeParticipant.unregisterForEvents(getClass());
. This allows
+ * TimeParticipant to optimize its internal behavior.
+ *
+ * @author Toni Kalajainen
+ * @author Tuukka Lehtonen
+ *
+ * @see ElementHeartbeater
+ * @see Heartbeat
+ * @see Notifications
+ */
+public class TimeParticipant extends AbstractCanvasParticipant {
+
+ /** Key for global timer enable state */
+ public static final Key KEY_TIMER_ENABLED = new KeyOf(Boolean.class);
+
+ /** Key for time code */
+ public static final Key KEY_TIME = new KeyOf(Long.class);
+
+ /** Key for timer interval in milliseconds */
+ public static final Key KEY_TIME_PULSE_INTERVAL = new KeyOf(Long.class);
+
+ /** Default interval in milliseconds */
+ public static final long DEFAULT_INTERVAL = 100L;
+
+ ScheduledFuture> future = null;
+
+ Set