--- /dev/null
+package org.simantics.objmap.annotations.factories;\r
+\r
+import java.lang.annotation.Annotation;\r
+import java.lang.reflect.Method;\r
+\r
+import org.apache.log4j.Logger;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.objmap.IFunction;\r
+import org.simantics.objmap.IMappingRule;\r
+import org.simantics.objmap.MappingException;\r
+import org.simantics.objmap.rules.factory.IMethodRuleFactory;\r
+\r
+public class UpdateMethodFactory implements IMethodRuleFactory {\r
+\r
+ static Logger LOGGER = Logger.getLogger("org.simantics.objmap");\r
+ \r
+ @Override\r
+ public IMappingRule create(ReadGraph g, \r
+ Annotation annotation, \r
+ final Method method)\r
+ throws DatabaseException {\r
+ method.setAccessible(true);\r
+ return new IMappingRule() {\r
+ \r
+ @Override\r
+ public boolean updateRange(ReadGraph g, IFunction<Resource, Object> map,\r
+ Resource domainElement, Object rangeElement)\r
+ throws MappingException {\r
+ LOGGER.info(" UpdateMethodFactory.updateRange"); \r
+ try {\r
+ return (Boolean)method.invoke(rangeElement, g, domainElement);\r
+ } catch (Exception e) {\r
+ // TODO Auto-generated catch block\r
+ e.printStackTrace();\r
+ }\r
+ return false;\r
+ }\r
+ \r
+ @Override\r
+ public boolean updateDomain(WriteGraph g, IFunction<Object, Resource> map,\r
+ Resource domainElement, Object rangeElement)\r
+ throws MappingException {\r
+ return false;\r
+ }\r
+ };\r
+ }\r
+\r
+}\r
}\r
\r
private void createDomain(WriteGraph g, Link link) throws MappingException {\r
+ LOGGER.info(" createDomain for " + link.rangeElement);\r
ILinkType type = schema.linkTypeOfRangeElement(link.rangeElement);\r
Resource domainElement = type.createDomainElement(g, link.rangeElement);\r
\r
link.type = type;\r
link.domainElement = domainElement;\r
domain.put(domainElement, link);\r
+ \r
+ // TODO Should we do this only if the mapping is listening?\r
+ domainModified(link);\r
}\r
\r
private void createRange(ReadGraph g, Link link) throws MappingException {\r
public Object get(Resource element) throws MappingException {\r
Link link = domain.get(element);\r
if(link == null) {\r
- ILinkType type = schema.linkTypeOfDomainElement(g, element);\r
- Object rangeElement = type.createRangeElement(g, element);\r
- \r
- link = new Link(type, element, rangeElement);\r
- domain.put(element, link); \r
- range.put(rangeElement, link);\r
- link.domainModified = true;\r
- modifiedDomainLinks.add(link);\r
- \r
- return rangeElement; \r
- }\r
- else {\r
- if(link.type == null) \r
- createRange(g, link);\r
- return link.rangeElement;\r
+ link = new Link(null, element, null);\r
+ link.domainModified = true;\r
+ modifiedDomainLinks.add(link);\r
+ domain.put(element, link); \r
+ createRange(g, link); \r
}\r
+ else if(link.type == null) \r
+ createRange(g, link);\r
+ return link.rangeElement;\r
}\r
\r
};\r
public Resource get(Object element) throws MappingException {\r
Link link = range.get(element);\r
if(link == null) {\r
- ILinkType type = schema.linkTypeOfRangeElement(element); \r
- Resource domainElement = type.createDomainElement(g, element);\r
- \r
- link = new Link(type, domainElement, element);\r
- domain.put(domainElement, link); \r
- range.put(element, link);\r
- link.rangeModified = true;\r
- modifiedRangeLinks.add(link);\r
- \r
- return domainElement; \r
- }\r
- else {\r
- if(link.type == null)\r
- createDomain(g, link);\r
- return link.domainElement;\r
+ link = new Link(null, null, element);\r
+ link.rangeModified = true;\r
+ modifiedRangeLinks.add(link);\r
+ range.put(element, link);\r
+ createDomain(g, link); \r
}\r
+ else if(link.type == null)\r
+ createDomain(g, link);\r
+ return link.domainElement;\r
}\r
\r
};\r
}\r
\r
@Override\r
- public Collection<Resource> updateDomain(WriteGraph g) throws MappingException {\r
+ public synchronized Collection<Resource> updateDomain(WriteGraph g) throws MappingException {\r
LOGGER.info("Mapping.updateDomain");\r
RangeToDomain map = new RangeToDomain(g);\r
ArrayList<Resource> updated = new ArrayList<Resource>();\r
\r
Link link = modifiedRangeLinks.remove(modifiedRangeLinks.size()-1);\r
link.rangeModified = false;\r
- if(link.domainModified) {\r
+ /*if(link.domainModified) {\r
link.domainModified = false;\r
modifiedDomainLinks.remove(link);\r
- }\r
+ }*/\r
\r
if(link.type == null) {\r
createDomain(g, link);\r
}\r
\r
@Override\r
- public Collection<Object> updateRange(ReadGraph g) throws MappingException {\r
+ public synchronized Collection<Object> updateRange(ReadGraph g) throws MappingException {\r
LOGGER.info("Mapping.updateRange");\r
DomainToRange map = new DomainToRange(g);\r
ArrayList<Object> updated = new ArrayList<Object>();\r
- while(!modifiedDomainLinks.isEmpty()) {\r
+ while(!modifiedDomainLinks.isEmpty()) { \r
+ LOGGER.info(" modifiedDomainLinks.size() = " + modifiedDomainLinks.size());\r
+ \r
Link link = modifiedDomainLinks.remove(modifiedDomainLinks.size()-1);\r
link.domainModified = false;\r
- if(link.rangeModified) {\r
+ /*if(link.rangeModified) {\r
link.rangeModified = false;\r
modifiedRangeLinks.remove(link);\r
- }\r
+ }*/\r
\r
if(link.type == null) {\r
createRange(g, link);\r
}\r
\r
void domainModified(Link link) {\r
- if(!link.domainModified) {\r
- link.domainModified = true;\r
- modifiedDomainLinks.add(link);\r
- if(modifiedDomainLinks.size() == 1) {\r
- for(IMappingListener listener : listeners)\r
- listener.domainModified();\r
- }\r
+ if(!link.domainModified) { \r
+ synchronized(modifiedDomainLinks) {\r
+ LOGGER.info(" domainModified for " + link.rangeElement);\r
+ link.domainModified = true;\r
+ modifiedDomainLinks.add(link);\r
+ if(modifiedDomainLinks.size() == 1) {\r
+ for(IMappingListener listener : listeners)\r
+ listener.domainModified();\r
+ }\r
+ }\r
}\r
}\r
\r
\r
void rangeModified(Link link) {\r
if(!link.rangeModified) {\r
- link.rangeModified = true;\r
- modifiedRangeLinks.add(link);\r
- if(modifiedRangeLinks.size() == 1) {\r
- for(IMappingListener listener : listeners)\r
- listener.rangeModified();\r
- }\r
+ synchronized(modifiedRangeLinks) {\r
+ link.rangeModified = true;\r
+ modifiedRangeLinks.add(link);\r
+ if(modifiedRangeLinks.size() == 1) {\r
+ for(IMappingListener listener : listeners)\r
+ listener.rangeModified();\r
+ }\r
+ }\r
}\r
}\r
\r
import java.awt.geom.AffineTransform;\r
import java.awt.geom.Rectangle2D;\r
\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
import org.simantics.h2d.action.IAction;\r
import org.simantics.h2d.editor.IDiagramEditor;\r
import org.simantics.h2d.element.handler.Connectable;\r
import org.simantics.h2d.node.TextNode;\r
import org.simantics.objmap.annotations.GraphType;\r
import org.simantics.objmap.annotations.RelatedValue;\r
+import org.simantics.objmap.annotations.UpdateMethod;\r
import org.simantics.scenegraph.g2d.G2DParentNode;\r
+import org.simantics.sysdyn.SysdynResource;\r
\r
@GraphType("http://www.simantics.org/Sysdyn#Auxiliary")\r
public class TextElement extends RectangularElement implements Movable, Connectable, IEventHandler {\r
textBounds.getHeight()+YPADDING*2\r
); \r
\r
- if(textNode != null)\r
- textNode.init(label, FONT, Color.BLACK, textX, textY, FONT_SCALE);\r
+ if(textNode != null) {\r
+ textNode.init(label, FONT, hasEquation ? Color.BLACK : Color.RED, \r
+ textX, textY, FONT_SCALE);\r
+ oldHasEquation = hasEquation;\r
+ }\r
}\r
\r
protected void update() {\r
}\r
return false;\r
} \r
+\r
+ boolean oldHasEquation = false;\r
+ boolean hasEquation = false;\r
\r
+ @UpdateMethod\r
+ boolean readHasExpression(ReadGraph g, Resource resource) {\r
+ try {\r
+ System.out.println("readHasExpression " +\r
+ g.getRelatedValue(resource, \r
+ g.getBuiltins().HasName));\r
+ } catch (DatabaseException e1) {\r
+ // TODO Auto-generated catch block\r
+ e1.printStackTrace();\r
+ }\r
+ SysdynResource sr = SysdynResource.getInstance(g);\r
+ try {\r
+ hasEquation = \r
+ !g.getObjects(resource, sr.HasExpression).isEmpty();\r
+ if(oldHasEquation != hasEquation) \r
+ update(); \r
+ } catch (DatabaseException e) {\r
+ // TODO Auto-generated catch block\r
+ e.printStackTrace();\r
+ } \r
+ return false;\r
+ }\r
}\r