]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/symbollibrary/IconRenderingUtil.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / symbollibrary / IconRenderingUtil.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.symbollibrary;
13
14 import java.awt.Color;
15 import java.awt.Graphics2D;
16 import java.awt.Point;
17 import java.awt.RenderingHints;
18 import java.awt.geom.AffineTransform;
19 import java.awt.geom.Rectangle2D;
20 import java.awt.image.BufferedImage;
21
22 import org.eclipse.swt.graphics.ImageData;
23 import org.simantics.db.ReadGraph;
24 import org.simantics.db.Resource;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.diagram.query.DiagramRequests;
27 import org.simantics.diagram.stubs.DiagramResource;
28 import org.simantics.diagram.stubs.G2DResource;
29 import org.simantics.g2d.diagram.DiagramClass;
30 import org.simantics.g2d.diagram.DiagramHints;
31 import org.simantics.g2d.diagram.IDiagram;
32 import org.simantics.g2d.diagram.impl.Diagram;
33 import org.simantics.g2d.element.ElementClass;
34 import org.simantics.g2d.element.handler.StaticSymbol;
35 import org.simantics.g2d.image.DefaultImages;
36 import org.simantics.g2d.image.Image;
37 import org.simantics.scenegraph.g2d.G2DSceneGraph;
38 import org.simantics.structural.stubs.StructuralResource2;
39 import org.simantics.utils.ui.gfx.ImageUtils;
40
41 /**
42  * @author Hannu Niemistö
43  */
44 public class IconRenderingUtil {
45
46     public static BufferedImage renderBufferedImage(ReadGraph graph, Resource symbolResource, int size) throws DatabaseException {
47         // Ensure these resources are available as services.
48         G2DResource.getInstance(graph);
49         StructuralResource2.getInstance(graph);
50         DiagramResource.getInstance(graph);
51
52         IDiagram diagram = Diagram.spawnNew(DiagramClass.DEFAULT);
53
54         diagram.setHint(DiagramHints.KEY_ELEMENT_RASTER_TARGET_SIZE, new Point(size,size));
55         ElementClass ec = 
56                 graph.syncRequest( DiagramRequests.getElementClass(symbolResource, diagram) );
57         StaticSymbol ss = ec.getSingleItem(StaticSymbol.class);
58         Image source = ss == null ? DefaultImages.UNKNOWN2.get() : ss.getImage();
59         G2DSceneGraph sceneGraph = new G2DSceneGraph();
60         source.init(sceneGraph);
61
62         BufferedImage targetImage = new BufferedImage(size, size, 
63                 java.awt.image.BufferedImage.TYPE_INT_ARGB);
64         Graphics2D g2d = (Graphics2D)targetImage.getGraphics();
65         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
66         g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
67         g2d.setBackground(Color.WHITE);
68         g2d.clearRect(0, 0, size, size);
69         Rectangle2D bounds = source.getBounds();
70         double maxB = Math.max(bounds.getWidth(), bounds.getHeight());
71         double scale = size/maxB;
72         g2d.setTransform(new AffineTransform(
73                 scale, 0.0, 0.0, scale, -bounds.getMinX()*scale, -bounds.getMinY()*scale
74                 ));
75         sceneGraph.render(g2d);
76
77         return targetImage;
78     }
79
80     public static ImageData renderIcon(ReadGraph graph, Resource symbolResource, int size) throws DatabaseException {
81         try {
82             BufferedImage img = renderBufferedImage(graph, symbolResource, size);
83             if (img == null)
84                 return null;
85             return ImageUtils.convertToSWT(img);
86         } catch (Exception e) {
87             // FIXME: this is here because rendering a symbol may crash in
88             // totally unexpected ways if the symbol contains unrenderable content.
89             return null;
90         }
91     }
92
93 }