From: lehtonen Date: Sun, 20 Jun 2010 10:14:38 +0000 (+0000) Subject: Basic stock, aux and valve symbols for sysdyn. X-Git-Tag: simantics-1.2.0~204 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=fcb8558b7017a20b24c88b08a18f6cb7c7520dc0;p=simantics%2Fsysdyn.git Basic stock, aux and valve symbols for sysdyn. Sysdyn-specific connections still missing completely. git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@16247 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/AuxiliaryFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/AuxiliaryFactory.java index d946c883..09f3595a 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/AuxiliaryFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/AuxiliaryFactory.java @@ -12,54 +12,125 @@ package org.simantics.sysdyn.ui.elements2; import java.awt.BasicStroke; -import java.awt.geom.Rectangle2D; +import java.awt.geom.AffineTransform; +import java.awt.geom.Ellipse2D; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import org.simantics.db.Builtins; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; -import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.DatabaseException; +import org.simantics.diagram.G2DUtils; import org.simantics.diagram.adapter.SyncElementFactory; +import org.simantics.diagram.content.ResourceTerminal; import org.simantics.diagram.elements.TextElementHandler; +import org.simantics.diagram.stubs.DiagramResource; +import org.simantics.diagram.stubs.G2DResource; +import org.simantics.diagram.synchronization.CompositeHintSynchronizer; +import org.simantics.diagram.synchronization.IHintSynchronizer; +import org.simantics.diagram.synchronization.SynchronizationHints; +import org.simantics.diagram.synchronization.graph.DiagramGraphUtil; +import org.simantics.diagram.synchronization.graph.TransformSynchronizer; import org.simantics.g2d.canvas.ICanvasContext; import org.simantics.g2d.diagram.IDiagram; import org.simantics.g2d.element.ElementClass; +import org.simantics.g2d.element.ElementUtils; import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.handler.impl.BoundsOutline; import org.simantics.g2d.element.handler.impl.DefaultTransform; +import org.simantics.g2d.element.handler.impl.ObjectTerminal; +import org.simantics.g2d.element.handler.impl.OutlinePick; import org.simantics.g2d.element.handler.impl.SimpleElementLayers; import org.simantics.g2d.element.handler.impl.StaticObjectAdapter; +import org.simantics.g2d.element.handler.impl.StaticSymbolImageInitializer; import org.simantics.g2d.element.handler.impl.StaticSymbolImpl; import org.simantics.g2d.element.handler.impl.TextColorImpl; import org.simantics.g2d.element.handler.impl.TextFontImpl; import org.simantics.g2d.element.handler.impl.TextImpl; import org.simantics.g2d.image.Image; import org.simantics.g2d.image.impl.ShapeImage; +import org.simantics.modeling.ModelingResources; +import org.simantics.structural.stubs.StructuralResource2; public class AuxiliaryFactory extends SyncElementFactory { + public static final IHintSynchronizer SYNCHRONIZER = new CompositeHintSynchronizer( + ComponentNameSynchronizer.INSTANCE, + TransformSynchronizer.INSTANCE); + @Override public void load(ReadGraph graph, final ICanvasContext canvas, final IDiagram diagram, final Resource element, final IElement e) throws DatabaseException { - // TODO: implement + Builtins b = graph.getBuiltins(); + G2DResource g2d = G2DResource.getInstance(graph); + DiagramResource dr = DiagramResource.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, b.HasName); + } + if (text == null) + text = "[empty]"; + + ElementUtils.setText(e, text); + + if (graph.isInstanceOf(element, dr.FontProvider)) { + Resource fontResource = graph.getPossibleObject(element, g2d.HasFont); + if (fontResource != null) + ElementUtils.setTextFont(e, G2DUtils.getFont(graph, fontResource)); + } + if (graph.isInstanceOf(element, dr.ColorProvider)) { + Resource colorResource = graph.getPossibleObject(element, g2d.HasColor); + if (colorResource != null) + ElementUtils.setTextColor(e, G2DUtils.getColor(graph, colorResource)); + } + + AffineTransform at = DiagramGraphUtil.getAffineTransform(graph, element); + ElementUtils.setTransform(e, at); + + // This synchronizes only text and transformation (not font and color) + e.setHint(SynchronizationHints.HINT_SYNCHRONIZER, SYNCHRONIZER); } @Override public ElementClass create(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType) throws DatabaseException { - String id = "Auxiliary: " + NameUtils.getSafeName(graph, elementType); + StructuralResource2 sr = StructuralResource2.getInstance(graph); + DiagramResource dr = DiagramResource.getInstance(graph); + + Resource definedByList = graph.getPossibleObject(elementType, sr.IsDefinedBy); + Collection definedBy = Collections.emptyList(); + if (definedByList != null) + definedBy = OrderedSetUtils.toList(graph, definedByList); + Collection terminals = new ArrayList(definedBy.size()); + for (Resource r : definedBy) { + if (graph.isInstanceOf(r, dr.Terminal)) { + terminals.add(new ResourceTerminal(r)); + } + } + return ElementClass.compile( + SimpleElementLayers.INSTANCE, + OutlinePick.INSTANCE, TextImpl.INSTANCE, TextColorImpl.BLACK, TextFontImpl.DEFAULT, - new StaticObjectAdapter(elementType), DefaultTransform.INSTANCE, - SimpleElementLayers.INSTANCE, + new StaticObjectAdapter(elementType), + new StaticSymbolImpl(AUX_STATIC_IMAGE), + StaticSymbolImageInitializer.INSTANCE, TextElementHandler.INSTANCE, - new StaticSymbolImpl(AUX_STATIC_IMAGE) - ) - .setId(id) - ; + BoundsOutline.INSTANCE, + new WholeElementTerminals(terminals) + ).setId(AuxiliaryFactory.class.getSimpleName()); } - private static final Image AUX_STATIC_IMAGE = new ShapeImage(new Rectangle2D.Double(-5, -2, 10, 4), null, new BasicStroke(1)); + // TODO: make the static symbol image a text "AUX" + private static final Image AUX_STATIC_IMAGE = new ShapeImage(new Ellipse2D.Double(-5, -2, 10, 4), null, new BasicStroke(0.1f), true); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/BorderSceneGraph.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/BorderSceneGraph.java new file mode 100644 index 00000000..1a6b388b --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/BorderSceneGraph.java @@ -0,0 +1,38 @@ +package org.simantics.sysdyn.ui.elements2; + +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.h2d.node.RectangleNode; +import org.simantics.scenegraph.g2d.G2DParentNode; +import org.simantics.utils.datastructures.Callback; +import org.simantics.utils.datastructures.hints.IHintContext.Key; + +public class BorderSceneGraph implements SceneGraph, Callback { + + public static final BorderSceneGraph INSTANCE = new BorderSceneGraph(); + + private static final long serialVersionUID = 5544256245734478634L; + + private static final Key BORDER_NODE = new SceneGraphNodeKey(RectangleNode.class, "BORDER_NODE"); + + @Override + public void init(IElement e, G2DParentNode parent) { + RectangleNode node = ElementUtils.getOrCreateNode(e, parent, BORDER_NODE, "border", RectangleNode.class, this); + + // Calculate borders from text node bounds. + node.init(ElementUtils.getElementBounds(e)); + } + + @Override + public void run(RectangleNode node) { + node.setZIndex(-10); + } + + @Override + public void cleanup(IElement e) { + ElementUtils.removePossibleNode(e, BORDER_NODE); + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/StockFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/StockFactory.java index 6c1941c8..98f9ae1d 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/StockFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/StockFactory.java @@ -12,7 +12,6 @@ package org.simantics.sysdyn.ui.elements2; import java.awt.BasicStroke; -import java.awt.Shape; import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; import java.util.ArrayList; @@ -40,9 +39,7 @@ import org.simantics.g2d.diagram.IDiagram; import org.simantics.g2d.element.ElementClass; import org.simantics.g2d.element.ElementUtils; import org.simantics.g2d.element.IElement; -import org.simantics.g2d.element.SceneGraphNodeKey; -import org.simantics.g2d.element.handler.Outline; -import org.simantics.g2d.element.handler.SceneGraph; +import org.simantics.g2d.element.handler.impl.BoundsOutline; import org.simantics.g2d.element.handler.impl.DefaultTransform; import org.simantics.g2d.element.handler.impl.ObjectTerminal; import org.simantics.g2d.element.handler.impl.OutlinePick; @@ -55,12 +52,8 @@ import org.simantics.g2d.element.handler.impl.TextFontImpl; import org.simantics.g2d.element.handler.impl.TextImpl; import org.simantics.g2d.image.Image; import org.simantics.g2d.image.impl.ShapeImage; -import org.simantics.h2d.node.RectangleNode; import org.simantics.modeling.ModelingResources; -import org.simantics.scenegraph.g2d.G2DParentNode; import org.simantics.structural.stubs.StructuralResource2; -import org.simantics.utils.datastructures.Callback; -import org.simantics.utils.datastructures.hints.IHintContext.Key; public class StockFactory extends SyncElementFactory { @@ -115,7 +108,7 @@ public class StockFactory extends SyncElementFactory { definedBy = OrderedSetUtils.toList(graph, definedByList); Collection terminals = new ArrayList(definedBy.size()); for (Resource r : definedBy) { - if (graph.isInheritedFrom(r, dr.Terminal)) { + if (graph.isInstanceOf(r, dr.Terminal)) { terminals.add(new ResourceTerminal(r)); } } @@ -137,36 +130,6 @@ public class StockFactory extends SyncElementFactory { ).setId(StockFactory.class.getSimpleName()); } - private static final Image DEFAULT_IMAGE = new ShapeImage(new Rectangle2D.Double(-5, -5, 10, 10), null, new BasicStroke(1.0f), Image.VECTOR); - - static class BoundsOutline implements Outline { - public static final BoundsOutline INSTANCE = new BoundsOutline(); - private static final long serialVersionUID = 5544256245734478634L; - @Override - public Shape getElementShape(IElement e) { - return ElementUtils.getElementBounds(e); - } - } - - static class BorderSceneGraph implements SceneGraph, Callback { - public static final BorderSceneGraph INSTANCE = new BorderSceneGraph(); - private static final long serialVersionUID = 5544256245734478634L; - private static final Key BORDER_NODE = new SceneGraphNodeKey(RectangleNode.class, "BORDER_NODE"); - @Override - public void init(IElement e, G2DParentNode parent) { - RectangleNode node = ElementUtils.getOrCreateNode(e, parent, BORDER_NODE, "border", RectangleNode.class, this); - - // Calculate borders from text node bounds. - node.init(ElementUtils.getElementBounds(e)); - } - @Override - public void run(RectangleNode node) { - node.setZIndex(-10); - } - @Override - public void cleanup(IElement e) { - ElementUtils.removePossibleNode(e, BORDER_NODE); - } - } + private static final Image DEFAULT_IMAGE = new ShapeImage(new Rectangle2D.Double(-5, -2.5, 10, 5), null, new BasicStroke(0.1f), Image.VECTOR); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ValveFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ValveFactory.java index 817c57bb..e38a31e2 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ValveFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ValveFactory.java @@ -12,57 +12,144 @@ package org.simantics.sysdyn.ui.elements2; import java.awt.BasicStroke; +import java.awt.Color; +import java.awt.geom.AffineTransform; import java.awt.geom.Path2D; +import java.awt.geom.Rectangle2D; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import org.simantics.db.Builtins; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; -import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.DatabaseException; +import org.simantics.diagram.G2DUtils; import org.simantics.diagram.adapter.SyncElementFactory; -import org.simantics.diagram.elements.TextElementHandler; +import org.simantics.diagram.content.ResourceTerminal; +import org.simantics.diagram.elements.TextElementNoBounds; +import org.simantics.diagram.stubs.DiagramResource; +import org.simantics.diagram.stubs.G2DResource; +import org.simantics.diagram.synchronization.CompositeHintSynchronizer; +import org.simantics.diagram.synchronization.IHintSynchronizer; +import org.simantics.diagram.synchronization.SynchronizationHints; +import org.simantics.diagram.synchronization.graph.DiagramGraphUtil; +import org.simantics.diagram.synchronization.graph.TransformSynchronizer; import org.simantics.g2d.canvas.ICanvasContext; import org.simantics.g2d.diagram.IDiagram; import org.simantics.g2d.element.ElementClass; +import org.simantics.g2d.element.ElementUtils; import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.SceneGraphNodeKey; +import org.simantics.g2d.element.handler.ElementHandler; +import org.simantics.g2d.element.handler.InternalSize; +import org.simantics.g2d.element.handler.SceneGraph; +import org.simantics.g2d.element.handler.impl.BoundsOutline; import org.simantics.g2d.element.handler.impl.DefaultTransform; +import org.simantics.g2d.element.handler.impl.ObjectTerminal; +import org.simantics.g2d.element.handler.impl.OutlinePick; import org.simantics.g2d.element.handler.impl.SimpleElementLayers; import org.simantics.g2d.element.handler.impl.StaticObjectAdapter; +import org.simantics.g2d.element.handler.impl.StaticSymbolImageInitializer; import org.simantics.g2d.element.handler.impl.StaticSymbolImpl; import org.simantics.g2d.element.handler.impl.TextColorImpl; import org.simantics.g2d.element.handler.impl.TextFontImpl; import org.simantics.g2d.element.handler.impl.TextImpl; import org.simantics.g2d.image.Image; import org.simantics.g2d.image.impl.ShapeImage; +import org.simantics.g2d.utils.Alignment; +import org.simantics.modeling.ModelingResources; +import org.simantics.scenegraph.g2d.G2DParentNode; +import org.simantics.scenegraph.g2d.nodes.ShapeNode; +import org.simantics.structural.stubs.StructuralResource2; +import org.simantics.utils.datastructures.hints.IHintContext.Key; +import org.simantics.utils.datastructures.hints.IHintContext.KeyOf; public class ValveFactory extends SyncElementFactory { - public static final double VALVE_SIZE = 5.0; + public static final Key KEY_ROTATED = new KeyOf(Boolean.class, "ROTATED"); + + public static final double VALVE_SIZE = 2.5; + + public static final IHintSynchronizer SYNCHRONIZER = new CompositeHintSynchronizer( + ComponentNameSynchronizer.INSTANCE, + TransformSynchronizer.INSTANCE); @Override public void load(ReadGraph graph, final ICanvasContext canvas, final IDiagram diagram, final Resource element, final IElement e) throws DatabaseException { - // TODO: implement + Builtins b = graph.getBuiltins(); + G2DResource g2d = G2DResource.getInstance(graph); + DiagramResource dr = DiagramResource.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, b.HasName); + } + if (text == null) + text = "[empty]"; + + ElementUtils.setText(e, text); + + if (graph.isInstanceOf(element, dr.FontProvider)) { + Resource fontResource = graph.getPossibleObject(element, g2d.HasFont); + if (fontResource != null) + ElementUtils.setTextFont(e, G2DUtils.getFont(graph, fontResource)); + } + if (graph.isInstanceOf(element, dr.ColorProvider)) { + Resource colorResource = graph.getPossibleObject(element, g2d.HasColor); + if (colorResource != null) + ElementUtils.setTextColor(e, G2DUtils.getColor(graph, colorResource)); + } + + AffineTransform at = DiagramGraphUtil.getAffineTransform(graph, element); + ElementUtils.setTransform(e, at); + + // This synchronizes only text and transformation (not font and color) + e.setHint(SynchronizationHints.HINT_SYNCHRONIZER, SYNCHRONIZER); } @Override public ElementClass create(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType) throws DatabaseException { - String id = "Valve: " + NameUtils.getSafeName(graph, elementType); + StructuralResource2 sr = StructuralResource2.getInstance(graph); + DiagramResource dr = DiagramResource.getInstance(graph); + + Resource definedByList = graph.getPossibleObject(elementType, sr.IsDefinedBy); + Collection definedBy = Collections.emptyList(); + if (definedByList != null) + definedBy = OrderedSetUtils.toList(graph, definedByList); + Collection terminals = new ArrayList(definedBy.size()); + for (Resource r : definedBy) { + if (graph.isInstanceOf(r, dr.Terminal)) { + terminals.add(new ResourceTerminal(r)); + } + } + return ElementClass.compile( + SimpleElementLayers.INSTANCE, + OutlinePick.INSTANCE, TextImpl.INSTANCE, TextColorImpl.BLACK, TextFontImpl.DEFAULT, - new StaticObjectAdapter(elementType), DefaultTransform.INSTANCE, - SimpleElementLayers.INSTANCE, - TextElementHandler.INSTANCE, - new StaticSymbolImpl(VALVE_STATIC_IMAGE) - ) - .setId(id) - ; + new StaticObjectAdapter(elementType), + new StaticSymbolImpl(VALVE_STATIC_IMAGE), + StaticSymbolImageInitializer.INSTANCE, + TEXT_NODE, + ValveSceneGraph.INSTANCE, + BoundsOutline.INSTANCE, + new WholeElementTerminals(terminals) + ).setId(ValveFactory.class.getSimpleName()); } - private static final Image VALVE_STATIC_IMAGE = new ShapeImage(createShape(VALVE_SIZE, false), null, new BasicStroke(1)); + private static final ElementHandler TEXT_NODE = new TextElementNoBounds(0, VALVE_SIZE+2, Alignment.CENTER); + + private static final BasicStroke STROKE = new BasicStroke(1f); + private static final Image VALVE_STATIC_IMAGE = new ShapeImage(createShape(VALVE_SIZE, false), null, STROKE, true); /** * @param valveSize @@ -85,4 +172,40 @@ public class ValveFactory extends SyncElementFactory { return path; } + public static class ValveSceneGraph implements SceneGraph, InternalSize { + + private static final long serialVersionUID = 5544256245734478634L; + + public static final ValveSceneGraph INSTANCE = new ValveSceneGraph(); + + private static final Key NODE = new SceneGraphNodeKey(ShapeNode.class, "VALVE_NODE"); + + @Override + public void init(IElement e, G2DParentNode parent) { + ShapeNode node = ElementUtils.getOrCreateNode(e, parent, NODE, "valve", ShapeNode.class); + + System.out.println("shape node : " + node); + // Calculate borders from text node bounds. + node.setStroke(STROKE); + node.setScaleStroke(true); + node.setColor(Color.BLACK); + node.setShape(createShape(VALVE_SIZE, Boolean.TRUE.equals(e.getHint(KEY_ROTATED)))); + System.out.println("shape node after init: " + node); + } + + @Override + public void cleanup(IElement e) { + ElementUtils.removePossibleNode(e, NODE); + } + + @Override + public Rectangle2D getBounds(IElement e, Rectangle2D size) { + if (size == null) + size = new Rectangle2D.Double(); + size.setFrame(createShape(VALVE_SIZE, Boolean.TRUE.equals(e.getHint(KEY_ROTATED))).getBounds2D()); + return size; + } + + } + }