1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
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.modeling.ui.diagram.monitor;
15 import java.util.concurrent.atomic.AtomicBoolean;
16 import java.util.concurrent.atomic.AtomicReference;
18 import org.simantics.db.Disposable;
19 import org.simantics.db.Resource;
20 import org.simantics.db.procedure.Listener;
21 import org.simantics.diagram.elements.MonitorClass;
22 import org.simantics.g2d.canvas.Hints;
23 import org.simantics.g2d.canvas.ICanvasContext;
24 import org.simantics.g2d.diagram.IDiagram;
25 import org.simantics.g2d.element.ElementUtils;
26 import org.simantics.g2d.element.IElement;
27 import org.simantics.utils.threads.ThreadUtils;
28 import org.simantics.utils.ui.ErrorLogger;
31 * @author Tuukka Lehtonen
33 public class MonitorListener implements Listener<MonitorVariableValue>, Runnable, Disposable {
35 private final Resource element;
36 private ICanvasContext canvas;
37 private IDiagram diagram;
38 private final Map<String, String> substitutions;
40 private transient AtomicReference<MonitorVariableValue> lastScheduledUpdate = null;
41 private transient AtomicBoolean inUpdate = null;
43 public MonitorListener(Resource element, ICanvasContext canvas, IDiagram diagram, Map<String, String> substitutions) {
45 throw new NullPointerException("null element");
47 throw new NullPointerException("null canvas");
49 throw new NullPointerException("null diagram");
50 if (substitutions == null)
51 throw new NullPointerException("null substitutions");
52 this.element = element;
54 this.diagram = diagram;
55 this.substitutions = substitutions;
59 public void dispose() {
65 public void execute(MonitorVariableValue result) {
66 // Implement some kind of throttling for AWT thread element
67 // update scheduling to keep the amount of AWT scheduling
70 inUpdate = new AtomicBoolean(false);
71 if (lastScheduledUpdate == null)
72 lastScheduledUpdate = new AtomicReference<MonitorVariableValue>();
74 lastScheduledUpdate.set(result);
76 // Don't schedule update if there was already one in the pipe.
77 synchronized (inUpdate) {
78 if (!inUpdate.compareAndSet(false, true))
82 //System.out.println(this + ".execute(" + result + "+")");
91 IElement el = ElementUtils.getByData(diagram, element);
98 // Mark null to allow new update scheduling to commence.
99 synchronized (inUpdate) {
100 if (lastScheduledUpdate.get() != null)
108 private void scheduleUpdate() {
109 ICanvasContext canvas = this.canvas;
111 ThreadUtils.asyncExec(canvas.getThreadAccess(), this);
114 private void performUpdate(IElement el) {
115 // Get the last updated monitor value but don't yet
116 // mark the container null to keep the outer code
117 // from scheduling new updates until this one is
119 MonitorVariableValue result = lastScheduledUpdate.getAndSet(null);
121 String value = "<no variable>";
122 if (result != null) {
123 if (result.getValue() != null) {
124 value = result.getValue();//ValueFormatUtil.valueStr(result.getValue(), format);
126 value = "<no value>";
128 el.setHint(MonitorClass.KEY_MONITOR_COMPONENT, result.getMonitorVariable().getMonitorComponent());
129 ElementUtils.setOrRemoveHint(el, MonitorClass.KEY_MONITOR_IS_EXTERNAL, result.getMonitorVariable().isExternal());
131 el.removeHint(MonitorClass.KEY_MONITOR_COMPONENT);
132 el.removeHint(MonitorClass.KEY_MONITOR_IS_EXTERNAL);
135 substitutions.put("#v1", value);
137 final Map<String, String> subs = el.getHint(MonitorClass.KEY_MONITOR_SUBSTITUTIONS);
138 if (substitutions != subs)
139 el.setHint(MonitorClass.KEY_MONITOR_SUBSTITUTIONS, substitutions);
141 //System.out.println("REPLACING #v1: " + substitutions.get("#v1"));
143 el.setHint(Hints.KEY_DIRTY, Hints.VALUE_SG_DELAYED_UPDATE);
147 public void exception(Throwable t) {
148 ErrorLogger.defaultLogError(t);
152 public boolean isDisposed() {
153 return canvas == null || diagram == null || canvas.isDisposed();
157 public int hashCode() {
158 return element.hashCode();
162 public boolean equals(Object obj) {
167 if (getClass() != obj.getClass())
169 MonitorListener other = (MonitorListener) obj;
170 return element.equals(other.element);