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.diagram.content;
14 import org.simantics.db.RequestProcessor;
15 import org.simantics.db.Resource;
16 import org.simantics.db.Session;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.diagram.adapter.DiagramContentRequest;
19 import org.simantics.diagram.synchronization.ErrorHandler;
20 import org.simantics.g2d.canvas.ICanvasContext;
21 import org.simantics.utils.ui.ErrorLogger;
24 * A utility for requesting a diagram's contents and keeping track of them and
25 * obtaining differences.
28 * This class is not thread-safe.
30 * @author Tuukka Lehtonen
32 public class DiagramContentTracker {
34 private final ErrorHandler errorHandler = new ErrorHandler() {
36 public void warning(String message, Exception e) {
37 ErrorLogger.defaultLogWarning(message, e);
40 public void error(String message, Throwable t) {
41 ErrorLogger.defaultLogError(message, t);
45 private DiagramContents lastContents = new DiagramContents();
46 private DiagramContentChanges lastDifference = DiagramContentChanges.EMPTY;
48 private final ICanvasContext context;
49 private final Session session;
50 private final Resource diagram;
52 public static DiagramContentTracker start(ICanvasContext context, RequestProcessor processor, Resource diagram) throws DatabaseException {
53 DiagramContentTracker tracker = new DiagramContentTracker(context, processor.getSession(), diagram);
54 tracker.update(processor);
58 private DiagramContentTracker(ICanvasContext context, Session session, Resource diagram) {
60 throw new NullPointerException("null canvas context");
62 throw new NullPointerException("null session");
64 throw new NullPointerException("null diagram");
65 this.context = context;
66 this.session = session;
67 this.diagram = diagram;
70 public DiagramContentChanges update() throws DatabaseException {
71 return update(session);
74 public DiagramContentChanges update(RequestProcessor processor) throws DatabaseException {
75 DiagramContents contents = processor.syncRequest(new DiagramContentRequest(context, diagram, errorHandler));
76 DiagramContentChanges changes = contents.differenceFrom(lastContents);
77 this.lastDifference = changes;
78 this.lastContents = contents;
82 public DiagramContents getLastContents() {
86 public DiagramContentChanges getLastDifference() {
87 return lastDifference;