]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/pdf/PDFPainter.java
Merge branch 'feature/funcwrite'
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / pdf / PDFPainter.java
index 85cf4bc08a801fe109b2ce5bf8bc1514b3d159e5..1adcf25c23d33a2b2f7ede11eaf9210697097fb6 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2017 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
@@ -8,14 +8,15 @@
  *
  * Contributors:
  *     VTT Technical Research Centre of Finland - initial API and implementation
+ *     Semantum Oy - (#7084) refactoring
  *******************************************************************************/
 package org.simantics.modeling.ui.pdf;
 
 import java.util.concurrent.Semaphore;
-import java.util.concurrent.atomic.AtomicReference;
 
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
+import org.simantics.db.Session;
 import org.simantics.db.common.request.PossibleIndexRoot;
 import org.simantics.db.common.request.UniqueRead;
 import org.simantics.db.common.utils.NameUtils;
@@ -23,7 +24,7 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.ValidationException;
 import org.simantics.db.layer0.variable.Variable;
 import org.simantics.db.layer0.variable.Variables;
-import org.simantics.db.management.ISessionContext;
+import org.simantics.db.request.Read;
 import org.simantics.diagram.elements.DiagramNodeUtil;
 import org.simantics.diagram.stubs.DiagramResource;
 import org.simantics.g2d.canvas.Hints;
@@ -31,7 +32,6 @@ import org.simantics.g2d.canvas.impl.CanvasContext;
 import org.simantics.g2d.scenegraph.ICanvasSceneGraphProvider;
 import org.simantics.modeling.requests.Node;
 import org.simantics.structural.stubs.StructuralResource2;
-import org.simantics.utils.DataContainer;
 import org.simantics.utils.datastructures.Pair;
 import org.simantics.utils.page.PageDesc;
 import org.simantics.utils.threads.IThreadWorkQueue;
@@ -48,9 +48,8 @@ import com.lowagie.text.pdf.PdfWriter;
  */
 public class PDFPainter {
 
-    public static boolean render(
+    public static void render(
             final IThreadWorkQueue thread,
-            final ISessionContext sessionContext,
             PDFExportPlan exportModel,
             final Node node,
             final PdfWriter writer,
@@ -59,105 +58,101 @@ public class PDFPainter {
             final PageDesc pageDesc,
             final boolean fitDiagramContentsToPageMargins,
             long timeout)
-    throws InterruptedException, DatabaseException
+                    throws InterruptedException, DatabaseException
     {
-        final DataContainer<Boolean> result = new DataContainer<Boolean>(false);
-        final DataContainer<DatabaseException> exception = new DataContainer<DatabaseException>();
+        DatabaseException[] exception = { null };
+        ICanvasSceneGraphProvider[] sgProvider = { null };
 
-        final CanvasContext ctx = new CanvasContext(thread);
-        final AtomicReference<ICanvasSceneGraphProvider> sgProvider = new AtomicReference<ICanvasSceneGraphProvider>();
+        CanvasContext ctx = new CanvasContext(thread);
 
         try {
             final Semaphore done = new Semaphore(0);
             // IMPORTANT: Load diagram in a different thread than the canvas context thread!
-            ThreadUtils.getBlockingWorkExecutor().execute(new Runnable() {
-                @Override
-                public void run() {
-                    try {
-                        Pair<Resource, String> modelAndRVI = sessionContext.getSession().syncRequest(new UniqueRead<Pair<Resource, String>>() {
-                            @Override
-                            public Pair<Resource, String> perform(ReadGraph graph) throws DatabaseException {
-                                return new Pair<Resource, String>( resolveModel(graph, node), resolveRVI(graph, node) );
-                            }
-                        });
-                        
-                        final Boolean isSymbol = sessionContext.getSession().syncRequest(new UniqueRead<Boolean>() {
-                            @Override
-                            public Boolean perform(ReadGraph graph) throws DatabaseException {
-                               StructuralResource2 STR = StructuralResource2.getInstance(graph);
-                               DiagramResource DIA = DiagramResource.getInstance(graph);
-                               Resource possibleSymbol = graph.getPossibleObject(node.getDiagramResource(), STR.Defines);
-                               return possibleSymbol != null && graph.isInstanceOf(possibleSymbol, DIA.ElementClass);
-                            }
-                        }); 
-                        
-                        ICanvasSceneGraphProvider provider = DiagramNodeUtil.loadSceneGraphProvider(ctx, modelAndRVI.first, node.getDiagramResource(), modelAndRVI.second, 5000);
-                        sgProvider.set( provider );
-                        ctx.getDefaultHintContext().setHint(Hints.KEY_PAGE_DESC, pageDesc);
-
-//                        StringBuilder b = new StringBuilder();
-//                        NodeUtil.printTreeNodes(ctx.getCanvasNode(), b);
-//                        System.err.println(b.toString());
-
-                        ThreadUtils.asyncExec(thread, new Runnable() {
-                            @Override
-                            public void run() {
-                                try {
-                                    PDFBuilder chassis = new PDFBuilder(writer, mapper, pageSize, pageDesc, fitDiagramContentsToPageMargins || isSymbol);
-                                    
-                                    chassis.paint(ctx, true);
-                                } finally {
-                                    done.release();
-                                }
-                            }
-                        });
-                    } catch (DatabaseException e) {
-                        done.release();
-                        exception.set(e);
-                    } catch (Throwable e) {
-                        done.release();
-                        exception.set(new DatabaseException(e));
-                    } finally {
-                        done.release();
-                    }
+            ThreadUtils.getBlockingWorkExecutor().execute(() -> {
+                try {
+                    Session s = exportModel.sessionContext.getSession();
+
+                    Pair<Resource, String> modelAndRVI = s.syncRequest( modelAndRVI(node) );
+                    Boolean isSymbol = s.syncRequest( isSymbol(node) ); 
+
+                    ICanvasSceneGraphProvider provider = DiagramNodeUtil.loadSceneGraphProvider(
+                            ctx,
+                            modelAndRVI.first,
+                            node.getDiagramResource(),
+                            modelAndRVI.second,
+                            5000);
+                    sgProvider[0] = provider;
+                    ctx.getDefaultHintContext().setHint(Hints.KEY_PAGE_DESC, pageDesc);
+
+//                    System.err.println(NodeUtil.printTreeNodes(ctx.getCanvasNode(), new StringBuilder()).toString());
+
+                    ThreadUtils.asyncExec(thread, () -> {
+                        try {
+                            PDFBuilder chassis = new PDFBuilder(writer, mapper, pageSize, pageDesc, fitDiagramContentsToPageMargins || isSymbol);
+                            chassis.paint(ctx, true);
+                        } catch (Throwable e) {
+                            exception[0] = new DatabaseException(e);
+                        } finally {
+                            done.release();
+                        }
+                    });
+                } catch (DatabaseException e) {
+                    done.release();
+                    exception[0] = e;
+                } catch (Throwable e) {
+                    done.release();
+                    exception[0] = new DatabaseException(e);
+                } finally {
+                    done.release();
                 }
             });
 
             done.acquire(2);
-            if (exception.get() != null)
-                throw exception.get();
-            return result.get();
+            if (exception[0] != null)
+                throw exception[0];
         } finally {
-            if (sgProvider.get() != null)
-                sgProvider.get().dispose();
+            if (sgProvider[0] != null)
+                sgProvider[0].dispose();
             ctx.dispose();
         }
     }
 
+    private static Read<Pair<Resource, String>> modelAndRVI(Node node) {
+        return new UniqueRead<Pair<Resource, String>>() {
+            @Override
+            public Pair<Resource, String> perform(ReadGraph graph) throws DatabaseException {
+                return Pair.make( resolveModel(graph, node), resolveRVI(graph, node) );
+            }
+        };
+    }
+
+    private static Read<Boolean> isSymbol(Node node) {
+        return new UniqueRead<Boolean>() {
+            @Override
+            public Boolean perform(ReadGraph graph) throws DatabaseException {
+                StructuralResource2 STR = StructuralResource2.getInstance(graph);
+                DiagramResource DIA = DiagramResource.getInstance(graph);
+                Resource possibleSymbol = graph.getPossibleObject(node.getDiagramResource(), STR.Defines);
+                return possibleSymbol != null && graph.isInstanceOf(possibleSymbol, DIA.ElementClass);
+            }
+        };
+    }
+
     private static Resource resolveModel(ReadGraph graph, Node node) throws DatabaseException {
         Resource composite = node.getDefiningResources().head();
         Resource model = graph.syncRequest(new PossibleIndexRoot(composite));
-//        Resource model = StructuralVariables.getModel(graph, composite);
         if (model == null)
             throw new ValidationException("no model found for composite " + NameUtils.getSafeName(graph, composite));
         return model;
     }
 
-//    private static String resolveModelURI(ReadGraph graph, final Node node) throws DatabaseException {
-//        return graph.getURI(resolveModel(graph, node));
-//    }
-
     private static String resolveRVI(ReadGraph graph, final Node node) throws DatabaseException {
-       String RVI = node.getRVI();
-       if(RVI != null) return RVI;
+        String RVI = node.getRVI();
+        if (RVI != null) return RVI;
         Resource composite = node.getDefiningResources().head();
         Variable var = Variables.getVariable(graph, composite);
         org.simantics.db.layer0.variable.RVI rvi = var.getPossibleRVI(graph);
-        if(rvi == null) return null;
-        return rvi.toString();
-//        final ResourceArray compositePath = StructuralVariables.getCompositeArray(graph, composite);
-//        final ResourceArray variablePath = compositePath.removeFromBeginning(1);
-//        return StructuralVariables.getRVI(graph, variablePath);
+        return rvi != null ? rvi.toString() : null;
     }
 
-}
+}
\ No newline at end of file