--- /dev/null
+/*******************************************************************************\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.scenegraph.g2d.nodes;\r
+\r
+import java.awt.BasicStroke;\r
+import java.awt.Color;\r
+import java.awt.Graphics2D;\r
+import java.awt.Shape;\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.geom.Rectangle2D;\r
+import org.simantics.scenegraph.g2d.G2DParentNode;\r
+import org.simantics.scenegraph.g2d.IG2DNode;\r
+\r
+/**\r
+ * \r
+ * Holder node that scales content to fit (and scales SVG inside those bounds)\r
+ * \r
+ * @author jplaine\r
+ *\r
+ */\r
+public class SVGHolderNode extends G2DParentNode {\r
+ /**\r
+ * \r
+ */\r
+ private static final long serialVersionUID = 8698435757824280001L;\r
+\r
+ protected Rectangle2D bounds = null;\r
+ protected Float borderWidth = null;\r
+ \r
+ \r
+ @PropertySetter("VariableFilter")\r
+ public void setScript(String script) {\r
+ for(IG2DNode node : getSortedNodes()) // Pass value to children\r
+ if(node instanceof AnimatedSVGNode) {\r
+ ((AnimatedSVGNode)node).setScript(script);\r
+ }\r
+ }\r
+ \r
+ @PropertySetter("SVG")\r
+ public void setData(String data) {\r
+ for(IG2DNode node : getSortedNodes()) // Pass value to children\r
+ if(node instanceof AnimatedSVGNode) {\r
+ ((AnimatedSVGNode)node).setData(data);\r
+ }\r
+ }\r
+\r
+ @PropertySetter("Stroke Width")\r
+ @SyncField("border")\r
+ public void setBorderWidth(Float borderWidth) {\r
+ this.borderWidth = borderWidth;\r
+ }\r
+\r
+ @PropertySetter("Bounds")\r
+ @SyncField("bounds")\r
+ public void setBounds(Rectangle2D bounds) {\r
+ this.bounds = bounds;\r
+ }\r
+\r
+ @Override\r
+ public void render(Graphics2D g2d) {\r
+ Rectangle2D cb = getBoundsInLocal(false);\r
+ double scaleX = bounds.getWidth() / cb.getWidth();\r
+ double scaleY = bounds.getHeight() / cb.getHeight();\r
+ double scale = Math.min(scaleX, scaleY);\r
+\r
+ if(borderWidth != null && borderWidth > 0.0f && bounds != null) {\r
+ Graphics2D g2 = (Graphics2D)g2d.create();\r
+ g2.transform(transform);\r
+ g2.setStroke(new BasicStroke(this.borderWidth));\r
+ g2.setColor(Color.BLACK);\r
+ g2.draw(bounds);\r
+ g2.dispose();\r
+ }\r
+\r
+ @SuppressWarnings("unused")\r
+ int noBounds = 0;\r
+ @SuppressWarnings("unused")\r
+ int clipped = 0;\r
+ @SuppressWarnings("unused")\r
+ int rendered = 0;\r
+\r
+ AffineTransform ot = g2d.getTransform();\r
+ g2d.transform(transform);\r
+\r
+ g2d.translate(-cb.getMinX()*scale+bounds.getMinX(), -cb.getMinY()*scale+bounds.getMinY());\r
+ g2d.scale(scale, scale);\r
+\r
+ // Get transformed clip bounds\r
+ Shape clipShape = g2d.getClip();\r
+ Rectangle2D bounds = null;\r
+ if (clipShape instanceof Rectangle2D)\r
+ bounds = (Rectangle2D) clipShape;\r
+ else if (clipShape != null)\r
+ bounds = clipShape.getBounds2D();\r
+\r
+ boolean noClipBounds = bounds == null;\r
+\r
+ for(IG2DNode node : getSortedNodes()) {\r
+ Rectangle2D localBounds = node.getBoundsInLocal();\r
+ boolean hasNoBounds = localBounds == null;\r
+ boolean visible = false;\r
+ if (!noClipBounds && !hasNoBounds) {\r
+ visible = node.intersects(bounds);\r
+ } else {\r
+ ++noBounds;\r
+ }\r
+ if (noClipBounds || hasNoBounds || visible) {\r
+ // TODO: consider removing this g2d.create ?\r
+ // Will definitely current cause bugs to appear.\r
+ if (node.validate()) {\r
+ node.render(g2d);\r
+ }\r
+ ++rendered;\r
+ } else {\r
+ ++clipped;\r
+ }\r
+ }\r
+\r
+ g2d.setTransform(ot);\r
+ }\r
+\r
+ @Override\r
+ public Rectangle2D getBoundsInLocal() {\r
+ return bounds; // FIXME: not sure if it's a good idea to return different value than the getBoundsInLocal(boolean nulls) returns..\r
+ }\r
+\r
+}\r