From 13aaf2db7875eb3d91706851457cd1c9a5691eda Mon Sep 17 00:00:00 2001 From: lehtonen Date: Mon, 21 Jun 2010 10:02:31 +0000 Subject: [PATCH] Interim commit for sysdyn, cloud symbol added. git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@16253 ac1ea38d-2e2b-0410-8846-a27921b304fc --- org.simantics.sysdyn.ui/adapters.xml | 2 +- .../sysdyn/ui/editor/DiagramViewer.java | 10 -- .../sysdyn/ui/elements2/AuxiliaryFactory.java | 89 +----------- .../sysdyn/ui/elements2/CloudFactory.java | 127 ++++++++++++++++++ .../sysdyn/ui/elements2/StockFactory.java | 84 +----------- .../ui/elements2/SysdynElementFactory.java | 117 ++++++++++++++++ .../sysdyn/ui/elements2/ValveFactory.java | 94 ++----------- .../sysdyn/ui/project/SysdynProject.java | 7 +- .../org/simantics/sysdyn/SysdynResource.java | 3 + sysdyn_ontologies/sysdyn.graph | 23 +++- 10 files changed, 293 insertions(+), 263 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/CloudFactory.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/SysdynElementFactory.java diff --git a/org.simantics.sysdyn.ui/adapters.xml b/org.simantics.sysdyn.ui/adapters.xml index 582a8283..863bfe88 100644 --- a/org.simantics.sysdyn.ui/adapters.xml +++ b/org.simantics.sysdyn.ui/adapters.xml @@ -25,5 +25,5 @@ - + \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/DiagramViewer.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/DiagramViewer.java index b46b3274..72a5f8e6 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/DiagramViewer.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/DiagramViewer.java @@ -12,8 +12,6 @@ import org.simantics.diagram.synchronization.IModifiableSynchronizationContext; import org.simantics.g2d.canvas.ICanvasContext; import org.simantics.g2d.canvas.impl.CanvasContext; import org.simantics.g2d.diagram.participant.DeleteHandler; -import org.simantics.modeling.ui.diagramEditor.PopulateElementDropParticipant; -import org.simantics.modeling.ui.diagramEditor.PopulateElementMonitorDropParticipant; import org.simantics.modeling.ui.diagramEditor.handlers.WorkbenchStructuralSelectionProvider2; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.ui.properties.SysdynPropertyPage; @@ -49,14 +47,6 @@ public class DiagramViewer extends org.simantics.modeling.ui.diagramEditor.Diagr SysdynResource.getInstance(graph); } - @Override - protected void addDropParticipants(ICanvasContext ctx) { - // FIXME This is a workaround so that this participant can be disabled - // for SymbolViewer - ctx.add(new PopulateElementDropParticipant(synchronizer)); - ctx.add(new PopulateElementMonitorDropParticipant(synchronizer, 0.15, 0.15)); - } - @Override protected void addStructureParticipants(ICanvasContext ctx) { structuralPath = getResourceInput().getResourceArray().removeFromBeginning(1); 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 09f3595a..ef91c80a 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,33 +12,12 @@ package org.simantics.sysdyn.ui.elements2; import java.awt.BasicStroke; -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.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; @@ -52,68 +31,17 @@ 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; +/** + * @author Tuukka Lehtonen + */ +public class AuxiliaryFactory extends SysdynElementFactory { -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 { - 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); - } + // 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); @Override - public ElementClass create(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType) - throws DatabaseException { - 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)); - } - } - + protected ElementClass compileElementClass(Resource elementType, Collection terminals) { return ElementClass.compile( SimpleElementLayers.INSTANCE, OutlinePick.INSTANCE, @@ -130,7 +58,4 @@ public class AuxiliaryFactory extends SyncElementFactory { ).setId(AuxiliaryFactory.class.getSimpleName()); } - // 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/CloudFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/CloudFactory.java new file mode 100644 index 00000000..93c9c5bf --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/CloudFactory.java @@ -0,0 +1,127 @@ +/******************************************************************************* + * 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.sysdyn.ui.elements2; + +import java.awt.BasicStroke; +import java.awt.Color; +import java.awt.Shape; +import java.awt.geom.Path2D; +import java.awt.geom.Rectangle2D; +import java.util.Collection; + +import org.simantics.db.Resource; +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.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.scenegraph.g2d.G2DParentNode; +import org.simantics.scenegraph.g2d.nodes.ShapeNode; +import org.simantics.utils.datastructures.hints.IHintContext.Key; + +public class CloudFactory extends SysdynElementFactory { + + public static final double CLOUD_SIZE_X = 5.0; + public static final double CLOUD_SIZE_Y = 3.0; + public static final double CLOUD_CURVES = 7; + + private static final BasicStroke STROKE = new BasicStroke(1f); + private static final Image DEFAULT_IMAGE = new ShapeImage(getCloudShape(), null, STROKE, true); + + static Shape getCloudShape() { + Path2D path = new Path2D.Double(); + double ox = CLOUD_SIZE_X; + double oy = 0.0; + double posX = 0; + double posY = 0; + path.moveTo(posX + ox, posY + oy); + for (int i = 1; i < CLOUD_CURVES + 1; ++i) { + double angle = (Math.PI * 2.0 / CLOUD_CURVES) * i; + double x = Math.cos(angle) * CLOUD_SIZE_X; + double y = Math.sin(angle) * CLOUD_SIZE_Y; + path.curveTo( + posX + (2*ox + x)*0.5, posY + (2*oy + y)*0.5, + posX + (ox + 2*x)*0.5, posY + (oy + 2*y)*0.5, + posX + x, posY + y); + ox = x; + oy = y; + } + return path; + } + + @Override + protected ElementClass compileElementClass(Resource elementType, Collection terminals) { + return ElementClass.compile( + SimpleElementLayers.INSTANCE, + OutlinePick.INSTANCE, + TextImpl.INSTANCE, + TextColorImpl.BLACK, + TextFontImpl.DEFAULT, + DefaultTransform.INSTANCE, + new StaticObjectAdapter(elementType), + new StaticSymbolImpl(DEFAULT_IMAGE), + StaticSymbolImageInitializer.INSTANCE, + CloudSceneGraph.INSTANCE, + BoundsOutline.INSTANCE, + new WholeElementTerminals(terminals) + ).setId(CloudFactory.class.getSimpleName()); + } + + + public static class CloudSceneGraph implements SceneGraph, InternalSize { + + private static final long serialVersionUID = 5544256245734478634L; + + public static final CloudSceneGraph INSTANCE = new CloudSceneGraph(); + + 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); + + node.setStroke(STROKE); + node.setScaleStroke(true); + node.setColor(Color.BLACK); + node.setShape(getCloudShape()); + } + + @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(getCloudShape().getBounds2D()); + return size; + } + + } + +} 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 98f9ae1d..4f2e4905 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,33 +12,12 @@ package org.simantics.sysdyn.ui.elements2; import java.awt.BasicStroke; -import java.awt.geom.AffineTransform; 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.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; @@ -52,67 +31,14 @@ 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 StockFactory extends SyncElementFactory { +public class StockFactory extends SysdynElementFactory { - public static final IHintSynchronizer SYNCHRONIZER = new CompositeHintSynchronizer( - ComponentNameSynchronizer.INSTANCE, - TransformSynchronizer.INSTANCE); + private static final BasicStroke STROKE = new BasicStroke(1f); + private static final Image DEFAULT_IMAGE = new ShapeImage(new Rectangle2D.Double(-5, -2.5, 10, 5), null, STROKE, true); @Override - public void load(ReadGraph graph, final ICanvasContext canvas, final IDiagram diagram, final Resource element, final IElement e) throws DatabaseException { - 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 { - 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)); - } - } - + protected ElementClass compileElementClass(Resource elementType, Collection terminals) { return ElementClass.compile( SimpleElementLayers.INSTANCE, OutlinePick.INSTANCE, @@ -130,6 +56,4 @@ public class StockFactory extends SyncElementFactory { ).setId(StockFactory.class.getSimpleName()); } - 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/SysdynElementFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/SysdynElementFactory.java new file mode 100644 index 00000000..c3aad14d --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/SysdynElementFactory.java @@ -0,0 +1,117 @@ +/******************************************************************************* + * 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.sysdyn.ui.elements2; + +import java.awt.geom.AffineTransform; +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.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.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.ObjectTerminal; +import org.simantics.modeling.ModelingResources; +import org.simantics.structural.stubs.StructuralResource2; + +/** + * An ElementFactory that gathers common functionality for system dynamics symbols. + * Just implement {@link #compileElementClass(Resource, Collection)} to add a symbol. + * + * @author Tuukka Lehtonen + */ +public abstract class SysdynElementFactory 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 { + 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 { + 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 compileElementClass(elementType, terminals); + } + + /** + * @param elementType the type of the loaded element + * @param terminals the terminals of the element type being loaded + * @return an {@link ElementClass} representing the loaded element type + */ + protected abstract ElementClass compileElementClass(Resource elementType, Collection terminals); + +} 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 e38a31e2..030ff856 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 @@ -13,31 +13,12 @@ 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.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.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; @@ -59,76 +40,28 @@ 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 class ValveFactory extends SysdynElementFactory { - public static final Key KEY_ROTATED = new KeyOf(Boolean.class, "ROTATED"); + public static final Key KEY_ROTATED = new KeyOf(Boolean.class, "ROTATED"); - public static final double VALVE_SIZE = 2.5; + public static final double VALVE_SIZE = 2.5; - public static final IHintSynchronizer SYNCHRONIZER = new CompositeHintSynchronizer( - ComponentNameSynchronizer.INSTANCE, - TransformSynchronizer.INSTANCE); + private static final ElementHandler TEXT_NODE = new TextElementNoBounds(0, VALVE_SIZE + 2, Alignment.CENTER); - @Override - public void load(ReadGraph graph, final ICanvasContext canvas, final IDiagram diagram, final Resource element, final IElement e) throws DatabaseException { - 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); - } + private static final BasicStroke STROKE = new BasicStroke(1f); + private static final Image VALVE_STATIC_IMAGE = new ShapeImage(createShape(VALVE_SIZE, false), null, STROKE, true); + /* (non-Javadoc) + * @see org.simantics.sysdyn.ui.elements2.SysdynElementFactory#compileElementClass(org.simantics.db.Resource, java.util.Collection) + */ @Override - public ElementClass create(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType) - throws DatabaseException { - 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)); - } - } - + protected ElementClass compileElementClass(Resource elementType, Collection terminals) { return ElementClass.compile( SimpleElementLayers.INSTANCE, OutlinePick.INSTANCE, @@ -146,11 +79,6 @@ public class ValveFactory extends SyncElementFactory { ).setId(ValveFactory.class.getSimpleName()); } - 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 * @param rotated true for vertical valve, false @@ -184,13 +112,11 @@ public class ValveFactory extends SyncElementFactory { 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 diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/SysdynProject.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/SysdynProject.java index 9634c86e..8a4a8a0c 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/SysdynProject.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/SysdynProject.java @@ -87,8 +87,11 @@ public class SysdynProject extends AbstractProjectFeature { ModelingUtils mu = new ModelingUtils(g); Resource model = g.newResource(); - g.claimValue(model, b.HasName, "Model1"); - g.claimValue(model, b.HasLabel, "Model1"); + + String modelName = GraphUtils.findFreshName(g, "Model", library, b.ConsistsOf, "%s%d"); + + g.claimValue(model, b.HasName, modelName); + g.claimValue(model, b.HasLabel, modelName); g.claim(model, b.InstanceOf, sr.SysdynModel); g.claim(model, b.PartOf, library); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java index 0377b37a..b745a584 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java @@ -24,6 +24,7 @@ public class SysdynResource { public final Resource AuxiliarySymbol; public final Resource BasicExperiment; public final Resource Cloud; + public final Resource CloudSymbol; public final Resource Configuration; public final Resource ConfigurationDiagram; public final Resource ConstantExpression; @@ -89,6 +90,7 @@ public class SysdynResource { public static final String AuxiliarySymbol = "http://www.simantics.org/Sysdyn-1.0/AuxiliarySymbol"; public static final String BasicExperiment = "http://www.simantics.org/Sysdyn-1.0/BasicExperiment"; public static final String Cloud = "http://www.simantics.org/Sysdyn-1.0/Cloud"; + public static final String CloudSymbol = "http://www.simantics.org/Sysdyn-1.0/CloudSymbol"; public static final String Configuration = "http://www.simantics.org/Sysdyn-1.0/Configuration"; public static final String ConfigurationDiagram = "http://www.simantics.org/Sysdyn-1.0/ConfigurationDiagram"; public static final String ConstantExpression = "http://www.simantics.org/Sysdyn-1.0/ConstantExpression"; @@ -164,6 +166,7 @@ public class SysdynResource { AuxiliarySymbol = getResourceOrNull(graph, URIs.AuxiliarySymbol); BasicExperiment = getResourceOrNull(graph, URIs.BasicExperiment); Cloud = getResourceOrNull(graph, URIs.Cloud); + CloudSymbol = getResourceOrNull(graph, URIs.CloudSymbol); Configuration = getResourceOrNull(graph, URIs.Configuration); ConfigurationDiagram = getResourceOrNull(graph, URIs.ConfigurationDiagram); ConstantExpression = getResourceOrNull(graph, URIs.ConstantExpression); diff --git a/sysdyn_ontologies/sysdyn.graph b/sysdyn_ontologies/sysdyn.graph index 01475b73..16f50640 100644 --- a/sysdyn_ontologies/sysdyn.graph +++ b/sysdyn_ontologies/sysdyn.graph @@ -353,9 +353,9 @@ ValveSymbol