+package org.simantics.diagram.export;\r
+\r
+import java.awt.Point;\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.image.BufferedImage;\r
+import java.io.File;\r
+import java.util.concurrent.Semaphore;\r
+import java.util.concurrent.atomic.AtomicReference;\r
+\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.SubMonitor;\r
+import org.simantics.Simantics;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.ResourceArray;\r
+import org.simantics.db.common.request.PossibleIndexRoot;\r
+import org.simantics.db.common.request.UniqueRead;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.ValidationException;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.diagram.elements.DiagramNodeUtil;\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.canvas.impl.CanvasContext;\r
+import org.simantics.g2d.chassis.ICanvasChassis;\r
+import org.simantics.g2d.chassis.SWTChassis;\r
+import org.simantics.g2d.participant.TransformUtil;\r
+import org.simantics.g2d.scenegraph.ICanvasSceneGraphProvider;\r
+import org.simantics.g2d.scenegraph.SceneGraphConstants;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.scenegraph.g2d.nodes.NavigationNode;\r
+import org.simantics.scenegraph.utils.NodeUtil;\r
+import org.simantics.structural2.StructuralVariables;\r
+import org.simantics.utils.DataContainer;\r
+import org.simantics.utils.datastructures.Pair;\r
+import org.simantics.utils.threads.AWTThread;\r
+import org.simantics.utils.threads.IThreadWorkQueue;\r
+import org.simantics.utils.threads.ThreadUtils;\r
+import org.simantics.utils.threads.WorkerThread;\r
+\r
+public class ImagePrinter {\r
+\r
+ public static class ImageExportPlan {\r
+ public File exportLocation;\r
+ public String name;\r
+ public Resource diagram;\r
+\r
+ // use dpi or size, not both.\r
+ public Double dpi;\r
+ public Point size;\r
+ public double margin;\r
+ }\r
+\r
+ /**\r
+ * @param monitor\r
+ * the progress monitor to use for reporting progress to the\r
+ * user. It is the caller's responsibility to call done() on the\r
+ * given monitor. Accepts <code>null</code>, indicating that no\r
+ * progress should be reported and that the operation cannot be\r
+ * cancelled.\r
+ * \r
+ * @param exportPath\r
+ * @throws Exception\r
+ */\r
+ public static BufferedImage printToImage(IProgressMonitor monitor, ImageExportPlan exportPlan) throws Exception {\r
+ \r
+\r
+ SubMonitor progress = SubMonitor.convert(monitor, "Export to Image", 1);\r
+\r
+ WorkerThread workerThread = new WorkerThread("Diagram Image Painter");\r
+ workerThread.start();\r
+ String loc;\r
+ if (exportPlan.exportLocation == null)\r
+ loc = "clipboard";\r
+ else\r
+ loc = exportPlan.exportLocation.getAbsolutePath();\r
+ progress.beginTask("Writing " + exportPlan.name + " to " + loc, 1);\r
+ \r
+ return render(workerThread, Simantics.getSessionContext(), exportPlan);\r
+ \r
+\r
+ }\r
+\r
+ /**\r
+ * Renders diagram to BufferedImage\r
+ * \r
+ * @param thread\r
+ * @param sessionContext\r
+ * @param exportPlan\r
+ * @return\r
+ * @throws Exception\r
+ */\r
+ public static BufferedImage render(\r
+ final IThreadWorkQueue thread,\r
+ final ISessionContext sessionContext,\r
+ final ImageExportPlan exportPlan)\r
+ throws Exception\r
+ {\r
+ final DataContainer<BufferedImage> result = new DataContainer<BufferedImage>(null);\r
+ final DataContainer<Exception> exception = new DataContainer<Exception>();\r
+\r
+ final CanvasContext ctx = new CanvasContext(thread);\r
+ final AtomicReference<ICanvasSceneGraphProvider> sgProvider = new AtomicReference<ICanvasSceneGraphProvider>();\r
+\r
+ try {\r
+ final Semaphore done = new Semaphore(0);\r
+ // IMPORTANT: Load diagram in a different thread than the canvas context thread!\r
+ ThreadUtils.getBlockingWorkExecutor().execute(new Runnable() {\r
+ @Override\r
+ public void run() {\r
+ try {\r
+ Pair<Resource, String> modelAndRVI = sessionContext.getSession().syncRequest(new UniqueRead<Pair<Resource, String>>() {\r
+ @Override\r
+ public Pair<Resource, String> perform(ReadGraph graph) throws DatabaseException {\r
+ return new Pair<Resource, String>( resolveModel(graph, exportPlan.diagram ), resolveRVI(graph, exportPlan.diagram) );\r
+ }\r
+ });\r
+\r
+ ICanvasSceneGraphProvider provider = DiagramNodeUtil.loadSceneGraphProvider(ctx, modelAndRVI.first, exportPlan.diagram, modelAndRVI.second);\r
+ sgProvider.set( provider );\r
+ \r
+ ThreadUtils.asyncExec(thread, new Runnable() {\r
+ @Override\r
+ public void run() {\r
+ try {\r
+ ImageBuilder chassis = new ImageBuilder(exportPlan.exportLocation,exportPlan.dpi,exportPlan.size,exportPlan.margin);\r
+ \r
+ result.set(chassis.paint(ctx));\r
+ } catch (Exception e) {\r
+ exception.set(e);\r
+ } finally {\r
+ done.release();\r
+ }\r
+ }\r
+ });\r
+ } catch (DatabaseException e) {\r
+ done.release();\r
+ exception.set(e);\r
+ } catch (Throwable e) {\r
+ done.release();\r
+ exception.set(new DatabaseException(e));\r
+ } finally {\r
+ done.release();\r
+ }\r
+ }\r
+ });\r
+\r
+ done.acquire(2);\r
+ if (exception.get() != null)\r
+ throw exception.get();\r
+ return result.get();\r
+ } finally {\r
+ if (sgProvider.get() != null)\r
+ sgProvider.get().dispose();\r
+ ctx.dispose();\r
+ }\r
+ }\r
+ \r
+ /**\r
+ * Renders diagram to BufferedImage using existing CanvasContext.\r
+ * \r
+ * Note: While the method tries to restore setting as they were, there may be visible side effects in the diagram. \r
+ * \r
+ * @param context\r
+ * @param chassis\r
+ * @param exportPlan\r
+ * @return\r
+ * @throws Exception\r
+ */\r
+ public static BufferedImage renderLocal(final ICanvasContext context, final ICanvasChassis chassis, final ImageExportPlan exportPlan) throws Exception {\r
+ final Semaphore done = new Semaphore(0);\r
+ final DataContainer<BufferedImage> result = new DataContainer<BufferedImage>(null);\r
+ final DataContainer<Exception> exception = new DataContainer<Exception>(null);\r
+ ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {\r
+\r
+ @Override\r
+ public void run() {\r
+ try {\r
+ result.set(renderLocal(context, exportPlan, chassis));\r
+ } catch (Exception e) {\r
+ exception.set(e);\r
+ } finally {\r
+ done.release();\r
+ }\r
+ }\r
+ });\r
+ done.acquire();\r
+ if (exception.get() != null)\r
+ throw exception.get();\r
+ return result.get();\r
+ }\r
+ \r
+ private static BufferedImage renderLocal(ICanvasContext context, ImagePrinter.ImageExportPlan exportPlan, ICanvasChassis chassis) throws Exception {\r
+ TransformUtil util = context.getSingleItem(TransformUtil.class);\r
+ AffineTransform at = util.getTransform();\r
+ final NavigationNode node = NodeUtil.findNodeById(context.getSceneGraph(), SceneGraphConstants.NAVIGATION_NODE_PATH);\r
+ boolean adapt = true;\r
+ if (node != null) {\r
+ adapt = node.getAdaptViewportToResizedControl();\r
+ // prevent NavigationNode from re-scaling when it detects canvas size change. \r
+ node.setAdaptViewportToResizedControl(false);\r
+ }\r
+\r
+ ImageBuilder builder = new ImageBuilder(exportPlan.exportLocation,exportPlan.dpi,exportPlan.size,exportPlan.margin);\r
+ BufferedImage image = builder.paint(context);\r
+ \r
+ util.setTransform(at);\r
+\r
+ \r
+ if (node != null && adapt != false) {\r
+ if (chassis instanceof SWTChassis) {\r
+ ((SWTChassis) chassis).getAWTComponent().repaint();\r
+ }\r
+ // restore re-scaling setting (need to be scheduled, so that Diagram canvas is re-painted once.\r
+ final boolean b = adapt;\r
+ ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {\r
+ @Override\r
+ public void run() {\r
+ node.setAdaptViewportToResizedControl(b);\r
+ }\r
+ });\r
+ }\r
+ \r
+ return image;\r
+ }\r
+ \r
+ \r
+\r
+ private static Resource resolveModel(ReadGraph graph, Resource diagram) throws DatabaseException {\r
+ ModelingResources mod = ModelingResources.getInstance(graph);\r
+ Resource composite = graph.getSingleObject(diagram, mod.DiagramToComposite);\r
+ Resource model = graph.syncRequest(new PossibleIndexRoot(composite));\r
+ if (model == null)\r
+ throw new ValidationException("no model found for composite " + NameUtils.getSafeName(graph, composite));\r
+ return model;\r
+ }\r
+\r
+\r
+\r
+ private static String resolveRVI(ReadGraph graph, Resource diagram) throws DatabaseException {\r
+ ModelingResources mod = ModelingResources.getInstance(graph);\r
+ Resource composite = graph.getSingleObject(diagram, mod.DiagramToComposite);\r
+ final ResourceArray compositePath = StructuralVariables.getCompositeArray(graph, composite);\r
+ final ResourceArray variablePath = compositePath.removeFromBeginning(1);\r
+ return StructuralVariables.getRVI(graph, variablePath);\r
+ }\r
+\r
+\r
+}\r