-/*******************************************************************************\r
- * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
- * 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.symbollibrary;\r
-\r
-import java.awt.Color;\r
-import java.awt.Graphics2D;\r
-import java.awt.Point;\r
-import java.awt.RenderingHints;\r
-import java.awt.geom.AffineTransform;\r
-import java.awt.geom.Rectangle2D;\r
-import java.awt.image.BufferedImage;\r
-\r
-import org.eclipse.swt.graphics.ImageData;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.diagram.query.DiagramRequests;\r
-import org.simantics.diagram.stubs.DiagramResource;\r
-import org.simantics.diagram.stubs.G2DResource;\r
-import org.simantics.g2d.diagram.DiagramClass;\r
-import org.simantics.g2d.diagram.DiagramHints;\r
-import org.simantics.g2d.diagram.IDiagram;\r
-import org.simantics.g2d.diagram.impl.Diagram;\r
-import org.simantics.g2d.element.ElementClass;\r
-import org.simantics.g2d.element.handler.StaticSymbol;\r
-import org.simantics.g2d.image.DefaultImages;\r
-import org.simantics.g2d.image.Image;\r
-import org.simantics.scenegraph.g2d.G2DSceneGraph;\r
-import org.simantics.structural.stubs.StructuralResource2;\r
-import org.simantics.utils.ui.gfx.ImageUtils;\r
-\r
-/**\r
- * @author Hannu Niemistö\r
- */\r
-public class IconRenderingUtil {\r
-\r
- public static BufferedImage renderBufferedImage(ReadGraph graph, Resource symbolResource, int size) throws DatabaseException {\r
- // Ensure these resources are available as services.\r
- G2DResource.getInstance(graph);\r
- StructuralResource2.getInstance(graph);\r
- DiagramResource.getInstance(graph);\r
-\r
- IDiagram diagram = Diagram.spawnNew(DiagramClass.DEFAULT);\r
-\r
- diagram.setHint(DiagramHints.KEY_ELEMENT_RASTER_TARGET_SIZE, new Point(size,size));\r
- ElementClass ec = \r
- graph.syncRequest( DiagramRequests.getElementClass(symbolResource, diagram) );\r
- StaticSymbol ss = ec.getSingleItem(StaticSymbol.class);\r
- Image source = ss == null ? DefaultImages.UNKNOWN2.get() : ss.getImage();\r
- G2DSceneGraph sceneGraph = new G2DSceneGraph();\r
- source.init(sceneGraph);\r
-\r
- BufferedImage targetImage = new BufferedImage(size, size, \r
- java.awt.image.BufferedImage.TYPE_INT_ARGB);\r
- Graphics2D g2d = (Graphics2D)targetImage.getGraphics();\r
- g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);\r
- g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);\r
- g2d.setBackground(Color.WHITE);\r
- g2d.clearRect(0, 0, size, size);\r
- Rectangle2D bounds = source.getBounds();\r
- double maxB = Math.max(bounds.getWidth(), bounds.getHeight());\r
- double scale = size/maxB;\r
- g2d.setTransform(new AffineTransform(\r
- scale, 0.0, 0.0, scale, -bounds.getMinX()*scale, -bounds.getMinY()*scale\r
- ));\r
- sceneGraph.render(g2d);\r
-\r
- return targetImage;\r
- }\r
-\r
- public static ImageData renderIcon(ReadGraph graph, Resource symbolResource, int size) throws DatabaseException {\r
- try {\r
- BufferedImage img = renderBufferedImage(graph, symbolResource, size);\r
- if (img == null)\r
- return null;\r
- return ImageUtils.convertToSWT(img);\r
- } catch (Exception e) {\r
- // FIXME: this is here because rendering a symbol may crash in\r
- // totally unexpected ways if the symbol contains unrenderable content.\r
- return null;\r
- }\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2011 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.symbollibrary;
+
+import java.awt.Color;
+import java.awt.Graphics2D;
+import java.awt.Point;
+import java.awt.RenderingHints;
+import java.awt.geom.AffineTransform;
+import java.awt.geom.Rectangle2D;
+import java.awt.image.BufferedImage;
+
+import org.eclipse.swt.graphics.ImageData;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.diagram.query.DiagramRequests;
+import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.diagram.stubs.G2DResource;
+import org.simantics.g2d.diagram.DiagramClass;
+import org.simantics.g2d.diagram.DiagramHints;
+import org.simantics.g2d.diagram.IDiagram;
+import org.simantics.g2d.diagram.impl.Diagram;
+import org.simantics.g2d.element.ElementClass;
+import org.simantics.g2d.element.handler.StaticSymbol;
+import org.simantics.g2d.image.DefaultImages;
+import org.simantics.g2d.image.Image;
+import org.simantics.scenegraph.g2d.G2DSceneGraph;
+import org.simantics.structural.stubs.StructuralResource2;
+import org.simantics.utils.ui.gfx.ImageUtils;
+
+/**
+ * @author Hannu Niemistö
+ */
+public class IconRenderingUtil {
+
+ public static BufferedImage renderBufferedImage(ReadGraph graph, Resource symbolResource, int size) throws DatabaseException {
+ // Ensure these resources are available as services.
+ G2DResource.getInstance(graph);
+ StructuralResource2.getInstance(graph);
+ DiagramResource.getInstance(graph);
+
+ IDiagram diagram = Diagram.spawnNew(DiagramClass.DEFAULT);
+
+ diagram.setHint(DiagramHints.KEY_ELEMENT_RASTER_TARGET_SIZE, new Point(size,size));
+ ElementClass ec =
+ graph.syncRequest( DiagramRequests.getElementClass(symbolResource, diagram) );
+ StaticSymbol ss = ec.getSingleItem(StaticSymbol.class);
+ Image source = ss == null ? DefaultImages.UNKNOWN2.get() : ss.getImage();
+ G2DSceneGraph sceneGraph = new G2DSceneGraph();
+ source.init(sceneGraph);
+
+ BufferedImage targetImage = new BufferedImage(size, size,
+ java.awt.image.BufferedImage.TYPE_INT_ARGB);
+ Graphics2D g2d = (Graphics2D)targetImage.getGraphics();
+ g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
+ g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
+ g2d.setBackground(Color.WHITE);
+ g2d.clearRect(0, 0, size, size);
+ Rectangle2D bounds = source.getBounds();
+ double maxB = Math.max(bounds.getWidth(), bounds.getHeight());
+ double scale = size/maxB;
+ g2d.setTransform(new AffineTransform(
+ scale, 0.0, 0.0, scale, -bounds.getMinX()*scale, -bounds.getMinY()*scale
+ ));
+ sceneGraph.render(g2d);
+
+ return targetImage;
+ }
+
+ public static ImageData renderIcon(ReadGraph graph, Resource symbolResource, int size) throws DatabaseException {
+ try {
+ BufferedImage img = renderBufferedImage(graph, symbolResource, size);
+ if (img == null)
+ return null;
+ return ImageUtils.convertToSWT(img);
+ } catch (Exception e) {
+ // FIXME: this is here because rendering a symbol may crash in
+ // totally unexpected ways if the symbol contains unrenderable content.
+ return null;
+ }
+ }
+
+}