From: Antti Villberg Date: Sun, 1 Oct 2017 17:49:17 +0000 (+0300) Subject: Supply SVG text editor with element measurement context X-Git-Tag: v1.31.0~153^2 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F60%2F1060%2F7;p=simantics%2Fplatform.git Supply SVG text editor with element measurement context refs #7520 Change-Id: Ie72bc282f98b2545fed14ff6bd55fb10198e0151 --- diff --git a/bundles/org.simantics.diagram.ontology/graph/Diagram.pgraph b/bundles/org.simantics.diagram.ontology/graph/Diagram.pgraph index 103140b6f..b26bfc698 100644 --- a/bundles/org.simantics.diagram.ontology/graph/Diagram.pgraph +++ b/bundles/org.simantics.diagram.ontology/graph/Diagram.pgraph @@ -127,7 +127,7 @@ DIA.DefinedElement >-- DIA.symbolCode -- DIA.symbolDropHandler ==> "[WorkbenchSelectionElement] -> ()" -- DIA.DefinedElement.textEditor ==> "String -> String -> ()" -- DIA.DefinedElement.textEditor ==> "SVGMeasurementContext -> String -> String -> ()" -- DIA.DefinedElement.textEditorFullText ==> "String -> String" String -> Maybe BoundingBox + +getBoundingBoxX :: BoundingBox -> Double +getBoundingBoxX (x,_,_,_) = x + +getBoundingBoxY :: BoundingBox -> Double +getBoundingBoxY (_,y,_,_) = y + +getBoundingBoxW :: BoundingBox -> Double +getBoundingBoxW (_,_,w,_) = w + +getBoundingBoxH :: BoundingBox -> Double +getBoundingBoxH (_,_,_,h) = h + + diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/EditorStateManager.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/EditorStateManager.java index 5c59177cc..1223d8eaf 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/EditorStateManager.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/EditorStateManager.java @@ -1,3 +1,14 @@ +/******************************************************************************* + * Copyright (c) 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 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation + *******************************************************************************/ package org.simantics.diagram.elements; import java.awt.geom.Point2D; @@ -13,7 +24,8 @@ import org.simantics.scenegraph.g2d.events.KeyEvent.KeyPressedEvent; import org.simantics.scenegraph.g2d.events.MouseEvent.MouseClickEvent; import org.simantics.scenegraph.g2d.nodes.SingleElementNode; import org.simantics.scl.runtime.function.Function1; -import org.simantics.scl.runtime.function.Function2; +import org.simantics.scl.runtime.function.Function3; +import org.simantics.scl.runtime.tuple.Tuple4; import com.kitfox.svg.SVGDiagram; import com.kitfox.svg.SVGElement; @@ -36,6 +48,23 @@ class EditorStateManager { private LinkedList editorState = null; private int editorStateIndex = 0; + static class SVGMeasurementContextImpl implements SVGMeasurementContext { + private SVGNode node; + public SVGMeasurementContextImpl(SVGNode node) { + this.node = node; + } + @Override + public Tuple4 getBoundingBox(String id) { + try { + Rectangle2D rect = node.getElementBounds(id); + if(rect == null) return null; + return new Tuple4(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight()); + } catch (SVGException e) { + return null; + } + } + } + EditorStateManager(SVGNode node) { this.node = node; } @@ -119,9 +148,9 @@ class EditorStateManager { SingleElementNode sne = node.getSingleElementNode(); if (sne != null) { EditorState es = currentState(); - Function2 editor = sne.getParameter("textEditor"); + Function3 editor = sne.getParameter("textEditor"); if(editor != null) { - editor.apply(es.base.textElementId, es.currentText); + editor.apply(new SVGMeasurementContextImpl(node), es.base.textElementId, es.currentText); } } } diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/SVGMeasurementContext.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/SVGMeasurementContext.java new file mode 100644 index 000000000..19175a833 --- /dev/null +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/elements/SVGMeasurementContext.java @@ -0,0 +1,22 @@ +/******************************************************************************* + * Copyright (c) 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 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package org.simantics.diagram.elements; + +import org.simantics.scl.runtime.tuple.Tuple4; + +/** + * @author Antti Villberg + * @since 1.31.0 + */ +public interface SVGMeasurementContext { + Tuple4 getBoundingBox(String id); +} diff --git a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/SVGNode.java b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/SVGNode.java index db491a3fd..d21ba1f16 100644 --- a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/SVGNode.java +++ b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/SVGNode.java @@ -45,6 +45,7 @@ import org.simantics.scl.runtime.function.Function1; import org.simantics.scl.runtime.function.Function2; import org.simantics.utils.threads.AWTThread; +import com.kitfox.svg.RenderableElement; import com.kitfox.svg.SVGCache; import com.kitfox.svg.SVGDiagram; import com.kitfox.svg.SVGElement; @@ -484,4 +485,13 @@ public class SVGNode extends G2DNode implements InitValueSupport, LoaderNode { return ret; } + public Rectangle2D getElementBounds(String id) throws SVGException { + SVGElement e = diagramCache.getElement(id); + if (e instanceof RenderableElement) { + return ((RenderableElement)e).getBoundingBox(); + } else { + return null; + } + } + }