X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Fcontent%2FDiagramContentTracker.java;fp=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Fcontent%2FDiagramContentTracker.java;h=1cd506e169b1e5e9238822bf9770045affa8ca06;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/content/DiagramContentTracker.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/content/DiagramContentTracker.java new file mode 100644 index 000000000..1cd506e16 --- /dev/null +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/content/DiagramContentTracker.java @@ -0,0 +1,90 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.diagram.content; + +import org.simantics.db.RequestProcessor; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.exception.DatabaseException; +import org.simantics.diagram.adapter.DiagramContentRequest; +import org.simantics.diagram.synchronization.ErrorHandler; +import org.simantics.g2d.canvas.ICanvasContext; +import org.simantics.utils.ui.ErrorLogger; + +/** + * A utility for requesting a diagram's contents and keeping track of them and + * obtaining differences. + * + *

+ * This class is not thread-safe. + * + * @author Tuukka Lehtonen + */ +public class DiagramContentTracker { + + private final ErrorHandler errorHandler = new ErrorHandler() { + @Override + public void warning(String message, Exception e) { + ErrorLogger.defaultLogWarning(message, e); + } + @Override + public void error(String message, Throwable t) { + ErrorLogger.defaultLogError(message, t); + } + }; + + private DiagramContents lastContents = new DiagramContents(); + private DiagramContentChanges lastDifference = DiagramContentChanges.EMPTY; + + private final ICanvasContext context; + private final Session session; + private final Resource diagram; + + public static DiagramContentTracker start(ICanvasContext context, RequestProcessor processor, Resource diagram) throws DatabaseException { + DiagramContentTracker tracker = new DiagramContentTracker(context, processor.getSession(), diagram); + tracker.update(processor); + return tracker; + } + + private DiagramContentTracker(ICanvasContext context, Session session, Resource diagram) { + if (context == null) + throw new NullPointerException("null canvas context"); + if (session == null) + throw new NullPointerException("null session"); + if (diagram == null) + throw new NullPointerException("null diagram"); + this.context = context; + this.session = session; + this.diagram = diagram; + } + + public DiagramContentChanges update() throws DatabaseException { + return update(session); + } + + public DiagramContentChanges update(RequestProcessor processor) throws DatabaseException { + DiagramContents contents = processor.syncRequest(new DiagramContentRequest(context, diagram, errorHandler)); + DiagramContentChanges changes = contents.differenceFrom(lastContents); + this.lastDifference = changes; + this.lastContents = contents; + return changes; + } + + public DiagramContents getLastContents() { + return lastContents; + } + + public DiagramContentChanges getLastDifference() { + return lastDifference; + } + +}