]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/content/DiagramContentTracker.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / content / DiagramContentTracker.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.content;
13
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;
22
23 /**
24  * A utility for requesting a diagram's contents and keeping track of them and
25  * obtaining differences.
26  * 
27  * <p>
28  * This class is not thread-safe.
29  * 
30  * @author Tuukka Lehtonen
31  */
32 public class DiagramContentTracker {
33
34     private final ErrorHandler errorHandler = new ErrorHandler() {
35         @Override
36         public void warning(String message, Exception e) {
37             ErrorLogger.defaultLogWarning(message, e);
38         }
39         @Override
40         public void error(String message, Throwable t) {
41             ErrorLogger.defaultLogError(message, t);
42         }
43     };
44
45     private DiagramContents       lastContents   = new DiagramContents();
46     private DiagramContentChanges lastDifference = DiagramContentChanges.EMPTY;
47
48     private final ICanvasContext  context;
49     private final Session         session;
50     private final Resource        diagram;
51
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);
55         return tracker;
56     }
57
58     private DiagramContentTracker(ICanvasContext context, Session session, Resource diagram) {
59         if (context == null)
60             throw new NullPointerException("null canvas context");
61         if (session == null)
62             throw new NullPointerException("null session");
63         if (diagram == null)
64             throw new NullPointerException("null diagram");
65         this.context = context;
66         this.session = session;
67         this.diagram = diagram;
68     }
69
70     public DiagramContentChanges update() throws DatabaseException {
71         return update(session);
72     }
73
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;
79         return changes;
80     }
81
82     public DiagramContents getLastContents() {
83         return lastContents;
84     }
85
86     public DiagramContentChanges getLastDifference() {
87         return lastDifference;
88     }
89
90 }