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;
14 import java.util.HashSet;
16 import java.util.concurrent.ScheduledExecutorService;
17 import java.util.concurrent.ScheduledFuture;
18 import java.util.concurrent.TimeUnit;
20 import org.simantics.g2d.canvas.Hints;
21 import org.simantics.g2d.canvas.ICanvasContext;
22 import org.simantics.g2d.diagram.IDiagram;
23 import org.simantics.g2d.diagram.IDiagram.CompositionListener;
24 import org.simantics.g2d.diagram.handler.PickRequest.PickFilter;
25 import org.simantics.g2d.element.IElement;
26 import org.simantics.g2d.internal.DebugPolicy;
27 import org.simantics.utils.datastructures.hints.IHintContext.Key;
28 import org.simantics.utils.datastructures.hints.IHintListener;
29 import org.simantics.utils.datastructures.hints.IHintObservable;
30 import org.simantics.utils.threads.ThreadUtils;
33 * A diagram participant that is meant for keeping a selected set of diagram
34 * elements up-to-date. The difference to {@link ElementPainter} is that this
35 * participant throttles updates so that the selected set of elements is updated
36 * once in a specified time interval.
39 * This class listens to {@link Hints#KEY_DIRTY} hint of elements accepted by
40 * the specified {@link #elementFilter}. If the key receives a value of
41 * {@link Hints#VALUE_SG_DELAYED_UPDATE}, this class will react by queueing the
42 * element to be updated after the current delay period is over. The delay
43 * period ensures that existing elements are not updated more frequently than
44 * the specified time period. When it becomes time to perform the actual
45 * updates, this class will set the {@link Hints#KEY_DIRTY} hint of all
46 * currently dirty elements to value {@link Hints#VALUE_SG_DIRTY}. This will
47 * signal other listeners ({@link ElementPainter}) to react.
49 * @author Tuukka Lehtonen
51 public class DelayedBatchElementPainter extends AbstractDiagramParticipant {
53 private static final boolean DEBUG = DebugPolicy.DEBUG_DELAYED_ELEMENT_PAINTER;
54 private static final boolean DEBUG_MARKING = DebugPolicy.DEBUG_DELAYED_ELEMENT_PAINTER_MARKING;
56 ScheduledExecutorService executor = ThreadUtils.getNonBlockingWorkExecutor();
58 PickFilter elementFilter;
62 Set<IElement> dirtyElements = new HashSet<IElement>();
63 volatile ScheduledFuture<?> scheduled = null;
64 long lastUpdateTime = 0;
66 class ElementListener implements CompositionListener, IHintListener {
68 public void onElementAdded(IDiagram d, IElement e) {
70 debug("onElementAdded(%s, %s)\n", d, e);
71 if (elementFilter.accept(e))
76 public void onElementRemoved(IDiagram d, IElement e) {
78 debug("onElementRemoved(%s, %s)\n", d, e);
79 if (elementFilter.accept(e))
84 public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
85 if (key == Hints.KEY_DIRTY && newValue == Hints.VALUE_SG_DELAYED_UPDATE && sender instanceof IElement) {
86 markDirty((IElement) sender);
91 public void hintRemoved(IHintObservable sender, Key key, Object oldValue) {
95 private final ElementListener elementListener = new ElementListener();
97 public DelayedBatchElementPainter(PickFilter elementFilter, long delay, TimeUnit delayUnit) {
98 this.elementFilter = elementFilter;
100 this.delayUnit = delayUnit;
104 public void removedFromContext(ICanvasContext ctx) {
105 ScheduledFuture<?> s = scheduled;
109 super.removedFromContext(ctx);
113 protected void onDiagramSet(IDiagram newValue, IDiagram oldValue) {
114 if (oldValue == newValue)
117 if (oldValue != null) {
118 for (IElement e : oldValue.getElements()) {
119 if (elementFilter.accept(e))
122 oldValue.removeCompositionListener(elementListener);
125 if (newValue != null) {
126 for (IElement e : newValue.getElements()) {
127 if (elementFilter.accept(e))
130 newValue.addCompositionListener(elementListener);
134 protected void addElement(IElement e) {
136 debug("addElement(%s)\n", e);
137 e.addKeyHintListener(Hints.KEY_DIRTY, elementListener);
140 protected void removeElement(IElement e) {
142 debug("removeElement(%s)\n", e);
143 e.removeKeyHintListener(Hints.KEY_DIRTY, elementListener);
146 protected void markDirty(IElement e) {
148 debug("Marking element dirty: %s\n", e);
149 dirtyElements.add(e);
153 private Runnable updater = new Runnable() {
157 debug("marking %d elements dirty\n", dirtyElements.size());
158 for (IElement e : dirtyElements)
159 e.setHint(Hints.KEY_DIRTY, Hints.VALUE_SG_DIRTY);
160 dirtyElements.clear();
162 lastUpdateTime = System.currentTimeMillis();
164 debug("marking last update time %d\n", lastUpdateTime);
171 private Runnable delayedUpdater = new Runnable() {
175 debug("scheduling updater\n");
180 private void scheduleUpdate() {
181 if (scheduled == null) {
182 long timeNow = System.currentTimeMillis();
183 long timeSinceLastUpdate = timeNow - lastUpdateTime;
184 long requestedDelay = delayUnit.toMillis(delay);
185 long scheduleDelay = Math.max(0, Math.min(requestedDelay, requestedDelay - timeSinceLastUpdate));
188 debug("scheduling update with delay %dms (time=%d, time passed=%dms)\n", scheduleDelay, timeNow, timeSinceLastUpdate);
190 if (scheduleDelay == 0) {
193 scheduled = executor.schedule(delayedUpdater, scheduleDelay, TimeUnit.MILLISECONDS);
199 private void debug(String format, Object... args) {
201 System.out.format(getClass().getSimpleName()
202 + "[filter=" + elementFilter
203 + ", delay=" + delayUnit.toMillis(delay) + "ms] "