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.scenegraph.profile.common;
14 import java.util.Collection;
15 import java.util.HashMap;
17 import java.util.concurrent.TimeUnit;
19 import org.simantics.Simantics;
20 import org.simantics.db.AsyncRequestProcessor;
21 import org.simantics.db.Resource;
22 import org.simantics.db.Session;
23 import org.simantics.db.common.session.SessionEventListenerAdapter;
24 import org.simantics.db.common.utils.Logger;
25 import org.simantics.db.procedure.Procedure;
26 import org.simantics.db.service.SessionEventSupport;
27 import org.simantics.scenegraph.INode;
28 import org.simantics.scenegraph.g2d.G2DSceneGraph;
29 import org.simantics.scenegraph.profile.EvaluationContext;
30 import org.simantics.scenegraph.profile.ProfileEntry;
31 import org.simantics.scenegraph.profile.impl.DebugPolicy;
32 import org.simantics.scenegraph.profile.impl.ProfileActivationListener;
33 import org.simantics.scenegraph.profile.request.RuntimeProfileActiveEntries;
34 import org.simantics.utils.datastructures.disposable.IDisposable;
35 import org.simantics.utils.threads.IThreadWorkQueue;
36 import org.simantics.utils.threads.ThreadUtils;
38 public class ProfileObserver implements EvaluationContext {
40 private final Session session;
43 * Runtime diagram resource.
45 private final Resource resource;
47 private final IDisposable canvas;
48 private final IThreadWorkQueue thread;
49 @SuppressWarnings("unused")
50 private final Object diagram;
51 private final Runnable notification;
52 private final G2DSceneGraph sceneGraph;
54 private boolean dirty = true;
55 private boolean disposed = false;
57 private ProfileActivationListener activationListener;
59 private Map<String, Object> constants = new HashMap<String, Object>();
61 private Map<INode, Map<String, Object>> temporaryProperties = new HashMap<INode, Map<String, Object>>();
62 private Map<INode, Map<String, Object>> properties = new HashMap<INode, Map<String, Object>>();
64 private final SessionEventListenerAdapter transactionListener = new SessionEventListenerAdapter() {
66 public void writeTransactionFinished() {
73 public void readTransactionFinished() {
81 public ProfileObserver(Session session, Resource resource, IThreadWorkQueue thread, IDisposable canvas, G2DSceneGraph sceneGraph, Object diagram, Map<String, Object> constants, Runnable notification) {
82 //System.out.println(this + " NEW PROFILE OBSERVER: ");
83 this.session = session;
84 this.resource = resource;
87 this.diagram = diagram;
88 this.sceneGraph = sceneGraph;
89 this.constants.putAll(constants);
90 this.notification = notification;
92 attachSessionListener();
94 if (DebugPolicy.DEBUG_PROFILE_OBSERVER_PERFORM)
95 System.out.println("ProfileObserver(" + this + ")");
97 // Tell SceneGraph that this observer is not yet done applying its operations
98 if(sceneGraph != null)
99 sceneGraph.setPending(ProfileObserver.this);
103 private void attachSessionListener() {
104 SessionEventSupport eventSupport = session.getService(SessionEventSupport.class);
105 eventSupport.addListener(transactionListener);
108 private void detachSessionListener() {
109 SessionEventSupport eventSupport = session.getService(SessionEventSupport.class);
110 eventSupport.removeListener(transactionListener);
113 public void dispose() {
114 synchronized (this) {
120 if (DebugPolicy.DEBUG_PROFILE_OBSERVER_PERFORM)
121 System.out.println("ProfileObserver.dispose(" + this + ")");
123 if(activationListener != null) {
124 activationListener.cleanup();
125 activationListener = null;
128 detachSessionListener();
132 public void update() {
133 if (DebugPolicy.DEBUG_PROFILE_OBSERVER_UPDATE)
134 System.out.println("Profile observer marked dirty.");
138 private void perform() {
140 if (DebugPolicy.DEBUG_PROFILE_OBSERVER_UPDATE)
141 System.out.println("Profile observer detected a change.");
143 session.asyncRequest(new RuntimeProfileActiveEntries(resource), new Procedure<Collection<ProfileEntry>>() {
145 public void execute(final Collection<ProfileEntry> entries) {
150 ThreadUtils.asyncExec(thread, new Runnable() {
152 // private void init(INode node) {
153 // //ProfileVariables.init(node, ProfileObserver.this);
154 //// NodeUtil.forChildren(node, new NodeProcedure<Object>() {
156 //// public Object execute(INode node, String id) {
169 temporaryProperties.clear();
173 // for(IElement e : diagram.getElements()) {
174 // Node node = NodeUtils.
175 // Variables.init(e, ProfileObserver.this);
178 for(ProfileEntry e : entries) {
179 if (DebugPolicy.DEBUG_PROFILE_OBSERVER_PERFORM)
180 System.out.println("Apply profile entry: " + e);
181 e.apply(ProfileObserver.this);
185 sceneGraph.setPending(ProfileObserver.this);
186 // System.err.println("setPending, dirty=true");
189 sceneGraph.clearPending(ProfileObserver.this);
190 // System.err.println("clearPending, dirty=false");
194 // canvas.getContentContext().setDirty();
196 // Something is underway, schedule update
198 Simantics.async(new Runnable() {
203 if (isDisposed()) return;
209 }, 100, TimeUnit.MILLISECONDS);
217 public void exception(Throwable t) {
218 Logger.defaultLogError(t);
224 public boolean isDisposed() {
225 return disposed || canvas.isDisposed();
229 public void exception(Throwable throwable) {
230 Logger.defaultLogError(throwable);
233 @SuppressWarnings("unchecked")
235 public <T> T getTemporaryProperty(INode element, String key) {
236 Map<String, Object> map = temporaryProperties.get(element);
237 T t = map == null ? null : (T) map.get(key);
238 //System.out.println(this + ".getTemporaryProperty(" + element + ", " + key + "): " + t);
243 public <T> void setTemporaryProperty(INode element, String key, T value) {
244 //System.out.println(this + ".setTemporaryProperty(" + element + ", " + key + ", " + value + ")");
245 Map<String, Object> map = temporaryProperties.get(element);
249 map = new HashMap<String, Object>(8);
250 temporaryProperties.put(element, map);
255 properties.remove(element);
260 @SuppressWarnings("unchecked")
262 public <T> T getProperty(INode element, String key) {
263 Map<String, Object> map = properties.get(element);
264 T t = map == null ? null : (T) map.get(key);
265 //System.out.println(this + ".getProperty(" + element + ", " + key + "): " + t);
269 @SuppressWarnings("unchecked")
271 public <T> T setProperty(INode element, String key, T value) {
273 //System.out.println(this + ".setProperty(" + element + ", " + key + ", " + value + ")");
274 Map<String, Object> map = properties.get(element);
278 map = new HashMap<String, Object>(8);
279 properties.put(element, map);
282 result = (T) map.remove(key);
284 properties.remove(element);
286 result = (T) map.put(key, value);
290 @SuppressWarnings("unchecked")
292 public <T> T getConstant(String key) {
293 return (T) constants.get(key);
297 public String toString() {
298 return "ProfileObserver[" + resource.getResourceId() + "]";
302 // public ICanvasContext getContext() {
307 // public IDiagram getDiagram() {
311 public void listen(AsyncRequestProcessor processor, IDisposable disposable) {
312 activationListener = new ProfileActivationListener(resource, this, disposable);
313 processor.asyncRequest(new RuntimeProfileActiveEntries(resource), activationListener);
317 public Resource getResource() {
322 public G2DSceneGraph getSceneGraph() {