-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.diagram.adapter;\r
-\r
-import java.awt.Point;\r
-import java.awt.geom.Rectangle2D;\r
-\r
-import org.simantics.Simantics;\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.db.AsyncReadGraph;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;\r
-import org.simantics.db.common.procedure.guarded.GuardedAsyncProcedureWrapper;\r
-import org.simantics.db.common.request.BinaryAsyncRead;\r
-import org.simantics.db.common.request.ReadRequest;\r
-import org.simantics.db.common.request.UnaryAsyncRead;\r
-import org.simantics.db.common.utils.NameUtils;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.procedure.AsyncProcedure;\r
-import org.simantics.diagram.stubs.G2DResource;\r
-import org.simantics.diagram.synchronization.SynchronizationHints;\r
-import org.simantics.diagram.synchronization.graph.TransformSynchronizer;\r
-import org.simantics.g2d.canvas.ICanvasContext;\r
-import org.simantics.g2d.diagram.DiagramHints;\r
-import org.simantics.g2d.diagram.IDiagram;\r
-import org.simantics.g2d.element.ElementClass;\r
-import org.simantics.g2d.element.ElementHints;\r
-import org.simantics.g2d.element.IElement;\r
-import org.simantics.g2d.element.handler.impl.DefaultTransform;\r
-import org.simantics.g2d.element.handler.impl.SimpleElementLayers;\r
-import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;\r
-import org.simantics.g2d.element.handler.impl.StaticSymbolImageInitializer;\r
-import org.simantics.g2d.element.handler.impl.StaticSymbolImpl;\r
-import org.simantics.g2d.element.handler.impl.TextImpl;\r
-import org.simantics.g2d.elementclass.ImageClass.ImageElementHandler;\r
-import org.simantics.g2d.image.DefaultImages;\r
-import org.simantics.g2d.image.Image;\r
-import org.simantics.g2d.image.ProviderUtils;\r
-import org.simantics.g2d.svg.SVGImage;\r
-import org.simantics.scenegraph.g2d.nodes.SVGNode;\r
-import org.simantics.utils.Development;\r
-import org.simantics.utils.datastructures.cache.IFactory;\r
-import org.simantics.utils.datastructures.cache.ProvisionException;\r
-import org.simantics.utils.ui.ErrorLogger;\r
-\r
-/**\r
- * An {@link ElementFactory} for DIA.SVGElement instances.\r
- * \r
- * <p>\r
- * Loads element layers, transform and SVG document.\r
- * \r
- * @author Tuukka Lehtonen\r
- */\r
-public class SVGElementClassFactory extends ElementFactoryAdapter {\r
-\r
- public static final ElementFactory INSTANCE = new SVGElementClassFactory();\r
-\r
- private static final StaticSymbolImpl STATIC_SYMBOL = new StaticSymbolImpl(DefaultImages.SVG.get());\r
-\r
- static class ClassRequest extends UnaryAsyncRead<Resource, ElementClass> {\r
-\r
- public ClassRequest(Resource elementType) {\r
- super(elementType);\r
- }\r
-\r
- @Override\r
- public void perform(AsyncReadGraph graph, AsyncProcedure<ElementClass> procedure) {\r
- createClass(graph, parameter, procedure);\r
- }\r
-\r
- }\r
-\r
- @Override\r
- public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram,\r
- final Resource elementType, final AsyncProcedure<ElementClass> procedure) {\r
-\r
- graph.asyncRequest(new ClassRequest(elementType), new TransientCacheAsyncListener<ElementClass>() {\r
-\r
- @Override\r
- public void exception(AsyncReadGraph graph, Throwable t) {\r
- procedure.exception(graph, t);\r
- }\r
-\r
- @Override\r
- public void execute(AsyncReadGraph graph, ElementClass result) {\r
- procedure.execute(graph, result);\r
- }\r
-\r
- });\r
-\r
- }\r
-\r
- static void createClass(AsyncReadGraph graph, final Resource elementType, final AsyncProcedure<ElementClass> procedure) {\r
- String id = "SVGElement: " + elementType.getResourceId();\r
- procedure.execute(graph, ElementClass.compile(\r
- TextImpl.INSTANCE,\r
- new StaticObjectAdapter(elementType),\r
- DefaultTransform.INSTANCE,\r
- StaticSymbolImageInitializer.INSTANCE,\r
- STATIC_SYMBOL,\r
- ImageElementHandler.INSTANCE,\r
- SimpleElementLayers.INSTANCE)\r
- .setId(id));\r
- }\r
-\r
- @Override\r
- public void load(AsyncReadGraph graph, final ICanvasContext canvas, final IDiagram diagram, final Resource element, final IElement e, final AsyncProcedure<IElement> procedure) {\r
- e.setHint(SynchronizationHints.HINT_SYNCHRONIZER, TransformSynchronizer.INSTANCE);\r
-\r
- final GuardedAsyncProcedureWrapper<IElement> guard = new GuardedAsyncProcedureWrapper<IElement>(procedure, 3);\r
-\r
- loadSVG(graph, diagram, element, e, guard);\r
- ElementFactoryUtil.loadLayersForElement(graph, diagram, e, element, guard);\r
- ElementFactoryUtil.readTransform(graph, element, e, guard);\r
- }\r
-\r
- private void loadSVG(AsyncReadGraph graph, final IDiagram diagram, final Resource element, final IElement e, final AsyncProcedure<IElement> guard) {\r
- G2DResource G2D = graph.getService(G2DResource.class);\r
-\r
- graph.forPossibleRelatedValue(element, G2D.HasSVGDocument, Bindings.STRING, new AsyncProcedure<String>() {\r
- @Override\r
- public void exception(AsyncReadGraph graph, Throwable throwable) {\r
- guard.exception(graph, throwable);\r
- }\r
-\r
- @Override\r
- public void execute(AsyncReadGraph graph, final String svgDocument) {\r
- if (svgDocument != null) {\r
-\r
- if(Development.DEVELOPMENT) {\r
- Rectangle2D bounds = SVGNode.getRealBounds(svgDocument);\r
- if(bounds != null && bounds.isEmpty()) {\r
- Simantics.async(new ReadRequest() {\r
- @Override \r
- public void run(ReadGraph graph) throws DatabaseException {\r
- System.err.println("Graphics for element " + NameUtils.getSafeName(graph, element) + " are empty.");\r
- }\r
- });\r
- }\r
- }\r
-\r
- Point referenceSize = diagram.getHint(DiagramHints.KEY_ELEMENT_RASTER_TARGET_SIZE);\r
- IFactory<Image> img = SVGImage.createFactoryFromString(String.valueOf(element.getResourceId()), svgDocument, referenceSize);\r
- try {\r
- img = ProviderUtils.rasterize(img);\r
- e.setHint(ElementHints.KEY_IMAGE, img.get());\r
- guard.execute(graph, e);\r
- } catch (ProvisionException ex) {\r
- ErrorLogger.defaultLogError(ex);\r
- e.setHint(ElementHints.KEY_IMAGE, DefaultImages.SVG.get());\r
- guard.execute(graph, e);\r
- }\r
- } else {\r
- e.setHint(ElementHints.KEY_IMAGE, DefaultImages.UNKNOWN.get());\r
- guard.execute(graph, e);\r
- }\r
- }\r
- });\r
- }\r
-\r
+/*******************************************************************************
+ * 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.Point;
+import java.awt.geom.Rectangle2D;
+
+import org.simantics.Simantics;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
+import org.simantics.db.common.procedure.guarded.GuardedAsyncProcedureWrapper;
+import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.common.request.UnaryAsyncRead;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.procedure.AsyncProcedure;
+import org.simantics.diagram.elements.SVGImage;
+import org.simantics.diagram.stubs.G2DResource;
+import org.simantics.diagram.synchronization.SynchronizationHints;
+import org.simantics.diagram.synchronization.graph.TransformSynchronizer;
+import org.simantics.g2d.canvas.ICanvasContext;
+import org.simantics.g2d.diagram.DiagramHints;
+import org.simantics.g2d.diagram.IDiagram;
+import org.simantics.g2d.element.ElementClass;
+import org.simantics.g2d.element.ElementHints;
+import org.simantics.g2d.element.IElement;
+import org.simantics.g2d.element.handler.impl.DefaultTransform;
+import org.simantics.g2d.element.handler.impl.SimpleElementLayers;
+import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;
+import org.simantics.g2d.element.handler.impl.StaticSymbolImageInitializer;
+import org.simantics.g2d.element.handler.impl.StaticSymbolImpl;
+import org.simantics.g2d.element.handler.impl.TextImpl;
+import org.simantics.g2d.elementclass.ImageClass.ImageElementHandler;
+import org.simantics.g2d.image.DefaultImages;
+import org.simantics.g2d.image.Image;
+import org.simantics.g2d.image.ProviderUtils;
+import org.simantics.scenegraph.g2d.nodes.SVGNode;
+import org.simantics.utils.Development;
+import org.simantics.utils.datastructures.cache.IFactory;
+import org.simantics.utils.datastructures.cache.ProvisionException;
+import org.simantics.utils.ui.ErrorLogger;
+
+/**
+ * An {@link ElementFactory} for DIA.SVGElement instances.
+ *
+ * <p>
+ * Loads element layers, transform and SVG document.
+ *
+ * @author Tuukka Lehtonen
+ */
+public class SVGElementClassFactory extends ElementFactoryAdapter {
+
+ public static final ElementFactory INSTANCE = new SVGElementClassFactory();
+
+ private static final StaticSymbolImpl STATIC_SYMBOL = new StaticSymbolImpl(DefaultImages.SVG.get());
+
+ static class ClassRequest extends UnaryAsyncRead<Resource, ElementClass> {
+
+ public ClassRequest(Resource elementType) {
+ super(elementType);
+ }
+
+ @Override
+ public void perform(AsyncReadGraph graph, AsyncProcedure<ElementClass> procedure) {
+ createClass(graph, parameter, procedure);
+ }
+
+ }
+
+ @Override
+ public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram,
+ final Resource elementType, final AsyncProcedure<ElementClass> procedure) {
+
+ graph.asyncRequest(new ClassRequest(elementType), new TransientCacheAsyncListener<ElementClass>() {
+
+ @Override
+ public void exception(AsyncReadGraph graph, Throwable t) {
+ procedure.exception(graph, t);
+ }
+
+ @Override
+ public void execute(AsyncReadGraph graph, ElementClass result) {
+ procedure.execute(graph, result);
+ }
+
+ });
+
+ }
+
+ static void createClass(AsyncReadGraph graph, final Resource elementType, final AsyncProcedure<ElementClass> procedure) {
+ String id = "SVGElement: " + elementType.getResourceId();
+ procedure.execute(graph, ElementClass.compile(
+ TextImpl.INSTANCE,
+ new StaticObjectAdapter(elementType),
+ DefaultTransform.INSTANCE,
+ StaticSymbolImageInitializer.INSTANCE,
+ STATIC_SYMBOL,
+ ImageElementHandler.INSTANCE,
+ SimpleElementLayers.INSTANCE)
+ .setId(id));
+ }
+
+ @Override
+ public void load(AsyncReadGraph graph, final ICanvasContext canvas, final IDiagram diagram, final Resource element, final IElement e, final AsyncProcedure<IElement> procedure) {
+ e.setHint(SynchronizationHints.HINT_SYNCHRONIZER, TransformSynchronizer.INSTANCE);
+
+ final GuardedAsyncProcedureWrapper<IElement> guard = new GuardedAsyncProcedureWrapper<IElement>(procedure, 3);
+
+ loadSVG(graph, diagram, element, e, guard);
+ ElementFactoryUtil.loadLayersForElement(graph, diagram, e, element, guard);
+ ElementFactoryUtil.readTransform(graph, element, e, guard);
+ }
+
+ private void loadSVG(AsyncReadGraph graph, final IDiagram diagram, final Resource element, final IElement e, final AsyncProcedure<IElement> guard) {
+ G2DResource G2D = graph.getService(G2DResource.class);
+
+ graph.forPossibleRelatedValue(element, G2D.HasSVGDocument, Bindings.STRING, new AsyncProcedure<String>() {
+ @Override
+ public void exception(AsyncReadGraph graph, Throwable throwable) {
+ guard.exception(graph, throwable);
+ }
+
+ @Override
+ public void execute(AsyncReadGraph graph, final String svgDocument) {
+ if (svgDocument != null) {
+
+ if(Development.DEVELOPMENT) {
+ Rectangle2D bounds = SVGNode.getRealBounds(svgDocument);
+ if(bounds != null && bounds.isEmpty()) {
+ Simantics.async(new ReadRequest() {
+ @Override
+ public void run(ReadGraph graph) throws DatabaseException {
+ System.err.println("Graphics for element " + NameUtils.getSafeName(graph, element) + " are empty.");
+ }
+ });
+ }
+ }
+
+ Point referenceSize = diagram.getHint(DiagramHints.KEY_ELEMENT_RASTER_TARGET_SIZE);
+ IFactory<Image> img = SVGImage.createFactoryFromString(String.valueOf(element.getResourceId()), svgDocument, referenceSize);
+ try {
+ img = ProviderUtils.rasterize(img);
+ e.setHint(ElementHints.KEY_IMAGE, img.get());
+ guard.execute(graph, e);
+ } catch (ProvisionException ex) {
+ ErrorLogger.defaultLogError(ex);
+ e.setHint(ElementHints.KEY_IMAGE, DefaultImages.SVG.get());
+ guard.execute(graph, e);
+ }
+ } else {
+ e.setHint(ElementHints.KEY_IMAGE, DefaultImages.UNKNOWN.get());
+ guard.execute(graph, e);
+ }
+ }
+ });
+ }
+