X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Fadapter%2FElementFactoryUtil.java;h=f05667cb6882b73f6faab486473f9ec836e9e98d;hb=617b9475710b80a125597f222f9777224972ce72;hp=c44d5df16f75a4b705741a31b3e5dce3cf6f3a40;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ElementFactoryUtil.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ElementFactoryUtil.java index c44d5df16..f05667cb6 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ElementFactoryUtil.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/ElementFactoryUtil.java @@ -1,120 +1,128 @@ -/******************************************************************************* - * 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.adapter; - -import java.awt.geom.AffineTransform; - -import org.simantics.databoard.Bindings; -import org.simantics.db.AsyncReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.common.procedure.adapter.AsyncProcedureAdapter; -import org.simantics.db.procedure.AsyncProcedure; -import org.simantics.diagram.stubs.G2DResource; -import org.simantics.diagram.synchronization.ErrorHandler; -import org.simantics.diagram.synchronization.ISynchronizationContext; -import org.simantics.diagram.synchronization.SynchronizationHints; -import org.simantics.diagram.synchronization.graph.DiagramGraphUtil; -import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints; -import org.simantics.diagram.synchronization.graph.layer.GraphLayerManager; -import org.simantics.g2d.diagram.DiagramHints; -import org.simantics.g2d.diagram.IDiagram; -import org.simantics.g2d.element.ElementUtils; -import org.simantics.g2d.element.IElement; -import org.simantics.g2d.layers.ILayersEditor; -import org.simantics.utils.datastructures.hints.IHintContext.Key; - -public class ElementFactoryUtil { - - public static void readTransform(AsyncReadGraph graph, final Resource resource, final IElement e) { - readTransform(graph, resource, e, new AsyncProcedureAdapter() { - @Override - public void exception(AsyncReadGraph graph, Throwable t) { - // Nowhere to log properly. - t.printStackTrace(); - } - }); - } - - public static void readTransform(AsyncReadGraph graph, final Resource resource, final IElement e, final AsyncProcedure procedure) { - G2DResource G2D = graph.getService(G2DResource.class); - graph.forPossibleRelatedValue(resource, G2D.HasTransform, Bindings.DOUBLE_ARRAY, new AsyncProcedure() { - @Override - public void exception(AsyncReadGraph graph, Throwable throwable) { - procedure.exception(graph, throwable); - } - - @Override - public void execute(AsyncReadGraph graph, double[] mat) { - mat = DiagramGraphUtil.validateAffineTransform(resource, mat); - AffineTransform tr = mat != null ? new AffineTransform(mat) : new AffineTransform(); - ElementUtils.setTransform(e, tr); - procedure.execute(graph, e); - } - }); - } - - public static ISynchronizationContext getContext(IDiagram diagram) { - return diagram.getHint(SynchronizationHints.CONTEXT); - } - - public static ISynchronizationContext getContextChecked(IDiagram diagram) { - ISynchronizationContext context = getContext(diagram); - if (context == null) - throw new IllegalStateException("diagram does not contain " + SynchronizationHints.CONTEXT + " hint"); - return context; - } - - public static ErrorHandler getErrorHandler(IDiagram diagram) { - ISynchronizationContext context = getContext(diagram); - if (context == null) - return null; - return context.get(SynchronizationHints.ERROR_HANDLER); - } - - public static ErrorHandler getErrorHandlerChecked(IDiagram diagram) { - ISynchronizationContext context = getContextChecked(diagram); - ErrorHandler errorHandler = context.get(SynchronizationHints.ERROR_HANDLER); - if (errorHandler == null) - throw new IllegalStateException("synchronization context " + context + " does not contain " + SynchronizationHints.ERROR_HANDLER + " hint"); - return errorHandler; - } - - public static T getContextHint(IDiagram diagram, Key key) { - ISynchronizationContext context = getContext(diagram); - if (context == null) - return null; - return context.get(key); - } - - public static T getContextHintChecked(IDiagram diagram, Key key) { - return getContextChecked(diagram).get(key); - } - - public static void loadLayersForElement(AsyncReadGraph graph, IDiagram diagram, IElement e, Resource element, - AsyncProcedure callback) { - final GraphLayerManager layerManager = getContextHint(diagram, GraphSynchronizationHints.GRAPH_LAYER_MANAGER); - if (layerManager != null) - loadLayersForElement(graph, layerManager, diagram, e, element, callback); - else - callback.execute(graph, e); - } - - public static void loadLayersForElement(AsyncReadGraph graph, GraphLayerManager layerManager, IDiagram diagram, IElement e, Resource element, - AsyncProcedure callback) { - final ILayersEditor layers = diagram.getHint(DiagramHints.KEY_LAYERS_EDITOR); - if (layers != null) - layerManager.loadLayersForElement(graph, layers, e, element, callback); - else - callback.execute(graph, e); - } - -} +/******************************************************************************* + * 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.adapter; + +import java.awt.geom.AffineTransform; +import java.util.Map; + +import org.simantics.db.AsyncReadGraph; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.procedure.adapter.AsyncProcedureAdapter; +import org.simantics.db.common.procedure.adapter.ProcedureAdapter; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.procedure.AsyncProcedure; +import org.simantics.diagram.stubs.DiagramResource; +import org.simantics.diagram.synchronization.ErrorHandler; +import org.simantics.diagram.synchronization.ISynchronizationContext; +import org.simantics.diagram.synchronization.SynchronizationHints; +import org.simantics.diagram.synchronization.graph.DiagramGraphUtil; +import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints; +import org.simantics.diagram.synchronization.graph.layer.GraphLayerManager; +import org.simantics.g2d.diagram.DiagramHints; +import org.simantics.g2d.diagram.IDiagram; +import org.simantics.g2d.element.ElementUtils; +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.layers.ILayersEditor; +import org.simantics.utils.datastructures.hints.IHintContext.Key; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ElementFactoryUtil { + + private static final Logger LOGGER = LoggerFactory.getLogger(ElementFactoryUtil.class); + + public static void readTransform(AsyncReadGraph graph, final Resource resource, final IElement e) { + readTransform(graph, resource, e, new AsyncProcedureAdapter() { + @Override + public void exception(AsyncReadGraph graph, Throwable t) { + LOGGER.error("Error reading transform from " + resource + " into element " + e, t); + } + }); + } + + public static void readTransform(AsyncReadGraph graph, final Resource resource, final IElement e, final AsyncProcedure procedure) { + DiagramResource DIA = graph.getService(DiagramResource.class); + try { + AffineTransform tr = DiagramGraphUtil.getDynamicAffineTransform((ReadGraph)graph, null, resource, DIA.HasDynamicTransform, true); + ElementUtils.setTransform(e, tr); + } catch (DatabaseException e1) { + ElementUtils.setTransform(e, new AffineTransform()); + } + procedure.execute(graph, e); + } + + public static void readParameters(AsyncReadGraph graph, final Resource resource, final IElement e) { + graph.asyncRequest(new ElementParameterRequest(resource), new ProcedureAdapter>() { + @Override + public void execute(Map result) { + ElementUtils.setParameters(e, result); + } + }); + } + + public static ISynchronizationContext getContext(IDiagram diagram) { + return diagram.getHint(SynchronizationHints.CONTEXT); + } + + public static ISynchronizationContext getContextChecked(IDiagram diagram) { + ISynchronizationContext context = getContext(diagram); + if (context == null) + throw new IllegalStateException("diagram does not contain " + SynchronizationHints.CONTEXT + " hint"); + return context; + } + + public static ErrorHandler getErrorHandler(IDiagram diagram) { + ISynchronizationContext context = getContext(diagram); + if (context == null) + return null; + return context.get(SynchronizationHints.ERROR_HANDLER); + } + + public static ErrorHandler getErrorHandlerChecked(IDiagram diagram) { + ISynchronizationContext context = getContextChecked(diagram); + ErrorHandler errorHandler = context.get(SynchronizationHints.ERROR_HANDLER); + if (errorHandler == null) + throw new IllegalStateException("synchronization context " + context + " does not contain " + SynchronizationHints.ERROR_HANDLER + " hint"); + return errorHandler; + } + + public static T getContextHint(IDiagram diagram, Key key) { + ISynchronizationContext context = getContext(diagram); + if (context == null) + return null; + return context.get(key); + } + + public static T getContextHintChecked(IDiagram diagram, Key key) { + return getContextChecked(diagram).get(key); + } + + public static void loadLayersForElement(AsyncReadGraph graph, IDiagram diagram, IElement e, Resource element, + AsyncProcedure callback) { + final GraphLayerManager layerManager = getContextHint(diagram, GraphSynchronizationHints.GRAPH_LAYER_MANAGER); + if (layerManager != null) + loadLayersForElement(graph, layerManager, diagram, e, element, callback); + else + callback.execute(graph, e); + } + + public static void loadLayersForElement(AsyncReadGraph graph, GraphLayerManager layerManager, IDiagram diagram, IElement e, Resource element, + AsyncProcedure callback) { + final ILayersEditor layers = diagram.getHint(DiagramHints.KEY_LAYERS_EDITOR); + if (layers != null) + layerManager.loadLayersForElement(graph, layers, e, element, callback); + else + callback.execute(graph, e); + } + +}