From: miettinen Date: Thu, 21 Nov 2013 11:45:46 +0000 (+0000) Subject: Sysdyn loop text to diagram. Hover around loop image. (refs #3012) X-Git-Tag: 1.8.1~188 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=393e5a860850a2301e446e061e60391a23246511;p=simantics%2Fsysdyn.git Sysdyn loop text to diagram. Hover around loop image. (refs #3012) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@28350 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ontology/graph.tg b/org.simantics.sysdyn.ontology/graph.tg index 848075d6..ec145e4c 100644 Binary files a/org.simantics.sysdyn.ontology/graph.tg and b/org.simantics.sysdyn.ontology/graph.tg differ diff --git a/org.simantics.sysdyn.ontology/graph/CommentSymbols.pgraph b/org.simantics.sysdyn.ontology/graph/CommentSymbols.pgraph index 22f149e7..82666f45 100644 --- a/org.simantics.sysdyn.ontology/graph/CommentSymbols.pgraph +++ b/org.simantics.sysdyn.ontology/graph/CommentSymbols.pgraph @@ -27,7 +27,7 @@ SYSDYN.Loop + d="m 5.77,0 -8.65,5 0,-10 8.65,5 z" + style="fill-rule:evenodd;stroke:#000000;stroke-width:1pt" + transform="scale(-0.8,-0.8)" + inkscape:connector-curvature="0" /> + style="overflow:visible"> + style="fill-rule:evenodd;stroke-width:0.625;stroke-linejoin:round" + d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z" + transform="matrix(-1.1,0,0,-1.1,-1.1,0)" + inkscape:connector-curvature="0" /> + style="fill-rule:evenodd;stroke-width:0.625;stroke-linejoin:round" + d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z" + transform="matrix(1.1,0,0,1.1,1.1,0)" + inkscape:connector-curvature="0" /> + d="M 0,0 5,-5 -12.5,0 5,5 0,0 z" + style="fill-rule:evenodd;stroke:#000000;stroke-width:1pt" + transform="matrix(0.8,0,0,0.8,10,0)" + inkscape:connector-curvature="0" /> + d="M 0,0 5,-5 -12.5,0 5,5 0,0 z" + style="fill-rule:evenodd;stroke:#000000;stroke-width:1pt" + transform="matrix(0.4,0,0,0.4,4,0)" + inkscape:connector-curvature="0" /> + style="overflow:visible"> + d="M 0,0 5,-5 -12.5,0 5,5 0,0 z" + style="fill-rule:evenodd;stroke:#000000;stroke-width:1pt" + transform="matrix(-0.8,0,0,-0.8,-10,0)" + inkscape:connector-curvature="0" /> + inkscape:window-maximized="0" + fit-margin-top="0" + fit-margin-left="0" + fit-margin-right="0" + fit-margin-bottom="0" /> @@ -123,7 +133,7 @@ image/svg+xml - + @@ -131,7 +141,7 @@ inkscape:label="Taso 1" inkscape:groupmode="layer" id="layer1" - style="opacity:1"> + transform="translate(-0.13537823,-1042.2366)"> + transform="matrix(0.00747689,-0.00472065,0.00249533,0.01414476,2.177026,1048.1856)" /> diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ExportTester.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ExportTester.java index df9343b5..dad1cd44 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ExportTester.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ExportTester.java @@ -15,6 +15,7 @@ package org.simantics.sysdyn.ui.browser.nodes; import java.util.Collection; import org.eclipse.core.expressions.PropertyTester; +import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IWorkbenchPage; import org.simantics.DatabaseJob; import org.simantics.browsing.ui.common.node.AbstractNode; @@ -53,14 +54,12 @@ public class ExportTester extends PropertyTester { if (inputResource == null) { IWorkbenchPage page = SysdynWorkbenchUtils.getActivePageOfEditor(); - DiagramEditor editor = (DiagramEditor)page.getActiveEditor(); - if (editor != null) { - if (editor instanceof DiagramEditor) { - inputResource = editor.getInputResource(); - } - } else { - return false; - } + IEditorPart editor = page.getActiveEditor(); + if (editor instanceof DiagramEditor) { + inputResource = ((DiagramEditor)editor).getInputResource(); + } else { + return false; + } } final Resource resource = inputResource; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/CreateVariablesShortcutParticipant.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/CreateVariablesShortcutParticipant.java index 4919ec2b..96d6f930 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/CreateVariablesShortcutParticipant.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/CreateVariablesShortcutParticipant.java @@ -48,6 +48,7 @@ import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.ui.elements.AuxiliaryFactory; import org.simantics.sysdyn.ui.elements.CloudFactory; import org.simantics.sysdyn.ui.elements.InputFactory; +import org.simantics.sysdyn.ui.elements.LoopFactory; import org.simantics.sysdyn.ui.elements.ShadowFactory; import org.simantics.sysdyn.ui.elements.StockFactory; import org.simantics.sysdyn.ui.elements.ValveFactory; @@ -163,12 +164,12 @@ public class CreateVariablesShortcutParticipant extends AbstractDiagramParticipa SysdynResource.URIs.ShadowSymbol, (ShapeNode)ShadowFactory.GHOST_IMAGE.init(parent) ); - /*} else if (kpe.keyCode == java.awt.event.KeyEvent.VK_L) { + } else if (kpe.keyCode == java.awt.event.KeyEvent.VK_L) { variableInformation = new VariableInformation( java.awt.event.KeyEvent.VK_L, SysdynResource.URIs.LoopSymbol, (ShapeNode)LoopFactory.LOOP_STATIC_IMAGE.init(parent) - );*/ + ); } if (variableInformation != null) { diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/HoverTextElementNoBounds.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/HoverTextElementNoBounds.java index 9ac131dc..41c548d9 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/HoverTextElementNoBounds.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/HoverTextElementNoBounds.java @@ -289,8 +289,10 @@ public class HoverTextElementNoBounds extends TextElementNoBounds implements Lif return; } - // If the resource is just been created, activate editing its name. - activateEdit(node, e); + // If the resource is just been created, activate editing its name. + if (!graph.isInstanceOf(r, SR.Loop)) { + activateEdit(node, e); + } graph.deny(component, SR.IndependentVariable_isUninitialized, r); } }); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/LoopFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/LoopFactory.java index f606fc72..68952772 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/LoopFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/LoopFactory.java @@ -12,21 +12,38 @@ package org.simantics.sysdyn.ui.elements; import java.awt.BasicStroke; +import java.awt.Color; +import java.awt.Paint; import java.awt.Shape; +import java.awt.geom.AffineTransform; import java.awt.geom.Arc2D; import java.awt.geom.Path2D; +import java.awt.geom.Rectangle2D; +import java.awt.geom.RectangularShape; import java.util.Collection; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.common.request.WriteRequest; import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; +import org.simantics.diagram.elements.ITextListener; +import org.simantics.diagram.elements.TextNode; import org.simantics.diagram.stubs.G2DResource; import org.simantics.g2d.canvas.ICanvasContext; +import org.simantics.g2d.diagram.DiagramUtils; import org.simantics.g2d.diagram.IDiagram; import org.simantics.g2d.element.ElementClass; +import org.simantics.g2d.element.ElementHints; +import org.simantics.g2d.element.ElementUtils; +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.SceneGraphNodeKey; +import org.simantics.g2d.element.handler.HandleMouseEvent; import org.simantics.g2d.element.handler.impl.BoundsOutline; import org.simantics.g2d.element.handler.impl.DefaultTransform; +import org.simantics.g2d.element.handler.impl.HoverImpl; import org.simantics.g2d.element.handler.impl.ObjectTerminal; import org.simantics.g2d.element.handler.impl.OutlinePick; import org.simantics.g2d.element.handler.impl.SimpleElementLayers; @@ -41,59 +58,282 @@ import org.simantics.g2d.image.DefaultImages; import org.simantics.g2d.image.Image; import org.simantics.g2d.image.impl.ShapeImage; import org.simantics.g2d.svg.SVGImage; +import org.simantics.g2d.utils.Alignment; +import org.simantics.modeling.ModelingResources; +import org.simantics.scenegraph.g2d.G2DParentNode; +import org.simantics.scenegraph.g2d.nodes.SVGNode; +import org.simantics.scenegraph.g2d.nodes.ShapeNode; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.properties.LoopTab; +import org.simantics.ui.SimanticsUI; +import org.simantics.utils.datastructures.Callback; +import org.simantics.utils.datastructures.hints.IHintContext.Key; +import org.simantics.utils.datastructures.hints.IHintListener; +import org.simantics.utils.datastructures.hints.IHintObservable; /** + * Factory for loops. + * * @author Tuomas Miettinen */ public class LoopFactory extends SysdynElementFactory { - public static final Image LOOP_STATIC_IMAGE = new ShapeImage(getLoopShape(), null, new BasicStroke(1), true); - - @Override - public ElementClass create(ReadGraph graph, ICanvasContext canvas, - IDiagram diagram, Resource elementType) throws DatabaseException { - - G2DResource g2d = G2DResource.getInstance(graph); - String svgDoc = graph.getPossibleRelatedValue(elementType, g2d.HasSVGDocument); - Image image = null; - String id = "TextElement: " + NameUtils.getSafeName(graph, elementType); - if (svgDoc != null) - image = new SVGImage(id+".svg", svgDoc); - else - image = DefaultImages.ERROR_DECORATOR.get(); - - return ElementClass.compile( - SimpleElementLayers.INSTANCE, - OutlinePick.INSTANCE, - TextImpl.INSTANCE, - TextColorImpl.BLACK, - TextFontImpl.DEFAULT, - DefaultTransform.INSTANCE, - new StaticSymbolImpl(image), - new StaticImageElementHandler(image), - new StaticObjectAdapter(elementType), - StaticSymbolImageInitializer.INSTANCE, - //new HoverTextElementHandler(0, 0, Alignment.LEADING, 0, 1.5, 1.5, true), - BoundsOutline.INSTANCE - ).setId(LoopFactory.class.getSimpleName()); - } - - static Shape getLoopShape() { - Path2D loop = new Path2D.Double(); - loop.moveTo(1, 2); - loop.lineTo(0, 0); - loop.lineTo(-1, 2); - loop.closePath(); - - loop.append(new Arc2D.Double(-10, -3, 10, 10, 0, -270, Arc2D.OPEN), false); - return loop; - } + public static final Image LOOP_STATIC_IMAGE = new ShapeImage(getLoopShape(), null, new BasicStroke(1), true); + private static Image image = null; + + @Override + public ElementClass create(ReadGraph graph, ICanvasContext canvas, + IDiagram diagram, Resource elementType) throws DatabaseException { + + // Create the loop symbol svg image + G2DResource g2d = G2DResource.getInstance(graph); + String svgDoc = graph.getPossibleRelatedValue(elementType, g2d.HasSVGDocument); + String id = "TextElement: " + NameUtils.getSafeName(graph, elementType); + if (svgDoc != null) + image = new SVGImage(id+".svg", svgDoc); + else + image = DefaultImages.ERROR_DECORATOR.get(); + + return ElementClass.compile( + SimpleElementLayers.INSTANCE, + OutlinePick.INSTANCE, + TextImpl.INSTANCE, + TextColorImpl.BLACK, + TextFontImpl.DEFAULT, + DefaultTransform.INSTANCE, + new StaticObjectAdapter(elementType), + new StaticSymbolImpl(image), + StaticSymbolImageInitializer.INSTANCE, + new LoopImageSceneGraph(image), + HoverImpl.INSTANCE, + LoopSceneGraph.INSTANCE, + BoundsOutline.INSTANCE + ).setId(LoopFactory.class.getSimpleName()); + } + + static Shape getLoopShape() { + // Used in shortcut. + Path2D loop = new Path2D.Double(); + loop.moveTo(1, 2); + loop.lineTo(0, 0); + loop.lineTo(-1, 2); + loop.closePath(); + + loop.append(new Arc2D.Double(-10, -3, 10, 10, 0, -270, Arc2D.OPEN), false); + return loop; + } + + @Override + protected String getText(ReadGraph graph, Resource element) throws DatabaseException { + // Get loop comment instead of loop name. + SysdynResource sr = SysdynResource.getInstance(graph); + ModelingResources mr = ModelingResources.getInstance(graph); + Resource component = graph.getPossibleObject(element, mr.ElementToComponent); + String text = null; + if (component != null) { + text = (String) graph.getPossibleRelatedValue(component, sr.Loop_Comment); + } + if (text == null || LoopTab.AUTO.equals(text)) + text = ""; + return text; + } @Override protected ElementClass compileElementClass(Resource elementType, Collection terminals) { - // TODO Auto-generated method stub return null; } + /** + * Handling for the loop image on diagram. + * @author Tuomas Miettinen + * + */ + public static class LoopImageSceneGraph extends StaticImageElementHandler { + + private static final long serialVersionUID = -185893525553743589L; + + private IHintListener hoverHintListener; + + private static final Key NODE = new SceneGraphNodeKey(HoverShapeNode.class, "LOOP_NODE"); + + public LoopImageSceneGraph(Image i) { + super(i); + } + + @Override + public void init(IElement e, G2DParentNode parent) { + super.init(e, parent); + + // Create new hover shape node for the loop image + final HoverShapeNode node = ElementUtils.getOrCreateNode(e, parent, NODE, "loopHover", HoverShapeNode.class); + + // Set the shape of the shape node to match the shape of the image + SVGNode image = e.getHint(getNodeKey()); + RectangularShape bounds = (RectangularShape)image.getBounds().clone(); + node.setShape(bounds); + + // Set the shape node transparent + Paint paint = new Color(0,0,0,0); + node.setColor(paint); + + // Handle hover + Boolean hover = e.getHint(ElementHints.KEY_HOVER); + node.setHover(hover != null ? hover : false); + hoverHintListener = new IHintListener() { + + @Override + public void hintRemoved(IHintObservable sender, Key key, Object oldValue) { + } + + @Override + public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) { + IElement e = (IElement)sender; + HoverShapeNode shape = (HoverShapeNode) e.getHint(NODE); + if(shape == null) { + return; + } + boolean hover = ElementUtils.isHovering(e); + shape.setHover(hover); + } + }; + e.addHintListener(hoverHintListener); + } + + @Override + public void cleanup(IElement e) { + super.cleanup(e); + e.removeHintListener(hoverHintListener); + } + } + + /** + * Handling for the text box of loop in diagram. + * @author Tuomas Miettinen + * + */ + public static class LoopSceneGraph extends HoverTextElementNoBounds implements HandleMouseEvent { + + private static final long serialVersionUID = -5093461687773246286L; + + public static final LoopSceneGraph INSTANCE = new LoopSceneGraph(); + + private static final Key NODE = new SceneGraphNodeKey(ShapeNode.class, "LOOP_COMMENT_NODE"); + + public LoopSceneGraph() { + super(0, 0, Alignment.CENTER, 0, 1, 1, true); + } + + @Override + public void init(IElement e, G2DParentNode parent) { + super.init(e, parent); + + // Move the text box into (around) the middle of the loop image + AffineTransform at = ElementUtils.getTransform(e); + final HoverShapeNode node = ElementUtils.getOrCreateNode(e, parent, NODE, "loopComment", HoverShapeNode.class); + + if(at != null) { + node.setTransform(at); + + TextNode name = (TextNode) e.getHint(SG_NODE); + if(name != null) { + AffineTransform at2 = (AffineTransform) at.clone(); + at2.translate(getXCoordShift(e), getYCoordShift(e)); + + Alignment alignment = Alignment.CENTER; + + name.setTransform(at2); + if(alignment != null) + name.setHorizontalAlignment((byte) alignment.ordinal()); + } + } + + } + + private static double getXCoordShift(IElement e) { + //String location = e.getHint(SysdynElementHints.KEY_LOCATION); + return 0.4; + } + + private static double getYCoordShift(IElement e) { + //String location = e.getHint(SysdynElementHints.KEY_LOCATION); + return 10.7; + } + + @Override + protected Callback getCallback(final IElement e, G2DParentNode parent, Class nodeClass) { + return new Callback() { + @Override + public void run(T node) { + node.setTextListener(new ITextListener() { + + Resource component; + + @Override + public void textChanged() { + } + + @Override + public void textEditingStarted() { + if(component != null) return; + + Object o = e.getHint(ElementHints.KEY_OBJECT); + if(o != null && o instanceof Resource) { + final Resource element = (Resource)o; + SimanticsUI.getSession().asyncRequest(new ReadRequest() { + + @Override + public void run(ReadGraph graph) throws DatabaseException { + component = graph.getPossibleObject(element, ModelingResources.getInstance(graph).ElementToComponent); + } + }); + } + } + + @Override + public void textEditingCancelled() { + TextNode node = (TextNode) e.getHint(SG_NODE); + if (node != null) { + endEdit(node); + } + } + + @Override + public void textEditingEnded() { + TextNode node = (TextNode) e.getHint(SG_NODE); + if (node == null) + return; + + // Write the changed comment of the loop + final String text = node.getText(); + try { + SimanticsUI.getSession().syncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) + throws DatabaseException { + if (component != null) { + SysdynResource sr = SysdynResource.getInstance(graph); + graph.deny(component, sr.Loop_Comment); + graph.claimLiteral(component, sr.Loop_Comment, text); + } + } + + }); + } catch (DatabaseException e1) { + e1.printStackTrace(); + } + + IDiagram diagram = ElementUtils.getDiagram(e); + DiagramUtils.synchronizeHintsToBackend(diagram, e); + endEdit(node); + } + }); + } + }; + } + + } + + } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/SysdynElementFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/SysdynElementFactory.java index 36996ec3..497f3de0 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/SysdynElementFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/SysdynElementFactory.java @@ -66,11 +66,9 @@ public abstract class SysdynElementFactory extends SyncElementFactory { ComponentNameSynchronizer.INSTANCE, TransformSynchronizer.INSTANCE); - @Override - public void load(ReadGraph graph, final ICanvasContext canvas, final IDiagram diagram, final Resource element, final IElement e) throws DatabaseException { + protected String getText(ReadGraph graph, Resource element) throws DatabaseException { Layer0 l0 = Layer0.getInstance(graph); ModelingResources mr = ModelingResources.getInstance(graph); - Resource component = graph.getPossibleObject(element, mr.ElementToComponent); String text = null; if (component != null) { @@ -78,8 +76,12 @@ public abstract class SysdynElementFactory extends SyncElementFactory { } if (text == null) text = "[empty]"; - - ElementUtils.setText(e, text); + return text; + } + + @Override + public void load(ReadGraph graph, final ICanvasContext canvas, final IDiagram diagram, final Resource element, final IElement e) throws DatabaseException { + ElementUtils.setText(e, getText(graph, element)); getVisualProperties(graph, element, e); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/LoopTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/LoopTab.java index 272e98d0..27cc9a56 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/LoopTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/LoopTab.java @@ -129,6 +129,7 @@ public class LoopTab extends LabelPropertyTabContributor { @Override public void apply(WriteGraph graph, Resource connectionElement) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); + graph.deny(connectionElement, sr.Loop_Clockwise); graph.claimLiteral(connectionElement, sr.Loop_Clockwise, clockwise); } @@ -165,6 +166,7 @@ public class LoopTab extends LabelPropertyTabContributor { @Override public void apply(WriteGraph graph, Resource connectionElement) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); + graph.deny(connectionElement, sr.Loop_Comment); graph.claimLiteral(connectionElement, sr.Loop_Comment, comment.trim()); } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Loop.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Loop.java index fd4c6b4a..1345faa0 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Loop.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Loop.java @@ -31,10 +31,10 @@ public class Loop implements IElement { @RelatedElement(Layer0.URIs.PartOf) protected Object parent; - @RelatedElement(SysdynResource.URIs.Loop_Clockwise) - protected boolean clockwise; + @RelatedValue(SysdynResource.URIs.Loop_Clockwise) + protected Boolean clockwise; - @RelatedListElements(SysdynResource.URIs.Loop_Comment) + @RelatedValue(SysdynResource.URIs.Loop_Comment) protected String comment; @RelatedListElements(SysdynResource.URIs.Loop_Items)