X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.g2d%2Fsrc%2Forg%2Fsimantics%2Fg2d%2Felement%2Fhandler%2Fimpl%2FTransitionFunctionPainter.java;h=0e9954927d33f3830cf433a78efd8d804096d373;hb=680a2159d36d893cf7fbf0212e7ffbff847a8f82;hp=c4f01dd94474e4d4b936ea2b0370b87a504a738c;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/TransitionFunctionPainter.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/TransitionFunctionPainter.java index c4f01dd94..0e9954927 100644 --- a/bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/TransitionFunctionPainter.java +++ b/bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/TransitionFunctionPainter.java @@ -1,85 +1,85 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 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.g2d.element.handler.impl; - -import java.awt.BasicStroke; -import java.awt.Color; -import java.awt.geom.AffineTransform; -import java.awt.geom.Path2D; -import java.awt.geom.Rectangle2D; - -import org.simantics.g2d.element.ElementUtils; -import org.simantics.g2d.element.IElement; -import org.simantics.g2d.element.SceneGraphNodeKey; -import org.simantics.g2d.element.handler.SceneGraph; -import org.simantics.g2d.multileveldiagram.TransitionFunction; -import org.simantics.scenegraph.Node; -import org.simantics.scenegraph.g2d.G2DParentNode; -import org.simantics.scenegraph.g2d.nodes.ShapeNode; -import org.simantics.utils.datastructures.hints.IHintContext.Key; - -/** - * @See {@link TransitionFunction} - * @author Toni Kalajainen - */ -public class TransitionFunctionPainter implements SceneGraph { - /** - * - */ - private static final long serialVersionUID = 218125859645762515L; - TransitionFunction func; - Path2D path; - final static BasicStroke STROKE = new BasicStroke(1.0f); - - public static final Key SG_NODE = new SceneGraphNodeKey(Node.class, "SUB_SG_NODE"); - - public TransitionFunctionPainter(TransitionFunction func) { - this.func = func; - path = new Path2D.Double(); - for (int i=0; i<101; i++) - { - double x = ((double)i)/100; - double y = func.f(x); - if (i==0) - path.moveTo(x, 1-y); - else - path.lineTo(x, 1-y); - } - } - - @Override - public void cleanup(IElement e) { - Node node = e.removeHint(SG_NODE); - if (node != null) - node.remove(); - } - - @Override - public void init(IElement e, G2DParentNode parent) { - ShapeNode node = (ShapeNode) e.getHint(SG_NODE); - if (node == null) { - node = parent.addNode(ShapeNode.class); - e.setHint(SG_NODE, node); - } - - Rectangle2D bounds = ElementUtils.getElementBounds(e); - - AffineTransform transform = AffineTransform.getTranslateInstance(-bounds.getMinX(), -bounds.getMinY()); - transform.scale(bounds.getWidth(), bounds.getHeight()); - - node.setTransform(transform); - node.setStroke(STROKE); - node.setScaleStroke(true); - node.setColor(Color.BLACK); - node.setShape(path); - } -} +/******************************************************************************* + * Copyright (c) 2007, 2010 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.g2d.element.handler.impl; + +import java.awt.BasicStroke; +import java.awt.Color; +import java.awt.geom.AffineTransform; +import java.awt.geom.Path2D; +import java.awt.geom.Rectangle2D; + +import org.simantics.g2d.element.ElementUtils; +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.SceneGraphNodeKey; +import org.simantics.g2d.element.handler.SceneGraph; +import org.simantics.g2d.multileveldiagram.TransitionFunction; +import org.simantics.scenegraph.Node; +import org.simantics.scenegraph.g2d.G2DParentNode; +import org.simantics.scenegraph.g2d.nodes.ShapeNode; +import org.simantics.utils.datastructures.hints.IHintContext.Key; + +/** + * @See {@link TransitionFunction} + * @author Toni Kalajainen + */ +public class TransitionFunctionPainter implements SceneGraph { + /** + * + */ + private static final long serialVersionUID = 218125859645762515L; + TransitionFunction func; + Path2D path; + final static BasicStroke STROKE = new BasicStroke(1.0f); + + public static final Key SG_NODE = new SceneGraphNodeKey(Node.class, "SUB_SG_NODE"); + + public TransitionFunctionPainter(TransitionFunction func) { + this.func = func; + path = new Path2D.Double(); + for (int i=0; i<101; i++) + { + double x = ((double)i)/100; + double y = func.f(x); + if (i==0) + path.moveTo(x, 1-y); + else + path.lineTo(x, 1-y); + } + } + + @Override + public void cleanup(IElement e) { + Node node = e.removeHint(SG_NODE); + if (node != null) + node.remove(); + } + + @Override + public void init(IElement e, G2DParentNode parent) { + ShapeNode node = (ShapeNode) e.getHint(SG_NODE); + if (node == null) { + node = parent.addNode(ShapeNode.class); + e.setHint(SG_NODE, node); + } + + Rectangle2D bounds = ElementUtils.getElementBounds(e); + + AffineTransform transform = AffineTransform.getTranslateInstance(-bounds.getMinX(), -bounds.getMinY()); + transform.scale(bounds.getWidth(), bounds.getHeight()); + + node.setTransform(transform); + node.setStroke(STROKE); + node.setScaleStroke(true); + node.setColor(Color.BLACK); + node.setShape(path); + } +}