From: niemisto Date: Fri, 29 Jan 2010 14:06:26 +0000 (+0000) Subject: bugfixes in objmap, annotating missing expressions in diagram X-Git-Tag: simantics-1.0~82 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=be55dd756ef1aeb5f93262143e4324569b1b4489;p=simantics%2Fsysdyn.git bugfixes in objmap, annotating missing expressions in diagram git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@13775 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.h2d/src/org/simantics/h2d/node/TextNode.java b/org.simantics.h2d/src/org/simantics/h2d/node/TextNode.java index 559e6e4d..6c359cf9 100644 --- a/org.simantics.h2d/src/org/simantics/h2d/node/TextNode.java +++ b/org.simantics.h2d/src/org/simantics/h2d/node/TextNode.java @@ -12,7 +12,6 @@ import java.awt.datatransfer.Transferable; import java.awt.event.KeyEvent; import java.awt.event.MouseEvent; import java.awt.font.FontRenderContext; -import java.awt.geom.AffineTransform; import java.awt.geom.Line2D; import java.awt.geom.Rectangle2D; @@ -61,6 +60,11 @@ public class TextNode extends G2DNode { this.scale = scale; } + @SyncField({"color"}) + public void setColor(Color color) { + this.color = color; + } + public String getText() { return text; } diff --git a/org.simantics.objmap/src/org/simantics/objmap/annotations/UpdateMethod.java b/org.simantics.objmap/src/org/simantics/objmap/annotations/UpdateMethod.java new file mode 100644 index 00000000..e15a0dcf --- /dev/null +++ b/org.simantics.objmap/src/org/simantics/objmap/annotations/UpdateMethod.java @@ -0,0 +1,20 @@ +package org.simantics.objmap.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.simantics.objmap.annotations.factories.UpdateMethodFactory; +import org.simantics.objmap.annotations.meta.HasMethodRuleFactory; + +/** + * Specifies that the annotated method should be called + * to update range object. + * @author Hannu Niemistö + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +@HasMethodRuleFactory(UpdateMethodFactory.class) +public @interface UpdateMethod { +} diff --git a/org.simantics.objmap/src/org/simantics/objmap/annotations/factories/UpdateMethodFactory.java b/org.simantics.objmap/src/org/simantics/objmap/annotations/factories/UpdateMethodFactory.java new file mode 100644 index 00000000..77c2e30f --- /dev/null +++ b/org.simantics.objmap/src/org/simantics/objmap/annotations/factories/UpdateMethodFactory.java @@ -0,0 +1,51 @@ +package org.simantics.objmap.annotations.factories; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; + +import org.apache.log4j.Logger; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.objmap.IFunction; +import org.simantics.objmap.IMappingRule; +import org.simantics.objmap.MappingException; +import org.simantics.objmap.rules.factory.IMethodRuleFactory; + +public class UpdateMethodFactory implements IMethodRuleFactory { + + static Logger LOGGER = Logger.getLogger("org.simantics.objmap"); + + @Override + public IMappingRule create(ReadGraph g, + Annotation annotation, + final Method method) + throws DatabaseException { + method.setAccessible(true); + return new IMappingRule() { + + @Override + public boolean updateRange(ReadGraph g, IFunction map, + Resource domainElement, Object rangeElement) + throws MappingException { + LOGGER.info(" UpdateMethodFactory.updateRange"); + try { + return (Boolean)method.invoke(rangeElement, g, domainElement); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return false; + } + + @Override + public boolean updateDomain(WriteGraph g, IFunction map, + Resource domainElement, Object rangeElement) + throws MappingException { + return false; + } + }; + } + +} diff --git a/org.simantics.objmap/src/org/simantics/objmap/impl/Mapping.java b/org.simantics.objmap/src/org/simantics/objmap/impl/Mapping.java index fb31e2c6..ef630547 100644 --- a/org.simantics.objmap/src/org/simantics/objmap/impl/Mapping.java +++ b/org.simantics.objmap/src/org/simantics/objmap/impl/Mapping.java @@ -59,12 +59,16 @@ public class Mapping implements IMapping { } private void createDomain(WriteGraph g, Link link) throws MappingException { + LOGGER.info(" createDomain for " + link.rangeElement); ILinkType type = schema.linkTypeOfRangeElement(link.rangeElement); Resource domainElement = type.createDomainElement(g, link.rangeElement); link.type = type; link.domainElement = domainElement; domain.put(domainElement, link); + + // TODO Should we do this only if the mapping is listening? + domainModified(link); } private void createRange(ReadGraph g, Link link) throws MappingException { @@ -164,22 +168,15 @@ public class Mapping implements IMapping { public Object get(Resource element) throws MappingException { Link link = domain.get(element); if(link == null) { - ILinkType type = schema.linkTypeOfDomainElement(g, element); - Object rangeElement = type.createRangeElement(g, element); - - link = new Link(type, element, rangeElement); - domain.put(element, link); - range.put(rangeElement, link); - link.domainModified = true; - modifiedDomainLinks.add(link); - - return rangeElement; - } - else { - if(link.type == null) - createRange(g, link); - return link.rangeElement; + link = new Link(null, element, null); + link.domainModified = true; + modifiedDomainLinks.add(link); + domain.put(element, link); + createRange(g, link); } + else if(link.type == null) + createRange(g, link); + return link.rangeElement; } }; @@ -196,22 +193,15 @@ public class Mapping implements IMapping { public Resource get(Object element) throws MappingException { Link link = range.get(element); if(link == null) { - ILinkType type = schema.linkTypeOfRangeElement(element); - Resource domainElement = type.createDomainElement(g, element); - - link = new Link(type, domainElement, element); - domain.put(domainElement, link); - range.put(element, link); - link.rangeModified = true; - modifiedRangeLinks.add(link); - - return domainElement; - } - else { - if(link.type == null) - createDomain(g, link); - return link.domainElement; + link = new Link(null, null, element); + link.rangeModified = true; + modifiedRangeLinks.add(link); + range.put(element, link); + createDomain(g, link); } + else if(link.type == null) + createDomain(g, link); + return link.domainElement; } }; @@ -227,7 +217,7 @@ public class Mapping implements IMapping { } @Override - public Collection updateDomain(WriteGraph g) throws MappingException { + public synchronized Collection updateDomain(WriteGraph g) throws MappingException { LOGGER.info("Mapping.updateDomain"); RangeToDomain map = new RangeToDomain(g); ArrayList updated = new ArrayList(); @@ -236,10 +226,10 @@ public class Mapping implements IMapping { Link link = modifiedRangeLinks.remove(modifiedRangeLinks.size()-1); link.rangeModified = false; - if(link.domainModified) { + /*if(link.domainModified) { link.domainModified = false; modifiedDomainLinks.remove(link); - } + }*/ if(link.type == null) { createDomain(g, link); @@ -252,17 +242,19 @@ public class Mapping implements IMapping { } @Override - public Collection updateRange(ReadGraph g) throws MappingException { + public synchronized Collection updateRange(ReadGraph g) throws MappingException { LOGGER.info("Mapping.updateRange"); DomainToRange map = new DomainToRange(g); ArrayList updated = new ArrayList(); - while(!modifiedDomainLinks.isEmpty()) { + while(!modifiedDomainLinks.isEmpty()) { + LOGGER.info(" modifiedDomainLinks.size() = " + modifiedDomainLinks.size()); + Link link = modifiedDomainLinks.remove(modifiedDomainLinks.size()-1); link.domainModified = false; - if(link.rangeModified) { + /*if(link.rangeModified) { link.rangeModified = false; modifiedRangeLinks.remove(link); - } + }*/ if(link.type == null) { createRange(g, link); @@ -312,13 +304,16 @@ public class Mapping implements IMapping { } void domainModified(Link link) { - if(!link.domainModified) { - link.domainModified = true; - modifiedDomainLinks.add(link); - if(modifiedDomainLinks.size() == 1) { - for(IMappingListener listener : listeners) - listener.domainModified(); - } + if(!link.domainModified) { + synchronized(modifiedDomainLinks) { + LOGGER.info(" domainModified for " + link.rangeElement); + link.domainModified = true; + modifiedDomainLinks.add(link); + if(modifiedDomainLinks.size() == 1) { + for(IMappingListener listener : listeners) + listener.domainModified(); + } + } } } @@ -331,12 +326,14 @@ public class Mapping implements IMapping { void rangeModified(Link link) { if(!link.rangeModified) { - link.rangeModified = true; - modifiedRangeLinks.add(link); - if(modifiedRangeLinks.size() == 1) { - for(IMappingListener listener : listeners) - listener.rangeModified(); - } + synchronized(modifiedRangeLinks) { + link.rangeModified = true; + modifiedRangeLinks.add(link); + if(modifiedRangeLinks.size() == 1) { + for(IMappingListener listener : listeners) + listener.rangeModified(); + } + } } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SysdynDiagramEditor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SysdynDiagramEditor.java index 813be9ad..fee183cb 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SysdynDiagramEditor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/SysdynDiagramEditor.java @@ -78,7 +78,7 @@ public class SysdynDiagramEditor extends ResourceEditorPart { mapping.addMappingListener(new IMappingListener() { @Override - public void rangeModified() { + public void rangeModified() { session.asyncRequest(new WriteRequest() { @Override diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/AuxiliaryElement.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/AuxiliaryElement.java index e10f97f6..e143b7a5 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/AuxiliaryElement.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/AuxiliaryElement.java @@ -4,12 +4,12 @@ import org.simantics.objmap.annotations.GraphType; @GraphType("http://www.simantics.org/Sysdyn#Auxiliary") public class AuxiliaryElement extends TextElement { - + public AuxiliaryElement() { } public AuxiliaryElement(String label, double x, double y) { super(label, x, y); - } + } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/StockElement.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/StockElement.java index 01c66927..a171f60c 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/StockElement.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/StockElement.java @@ -2,6 +2,7 @@ package org.simantics.sysdyn.ui.elements; import org.simantics.h2d.node.RectangleNode; import org.simantics.objmap.annotations.GraphType; +import org.simantics.objmap.annotations.UpdateMethod; import org.simantics.scenegraph.g2d.G2DParentNode; @GraphType("http://www.simantics.org/Sysdyn#Stock") diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/TextElement.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/TextElement.java index 860091f8..53d32ab2 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/TextElement.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/TextElement.java @@ -6,6 +6,9 @@ import java.awt.font.FontRenderContext; import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; import org.simantics.h2d.action.IAction; import org.simantics.h2d.editor.IDiagramEditor; import org.simantics.h2d.element.handler.Connectable; @@ -18,7 +21,9 @@ import org.simantics.h2d.node.ITextListener; import org.simantics.h2d.node.TextNode; import org.simantics.objmap.annotations.GraphType; import org.simantics.objmap.annotations.RelatedValue; +import org.simantics.objmap.annotations.UpdateMethod; import org.simantics.scenegraph.g2d.G2DParentNode; +import org.simantics.sysdyn.SysdynResource; @GraphType("http://www.simantics.org/Sysdyn#Auxiliary") public class TextElement extends RectangularElement implements Movable, Connectable, IEventHandler { @@ -91,8 +96,11 @@ public class TextElement extends RectangularElement implements Movable, Connecta textBounds.getHeight()+YPADDING*2 ); - if(textNode != null) - textNode.init(label, FONT, Color.BLACK, textX, textY, FONT_SCALE); + if(textNode != null) { + textNode.init(label, FONT, hasEquation ? Color.BLACK : Color.RED, + textX, textY, FONT_SCALE); + oldHasEquation = hasEquation; + } } protected void update() { @@ -170,5 +178,30 @@ public class TextElement extends RectangularElement implements Movable, Connecta } return false; } + + boolean oldHasEquation = false; + boolean hasEquation = false; + @UpdateMethod + boolean readHasExpression(ReadGraph g, Resource resource) { + try { + System.out.println("readHasExpression " + + g.getRelatedValue(resource, + g.getBuiltins().HasName)); + } catch (DatabaseException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + SysdynResource sr = SysdynResource.getInstance(g); + try { + hasEquation = + !g.getObjects(resource, sr.HasExpression).isEmpty(); + if(oldHasEquation != hasEquation) + update(); + } catch (DatabaseException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return false; + } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java index 02d79200..40774e98 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java @@ -112,7 +112,6 @@ public class TrendView extends ViewPart { @Override public void selectionChanged(IWorkbenchPart part, ISelection selection) { - System.out.println(PinTrend.getState()); if(selection.isEmpty() || Boolean.TRUE.equals(PinTrend.getState())) return; if(selection instanceof IStructuredSelection) {