From: lehtonen Date: Tue, 15 Jun 2010 20:22:53 +0000 (+0000) Subject: Working on g2d-based system dynamics editing. X-Git-Tag: simantics-1.2.0~213 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=d0dc583783210d950845760697e98c46af4d41c7;p=simantics%2Fsysdyn.git Working on g2d-based system dynamics editing. Lots of small changes all over, shouldn't break anything. git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@16170 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ui/adapters.xml b/org.simantics.sysdyn.ui/adapters.xml index 4b552959..582a8283 100644 --- a/org.simantics.sysdyn.ui/adapters.xml +++ b/org.simantics.sysdyn.ui/adapters.xml @@ -16,7 +16,7 @@ - + - + \ 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 54e8ba08..b46b3274 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 @@ -1,5 +1,10 @@ package org.simantics.sysdyn.ui.editor; +import java.util.Collections; +import java.util.Set; + +import org.eclipse.ui.IWorkbenchPartSite; +import org.simantics.browsing.ui.swt.IPropertyPage; import org.simantics.db.ReadGraph; import org.simantics.diagram.handler.ConnectionCommandHandler; import org.simantics.diagram.handler.CopyPasteHandler; @@ -10,18 +15,22 @@ 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; /** * @author Tuukka Lehtonen */ public class DiagramViewer extends org.simantics.modeling.ui.diagramEditor.DiagramViewer { - @SuppressWarnings("unchecked") @Override - public Object getAdapter(Class adapter) { -// if (adapter == IPropertyPage.class) -// return new AprosPropertyPage(getSite()); - return super.getAdapter(adapter); + protected Set getPropertyPageContexts() { + return Collections.singleton("http://www.simantics.org/Sysdyn-1.0/Browser"); + } + + @Override + protected IPropertyPage createPropertyPage(IWorkbenchPartSite site, Set contexts) { + return new SysdynPropertyPage(site, contexts); } @Override @@ -35,7 +44,9 @@ public class DiagramViewer extends org.simantics.modeling.ui.diagramEditor.Diagr @Override protected void initializeSynchronizationContext(ReadGraph graph, IModifiableSynchronizationContext context) { super.initializeSynchronizationContext(graph, context); -// context.set(AprosDiagramSynchronizationHints.APROS_RESOURCE, AprosBuiltins.getInstance(graph)); + + // Make sure SysdynResource is available. + SysdynResource.getInstance(graph); } @Override @@ -49,11 +60,7 @@ public class DiagramViewer extends org.simantics.modeling.ui.diagramEditor.Diagr @Override protected void addStructureParticipants(ICanvasContext ctx) { structuralPath = getResourceInput().getResourceArray().removeFromBeginning(1); - ctx.add(new WorkbenchStructuralSelectionProvider2(swt, getSite(), structuralPath)); - // Add visual browsing capabilities for structural models -// ctx.add(new StructuralBrowsingHandler(getSite(), sessionContext, structuralPath)); -// ctx.add(new LinkBrowsingHandler(getSite(), this, sessionContext)); } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SymbolsFeature.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SymbolsFeature.java deleted file mode 100644 index 6f2d1d7f..00000000 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SymbolsFeature.java +++ /dev/null @@ -1,336 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2007 VTT Technical Research Centre of Finland and others. - * 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.editor; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - -import org.simantics.db.ReadGraph; -import org.simantics.db.RequestProcessor; -import org.simantics.db.Resource; -import org.simantics.db.common.procedure.adapter.MultiProcedureAdapter; -import org.simantics.db.common.request.ResourceRead; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.exception.ResourceNotFoundException; -import org.simantics.db.request.Read; -import org.simantics.diagram.query.DiagramRequests; -import org.simantics.diagram.stubs.DiagramResource; -import org.simantics.diagram.symbollibrary.ISymbolGroup; -import org.simantics.diagram.symbollibrary.ISymbolItem; -import org.simantics.diagram.symbollibrary.ISymbolManager; -import org.simantics.g2d.element.ElementClass; -import org.simantics.g2d.element.handler.StaticSymbol; -import org.simantics.layer0.utils.direct.GraphUtils; -import org.simantics.layer0.utils.direct.GraphUtils.ResourceTester; -import org.simantics.project.IProject; -import org.simantics.project.exception.ProjectException; -import org.simantics.project.features.AbstractProjectFeature; -import org.simantics.project.features.IGraphProjectFeature; -import org.simantics.utils.datastructures.cache.ProvisionException; -import org.simantics.utils.datastructures.hints.IHintObservable; - -/** - * @author Tuukka Lehtonen - * - * TODO: live tracking of symbol libraries based on selected model, since they can contain their own local symbols - * Currently the code only provides the symbols that are in the ontology - */ -public class SymbolsFeature extends AbstractProjectFeature implements IGraphProjectFeature { - - Collection symbolGroups = Collections.emptySet(); - - Collection toSymbolGroups(Resource... rs) { - Collection result = new ArrayList(rs.length); - for (int i = 0; i < rs.length; ++i) { - result.add(new SymbolGroup(rs[i])); - } - return result; - } - - @Override - public void configure(ReadGraph g) throws ProjectException { -// IProject p = getProject(); -// ISymbolManager sm = p.getHint(ISymbolManager.KEY_SYMBOL_MANAGER); -// if (sm != null) { -// Collection grps; -// try { -// grps = g.syncRequest(new GroupDiscoveryQuery(p)); -// AprosSymbolsFeature.this.symbolGroups = toSymbolGroups(grps.toArray(Resource.NONE)); -// sm.addEntryPoints(symbolGroups); -// } catch (DatabaseException e) { -// throw new ProjectException(e); -// } -// } - } - - @Override - public void deconfigure() throws ProjectException { - ISymbolManager sm = getProject().getHint(ISymbolManager.KEY_SYMBOL_MANAGER); - if (sm != null) { - sm.removeEntryPoints(symbolGroups); - symbolGroups = Collections.emptySet(); - } - } - - public class SymbolGroup implements ISymbolGroup { - final Resource r; - - public SymbolGroup(Resource r) { - this.r = r; - } - - @Override - public ISymbolItem[] getItems() { - RequestProcessor sgrp = getGraphRequestProcessor(); - IProject project = peekProject(); - if (project == null || sgrp == null) - return new ISymbolItem[0]; - Collection items; - try { - items = sgrp.syncRequest(new ElementDiscoveryQuery(r)); - List result = new ArrayList(); - for (Resource r : items) { - result.add(new SymbolItem(this, r)); - } - Collections.sort(result, new Comparator() { - @Override - public int compare(ISymbolItem o1, ISymbolItem o2) { - String n1 = o1.getName(); - String n2 = o2.getName(); - return n1.compareTo(n2); - } - }); - return result.toArray(new ISymbolItem[result.size()]); - } catch (DatabaseException e) { - throw new RuntimeException(e); - } - } - - @Override - public String getName() { - try { - RequestProcessor sgrp = getGraphRequestProcessor(); - IProject project = peekProject(); - if (project == null || sgrp == null) - return "(no project)"; - return sgrp.syncRequest(new SafeLabelQuery(r)); - } catch (DatabaseException e) { - throw new RuntimeException(e); - } - } - - @Override - public String getDescription() { - return getName(); - } - - @Override - public int hashCode() { - return r.hashCode(); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - SymbolGroup other = (SymbolGroup) obj; - return r.equals(other.r); - } - } - - public class SymbolItem implements ISymbolItem { - - final ISymbolGroup group; - final Resource r; - - public SymbolItem(ISymbolGroup group, Resource r) { - this.group = group; - this.r = r; - } - - @Override - public ISymbolGroup getGroup() { - return group; - } - - @Override - public ElementClass getElementClass(IHintObservable hints) { - RequestProcessor sgrp = getGraphRequestProcessor(); - if (sgrp == null) - throw new ProvisionException("No RequestProcessor available for querying an ElementClass for resource " + r); - try { - ElementClass ec = sgrp.syncRequest(DiagramRequests.getElementClass(r, hints)); - if (ec == null) - throw new ProvisionException("ElementClass query failed, returned null"); - if (!ec.containsClass(StaticSymbol.class)) - throw new ProvisionException("ElementClass " + ec + " does not provide a StaticSymbol handler"); - return ec; - } catch (DatabaseException e) { - throw new ProvisionException(e); - } - } - - @Override - public String getName() { - try { - RequestProcessor sgrp = getGraphRequestProcessor(); - // Return a safe "" if the query cannot be performed. - return sgrp != null ? sgrp.syncRequest(new SafeLabelQuery(r)) : ""; - } catch (DatabaseException e) { - throw new RuntimeException(e); - } - } - - @Override - public String getDescription() { - return getName(); - } - - @SuppressWarnings("unchecked") - @Override - public Object getAdapter(Class adapter) { - //if (adapter == ElementClass.class) - // return getElementClass(); - if (adapter == Resource.class) - return r; - return null; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + group.hashCode(); - result = prime * result + r.hashCode(); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - SymbolItem other = (SymbolItem) obj; - if (!group.equals(other.group)) - return false; - return r.equals(other.r); - } - } - - static class SafeLabelQuery extends ResourceRead { - public SafeLabelQuery(Resource resource) { - super(resource); - } - - @Override - public String perform(ReadGraph g) throws DatabaseException { - try { - return g.adapt(resource, String.class); - } catch (DatabaseException e) { - return GraphUtils.getReadableName(g, resource); - } - } - } - - public static class GroupDiscoveryQuery implements Read> { - - IProject project; - - GroupDiscoveryQuery(IProject project) { - assert project != null; - this.project = project; - } - - @Override - public Collection perform(ReadGraph g) throws DatabaseException { - try { - Resource[] eps = new Resource[] { - g.getResource("http://www.simantics.org/Sysdyn-1.0/SymbolReferences") - }; - - final DiagramResource dr = DiagramResource.getInstance(g); - final ArrayList groups = new ArrayList(); - for (Resource ep : eps) { - GraphUtils.findResources(g, Collections.singletonList(ep), g.getBuiltins().ConsistsOf, new ResourceTester() { - @Override - public boolean test(ReadGraph g, Resource r) throws DatabaseException { - return g.isInstanceOf(r, g.getBuiltins().Library) - || g.isInstanceOf(r, dr.SymbolReferenceLibrary); - } - }, new MultiProcedureAdapter() { - @Override - public void execute(Resource result) { - groups.add(result); - } - @Override - public void exception(Throwable t) { - new Exception().printStackTrace(); - t.printStackTrace(); - } - }); - } - return groups; - - } catch (ResourceNotFoundException e) { - throw new RuntimeException(e); - } - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null || getClass() != obj.getClass()) - return false; - GroupDiscoveryQuery other = (GroupDiscoveryQuery) obj; - if (!project.equals(other.project)) - return false; - return true; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((project == null) ? 0 : project.hashCode()); - return result; - } - } - - static public class ElementDiscoveryQuery extends ResourceRead> { - public ElementDiscoveryQuery(Resource resource) { - super(resource); - } - @Override - public Collection perform(ReadGraph g) throws DatabaseException { - DiagramResource dr = DiagramResource.getInstance(g); - Collection objs = g.getObjects(resource, g.getBuiltins().DependsOn); - // Filter out everything besides Elements. - ArrayList result = new ArrayList(objs.size()); - for (Resource r : objs) { - if (g.isInheritedFrom(r, dr.Element)) - result.add( r ); - } - return result; - } - } - -} 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 new file mode 100644 index 00000000..d946c883 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/AuxiliaryFactory.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * 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.geom.Rectangle2D; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.diagram.adapter.SyncElementFactory; +import org.simantics.diagram.elements.TextElementHandler; +import org.simantics.g2d.canvas.ICanvasContext; +import org.simantics.g2d.diagram.IDiagram; +import org.simantics.g2d.element.ElementClass; +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.handler.impl.DefaultTransform; +import org.simantics.g2d.element.handler.impl.SimpleElementLayers; +import org.simantics.g2d.element.handler.impl.StaticObjectAdapter; +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; + + +public class AuxiliaryFactory extends SyncElementFactory { + + @Override + public void load(ReadGraph graph, final ICanvasContext canvas, final IDiagram diagram, final Resource element, final IElement e) throws DatabaseException { + // TODO: implement + } + + @Override + public ElementClass create(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType) + throws DatabaseException { + String id = "Auxiliary: " + NameUtils.getSafeName(graph, elementType); + return ElementClass.compile( + TextImpl.INSTANCE, + TextColorImpl.BLACK, + TextFontImpl.DEFAULT, + new StaticObjectAdapter(elementType), + DefaultTransform.INSTANCE, + SimpleElementLayers.INSTANCE, + TextElementHandler.INSTANCE, + new StaticSymbolImpl(AUX_STATIC_IMAGE) + ) + .setId(id) + ; + } + + private static final Image AUX_STATIC_IMAGE = new ShapeImage(new Rectangle2D.Double(-5, -2, 10, 4), null, new BasicStroke(1)); + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ComponentNameSynchronizer.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ComponentNameSynchronizer.java new file mode 100644 index 00000000..29b118f3 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ComponentNameSynchronizer.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * 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 org.simantics.databoard.binding.java.StringJavaBinding; +import org.simantics.db.Builtins; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.diagram.synchronization.IModificationQueue; +import org.simantics.diagram.synchronization.ISynchronizationContext; +import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints; +import org.simantics.diagram.synchronization.graph.RelatedPropertyModification; +import org.simantics.diagram.synchronization.graph.ResourceSynchronizer; +import org.simantics.g2d.element.ElementHints; +import org.simantics.modeling.ModelingResources; +import org.simantics.utils.datastructures.hints.IHintContext.Key; +import org.simantics.utils.datastructures.hints.IHintObservable; + +/** + * @author Tuukka Lehtonen + */ +public class ComponentNameSynchronizer extends ResourceSynchronizer { + + public static final ComponentNameSynchronizer INSTANCE = new ComponentNameSynchronizer(); + + private static final Key[] SYNCHRONIZED_HINTS = { + ElementHints.KEY_TEXT + }; + + @Override + public Key[] getSynchronizedHints() { + return SYNCHRONIZED_HINTS; + } + + @Override + public boolean hintChanged(ISynchronizationContext context, IModificationQueue queue, Resource object, IHintObservable sender, Key key, Object oldValue, Object newValue) { + if (ElementHints.KEY_TEXT.equals(key)) { + Session session = context.get(GraphSynchronizationHints.SESSION); + Builtins b = session.getBuiltins(); + ModelingResources mr = session.getService(ModelingResources.class); + return queue.offer(new RelatedPropertyModification(object, mr.ElementToComponent, b.HasName, b.String, newValue, StringJavaBinding.INSTANCE), null); + } + return false; + } + +} 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 new file mode 100644 index 00000000..6c1941c8 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/StockFactory.java @@ -0,0 +1,172 @@ +/******************************************************************************* + * 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.Shape; +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.SceneGraphNodeKey; +import org.simantics.g2d.element.handler.Outline; +import org.simantics.g2d.element.handler.SceneGraph; +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.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 { + + 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.isInheritedFrom(r, dr.Terminal)) { + terminals.add(new ResourceTerminal(r)); + } + } + + return ElementClass.compile( + SimpleElementLayers.INSTANCE, + OutlinePick.INSTANCE, + TextImpl.INSTANCE, + TextColorImpl.BLACK, + TextFontImpl.DEFAULT, + DefaultTransform.INSTANCE, + new StaticObjectAdapter(elementType), + new StaticSymbolImpl(DEFAULT_IMAGE), + StaticSymbolImageInitializer.INSTANCE, + TextElementHandler.INSTANCE, + BorderSceneGraph.INSTANCE, + BoundsOutline.INSTANCE, + new WholeElementTerminals(terminals) + ).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); + } + } + +} 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 new file mode 100644 index 00000000..817c57bb --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/ValveFactory.java @@ -0,0 +1,88 @@ +/******************************************************************************* + * 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.geom.Path2D; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.diagram.adapter.SyncElementFactory; +import org.simantics.diagram.elements.TextElementHandler; +import org.simantics.g2d.canvas.ICanvasContext; +import org.simantics.g2d.diagram.IDiagram; +import org.simantics.g2d.element.ElementClass; +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.handler.impl.DefaultTransform; +import org.simantics.g2d.element.handler.impl.SimpleElementLayers; +import org.simantics.g2d.element.handler.impl.StaticObjectAdapter; +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; + + +public class ValveFactory extends SyncElementFactory { + + public static final double VALVE_SIZE = 5.0; + + @Override + public void load(ReadGraph graph, final ICanvasContext canvas, final IDiagram diagram, final Resource element, final IElement e) throws DatabaseException { + // TODO: implement + } + + @Override + public ElementClass create(ReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType) + throws DatabaseException { + String id = "Valve: " + NameUtils.getSafeName(graph, elementType); + return ElementClass.compile( + TextImpl.INSTANCE, + TextColorImpl.BLACK, + TextFontImpl.DEFAULT, + new StaticObjectAdapter(elementType), + DefaultTransform.INSTANCE, + SimpleElementLayers.INSTANCE, + TextElementHandler.INSTANCE, + new StaticSymbolImpl(VALVE_STATIC_IMAGE) + ) + .setId(id) + ; + } + + private static final Image VALVE_STATIC_IMAGE = new ShapeImage(createShape(VALVE_SIZE, false), null, new BasicStroke(1)); + + /** + * @param valveSize + * @param rotated true for vertical valve, false + * for horizontal + * @return + */ + private static Path2D createShape(double valveSize, boolean rotated) { + Path2D path = new Path2D.Double(); + path.moveTo(-valveSize, -valveSize); + if(rotated) { + path.lineTo(-valveSize, +valveSize); + path.lineTo(+valveSize, -valveSize); + } else { + path.lineTo(+valveSize, -valveSize); + path.lineTo(-valveSize, +valveSize); + } + path.lineTo(+valveSize, +valveSize); + path.closePath(); + return path; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/WholeElementTerminals.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/WholeElementTerminals.java new file mode 100644 index 00000000..46296ae9 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/WholeElementTerminals.java @@ -0,0 +1,29 @@ +package org.simantics.sysdyn.ui.elements2; + +import java.awt.Shape; +import java.util.Collection; + +import org.simantics.g2d.diagram.handler.Topology.Terminal; +import org.simantics.g2d.element.ElementUtils; +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.element.handler.impl.ObjectTerminal; +import org.simantics.g2d.element.handler.impl.Terminals; + +/** + * @author Tuukka Lehtonen + */ +public class WholeElementTerminals extends Terminals { + + private static final long serialVersionUID = -8209833430671135001L; + + public WholeElementTerminals(Collection ts) { + super(ts); + } + + @Override + public Shape getTerminalShape(IElement node, Terminal t) { + // For each terminal, return the shape of the element. + return ElementUtils.getElementShapeOrBounds(node); + } + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java index 3be2eda9..c6f7adaa 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java @@ -57,12 +57,13 @@ public class SysdynResource { public final Resource HasY; public final Resource IndependentVariable; public final Resource IsHeadOf; + public final Resource IsHeadOfTerminal; public final Resource IsTailOf; + public final Resource IsTailOfTerminal; public final Resource LookupExpression; public final Resource Module; public final Resource NormalExpression; public final Resource ParameterExpression; - public final Resource PhantomTerminal; public final Resource RefersTo; public final Resource Result; public final Resource SimulateOnChangeExperiment; @@ -73,7 +74,7 @@ public class SysdynResource { public final Resource SysdynModel; public final Resource SysdynModelManager; public final Resource SysdynProject; - public final Resource SysdynSymbols; + public final Resource SysdynTerminal; public final Resource Terminal; public final Resource Valve; public final Resource ValveSymbol; @@ -118,12 +119,13 @@ public class SysdynResource { public static final String HasY = "http://www.simantics.org/Sysdyn-1.0/HasY"; public static final String IndependentVariable = "http://www.simantics.org/Sysdyn-1.0/IndependentVariable"; public static final String IsHeadOf = "http://www.simantics.org/Sysdyn-1.0/IsHeadOf"; + public static final String IsHeadOfTerminal = "http://www.simantics.org/Sysdyn-1.0/IsHeadOfTerminal"; public static final String IsTailOf = "http://www.simantics.org/Sysdyn-1.0/IsTailOf"; + public static final String IsTailOfTerminal = "http://www.simantics.org/Sysdyn-1.0/IsTailOfTerminal"; public static final String LookupExpression = "http://www.simantics.org/Sysdyn-1.0/LookupExpression"; public static final String Module = "http://www.simantics.org/Sysdyn-1.0/Module"; public static final String NormalExpression = "http://www.simantics.org/Sysdyn-1.0/NormalExpression"; public static final String ParameterExpression = "http://www.simantics.org/Sysdyn-1.0/ParameterExpression"; - public static final String PhantomTerminal = "http://www.simantics.org/Sysdyn-1.0/PhantomTerminal"; public static final String RefersTo = "http://www.simantics.org/Sysdyn-1.0/RefersTo"; public static final String Result = "http://www.simantics.org/Sysdyn-1.0/Result"; public static final String SimulateOnChangeExperiment = "http://www.simantics.org/Sysdyn-1.0/SimulateOnChangeExperiment"; @@ -134,7 +136,7 @@ public class SysdynResource { public static final String SysdynModel = "http://www.simantics.org/Sysdyn-1.0/SysdynModel"; public static final String SysdynModelManager = "http://www.simantics.org/Sysdyn-1.0/SysdynModelManager"; public static final String SysdynProject = "http://www.simantics.org/Sysdyn-1.0/SysdynProject"; - public static final String SysdynSymbols = "http://www.simantics.org/Sysdyn-1.0/SysdynSymbols"; + public static final String SysdynTerminal = "http://www.simantics.org/Sysdyn-1.0/SysdynTerminal"; public static final String Terminal = "http://www.simantics.org/Sysdyn-1.0/Terminal"; public static final String Valve = "http://www.simantics.org/Sysdyn-1.0/Valve"; public static final String ValveSymbol = "http://www.simantics.org/Sysdyn-1.0/ValveSymbol"; @@ -189,12 +191,13 @@ public class SysdynResource { HasY = getResourceOrNull(graph, URIs.HasY); IndependentVariable = getResourceOrNull(graph, URIs.IndependentVariable); IsHeadOf = getResourceOrNull(graph, URIs.IsHeadOf); + IsHeadOfTerminal = getResourceOrNull(graph, URIs.IsHeadOfTerminal); IsTailOf = getResourceOrNull(graph, URIs.IsTailOf); + IsTailOfTerminal = getResourceOrNull(graph, URIs.IsTailOfTerminal); LookupExpression = getResourceOrNull(graph, URIs.LookupExpression); Module = getResourceOrNull(graph, URIs.Module); NormalExpression = getResourceOrNull(graph, URIs.NormalExpression); ParameterExpression = getResourceOrNull(graph, URIs.ParameterExpression); - PhantomTerminal = getResourceOrNull(graph, URIs.PhantomTerminal); RefersTo = getResourceOrNull(graph, URIs.RefersTo); Result = getResourceOrNull(graph, URIs.Result); SimulateOnChangeExperiment = getResourceOrNull(graph, URIs.SimulateOnChangeExperiment); @@ -205,7 +208,7 @@ public class SysdynResource { SysdynModel = getResourceOrNull(graph, URIs.SysdynModel); SysdynModelManager = getResourceOrNull(graph, URIs.SysdynModelManager); SysdynProject = getResourceOrNull(graph, URIs.SysdynProject); - SysdynSymbols = getResourceOrNull(graph, URIs.SysdynSymbols); + SysdynTerminal = getResourceOrNull(graph, URIs.SysdynTerminal); Terminal = getResourceOrNull(graph, URIs.Terminal); Valve = getResourceOrNull(graph, URIs.Valve); ValveSymbol = getResourceOrNull(graph, URIs.ValveSymbol); diff --git a/sysdyn_ontologies/sysdyn.graph b/sysdyn_ontologies/sysdyn.graph index 9f37766a..6cd7f836 100644 --- a/sysdyn_ontologies/sysdyn.graph +++ b/sysdyn_ontologies/sysdyn.graph @@ -9,8 +9,6 @@ %deflib L0.Type Sysdyn %deflib L0.Relation Sysdyn - - %deflib DIA.ElementClass Symbols %define assert($pred,$obj) @@ -46,7 +44,7 @@ SysdynProject : PROJ.Feature L0.HasDescription "Specifies the ontologies required by a Sysdyn project." : L0.String PROJ.RequiresNamespace "http://www.simantics.org/Sysdyn-1.0" : L0.URI - SysdynSymbols + WORKBENCH.SymbolManager PROJ.ExperimentControl SysdynModelManager : PROJ.Feature L0.PartOf Sysdyn @@ -55,12 +53,6 @@ SysdynProject : PROJ.Feature PROJ.RequiresFeature MOD.ModelingProject PROJ.RequiresFeature PROJ.ExperimentControl -SysdynSymbols : PROJ.Feature - L0.PartOf Sysdyn - L0.HasLabel "Sysdyn symbols" : L0.String - L0.HasDescription """Sysdyn symbols.""" : L0.String - PROJ.RequiresFeature WORKBENCH.SymbolManager - ###################################################################### # Model ###################################################################### @@ -146,7 +138,7 @@ Stock -# -# -#""" : L0.String - -#Stock -# MOD.ComponentTypeToSymbol StockSymbol -# -# -#""" : L0.String - - -#Valve -# MOD.ComponentTypeToSymbol ValveSymbol -# -# -#""" : L0.String - - - - - -###################################################################### -# Terminal -###################################################################### - -PhantomTerminal """ : L0.String - L0.PartOf Sysdyn - ###################################################################### # Macros for component types ###################################################################### @@ -345,95 +282,83 @@ PhantomTerminal - - -""" : L0.String - _ : PhantomTerminal - terminal("1,0,0,1,-7,0", StockSymbol , IsHeadOf) - _ : PhantomTerminal - terminal("1,0,0,1,7,0", StockSymbol , IsTailOf) + +StockSymbol - - -""" : L0.String - _ : PhantomTerminal - terminal("1,0,0,1,-7,0", ValveSymbol, IsHeadOf) - _ : PhantomTerminal - terminal("1,0,0,1,7,0", ValveSymbol, IsTailOf) + +ValveSymbol - - -""" : L0.String - _ : PhantomTerminal - terminal("1,0,0,1,-7,0", AuxiliarySymbol, IsHeadOf) - _ : PhantomTerminal - terminal("1,0,0,1,7,0", AuxiliarySymbol, IsTailOf) +AuxiliarySymbol